{"id":68490,"date":"2024-08-15T11:42:02","date_gmt":"2024-08-15T03:42:02","guid":{"rendered":"https:\/\/oihelp.corporate.ifs.com\/help\/?page_id=68490"},"modified":"2024-08-15T13:36:13","modified_gmt":"2024-08-15T05:36:13","slug":"pythontimeseries","status":"publish","type":"page","link":"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/calculations\/pythontimeseries\/","title":{"rendered":"PythonTimeSeries()"},"content":{"rendered":"\n<p class=\"left-bar\">This article applies to IFS OI Explorer version 4.17 and later.<\/p>\n<h2><span id=\"Format\">Format<\/span><\/h2>\n<p class=\"intro-text\"><strong>PythonTimeSeries (Script [,Timestamp Pattern] [,Parameters])<\/strong><\/p>\n<h2><span id=\"Returns\">Returns<\/span><\/h2>\n<p class=\"intro-text\">Allows users to pass a Python script into the calculation, and returns values as time series data.<\/p>\n<hr \/>\n<h2><span id=\"Inputs\">Inputs<\/span><\/h2>\n<p class=\"intro-text\"><b>Script: <\/b>The Python script to execute. This is case sensitive.\u00a0<br \/>\nBehaviour: Required<br \/>\nDimensions: SingleValue, MultiCollection<br \/>\nValid data types: String<\/p>\n<p class=\"intro-text\"><b>Timestamp Pattern<\/b>: The formatting string used to pass the timestamps of the data points as text e.g. dd\/M\/yyyy H:mm:s<br \/>\nBehaviour: Optional [0..1]<br \/>\nDimensions: SingleValue<br \/>\nValid data types: Null, String<\/p>\n<p class=\"intro-text\"><b>Parameters<\/b>: The parameters to use as inputs for the Script. These parameters are case sensitive and are passed as data frames. For more information, see <strong><a href=\"#Parameters\">Parameters<\/a><\/strong> below.<br \/>\nBehaviour: Optional [0..n]<br \/>\nDimensions: Expression<br \/>\nValid data types: Expression<\/p>\n<p class=\"left-bar\">Related: <a href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/calculations\/calculation-syntax\/#Data_Type\">How to format data types<\/a><\/p>\n<h3>Parameters<\/h3>\n<p class=\"intro-text\">The input parameters are passed as data frames with the following columns:<\/p>\n<ul class=\"intro-text\">\n<li><strong>timestamp<\/strong>: If a timestamp pattern is specified for <strong>PythonTimeSeries <\/strong>() (using the 2<sup>nd<\/sup> parameter) then this will be a string type, otherwise it will be a numeric type (expressed in seconds since Unix epoch).<\/li>\n<li><strong>value<\/strong>: The value of this column depends on the type of parameter passed into the script. It can be a numeric, integer, or string data type.<\/li>\n<li><strong>confidence<\/strong>: This is an integer data type.<\/li>\n<\/ul>\n<p class=\"intro-text\">There are also some additional input parameters:<\/p>\n<ul class=\"intro-text\">\n<li>The expression of the parameter is passed to the Python script as a simple string with the name \"parameter<strong>N<\/strong>Name\" where N is the index of the parameter. <br \/>\nE.g. If you call <strong>PythonTimeSeries <\/strong><span class=\"code-fragment\">(\"[Python script]\", null, 1 + 2, {Silver}, {Well 1:THP})<\/span> then, in your script:<\/p>\n<ul class=\"intro-text\">\n<li>parameter1Name will be a string which contains <span class=\"code-fragment\">\"1 + 2\"<\/span><\/li>\n<li>parameter2Name will be a string which contains <span class=\"code-fragment\">\"{Silver}\"<\/span><\/li>\n<li>parameter3Name contains <span class=\"code-fragment\">\"{Well 1:THP}\"<\/span><\/li>\n<\/ul>\n<\/li>\n<li>If the above-mentioned expression is an <em>entity-attribute<\/em> fetch then an additional string variable called \"parameter<strong>N<\/strong>Entity\" will be created, where N is the index of the parameter. <br \/>\nE.g. If you call <strong>PythonTimeSeries <\/strong><span class=\"code-fragment\">(\"[Python script]\", null, 1 + 2, {Silver}, {Well 1:THP})<\/span> then:<\/p>\n<ul class=\"intro-text\">\n<li>parameter1Entity and parameter2Entity will not exist<\/li>\n<li>parameter3Entity will exist and will contain <span class=\"code-fragment\">\"Well 1\"<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>output['value']<\/h3>\n<p class=\"intro-text\">The output parameter <strong>output['value']<\/strong> is a data frame with the following columns:<\/p>\n<ul class=\"intro-text\">\n<li><strong>timestamp<\/strong>: This is pre-populated with timestamps based on the timestamp pattern parameter (string vs numeric), and a timestamp will be created for every sample interval within the start and end time of the fetch (this applies to raw requests as well).<\/li>\n<li><strong>value<\/strong>: This is left empty and the Python script is expected to fill it in.<\/li>\n<li><strong>confidence<\/strong>: This is set to 100 in every row.<\/li>\n<\/ul>\n<p class=\"intro-text\">The output parameter will have as many rows as the sample intervals are expected to have, and the function will expect the result in the <strong>output<\/strong> variable. This variable can be either a:<\/p>\n<ul class=\"intro-text\">\n<li><strong>Data frame<\/strong>, in which case:\n<ul class=\"intro-text\">\n<li>A column called \"value\" must exist and it must be an integer, numeric, or string data type.<\/li>\n<li>If a column called \"timestamp\" also exists (this is optional), then its type must be <em>string<\/em> or <em>numeric<\/em> (based on the timestamp pattern parameter).<\/li>\n<li>If a column called \"confidence\" also exists (this is optional), then its type must be <em>integer<\/em>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Vector<\/strong>:\n<ul class=\"intro-text\">\n<li>Character vectors will be turned into <em>string<\/em> collections.<\/li>\n<li>Integer vectors will be turned into <em>integer<\/em> collections.<\/li>\n<li>Numeric vectors will be turned into <em>decimal<\/em> collections.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h2><span id=\"Useful_to_Know\">Useful to Know<\/span><\/h2>\n<p class=\"intro-text\">This function requires:<\/p>\n<ul class=\"intro-text\">\n<li>IFS OI Server licence with R capabilities.<\/li>\n<li>Python v3.7-v3.11 installed on the Explorer server machine.<\/li>\n<li>We strongly recommend installing the <strong>pandas<\/strong> module, for full access to Python\u2019s data analysis features.<\/li>\n<li>Additional settings for configuring the Python Adaptor can be found in the \"Python Adaptor\" group in the <a href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-explorer\/explorer-admin\/serverconfig-xml\/\">ServerConfig.xml<\/a> file.<\/li>\n<\/ul>\n<hr \/>\n<h2><span id=\"Examples\">Examples<\/span><\/h2>\n<p class=\"intro-text\">The following examples demonstrate how the PythonTimesSeries() function works, but cannot be used in Explorer trends or pages as tags, as there are no timestamps returned.<\/p>\n<p class=\"intro-text\">You can paste these examples into the calculation editor and test to see what the results are, in the editor.<\/p>\n<p class=\"intro-text\">Expression: PythonTimeSeries(\"output['value'] = parameter1\", null, 5.6)<br \/>\nOutput: Returns 5.6 as the value for each timestamp.<\/p>\n<p class=\"intro-text\">Expression: PythonTimeSeries(\"output['value'] = parameter1Name\", null, 2.2 + 3.4)<br \/>\nOutput: Returns the string 2.2 + 3.4 as the value for each timestamp.<\/p>\n<p class=\"intro-text\">Expression: PythonTimeSeries(\"output['value'] = scipy.interpolate.splev(1,scipy.interpolate.splrep(parameter1['timestamp'], parameter1['value']))\", null, {0072895.PV})<br \/>\nOutput: Fits a cubic smoothing spline on the values fetched by the <em>0072895.PV<\/em> tag.<\/p>\n<hr \/>\n<h2><span id=\"Release_History\">Release History<\/span><\/h2>\n<ul class=\"intro-text\">\n<li>PythonTimeSeries() 4.17.0\n<ul class=\"intro-text\">\n<li>Initial version<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The PythonTimeSeries() calculation function allows users to access Python, as a way of analysing data within your calculations.<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/calculations\/pythontimeseries\/\">Read more<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":36105,"parent":3650,"menu_order":165,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[9],"tags":[1148],"class_list":["post-68490","page","type-page","status-publish","has-post-thumbnail","hentry","category-tech-ref","tag-python","Version-4-17","Product-srv"],"_links":{"self":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/68490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/comments?post=68490"}],"version-history":[{"count":5,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/68490\/revisions"}],"predecessor-version":[{"id":68547,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/68490\/revisions\/68547"}],"up":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/3650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/media\/36105"}],"wp:attachment":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/media?parent=68490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/categories?post=68490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/tags?post=68490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}