Python Equivalent for returning time series object

#1

Hi There,

referring to the link below where the second snippet that has a return function:

  return transformToTimeseries(results, consumption);

#Metric referencing actionDecl-based metric returns empty timeseries
What is the python equivelant to return a time series object?

 def simpleCorrelation(Y,x):

    try:
	    indi = list(reduce(lambda x, y: x + y, Y.items()))
	    depen = list(reduce(lambda x, y: x + y, x.items()))
    except NameError:
	    indi = list(fc.reduce(lambda x, y: x + y, Y.items()))
	    depen = list(fc.reduce(lambda x, y: x + y, x.items()))		


    a = (np.corrcoef(indi[1], depen[1]))

    flat_list = [item for sublist in a for item in sublist]
    r = (flat_list[1])

    return TimeSeries(r)  #<------------------------????????????????????????

Or do I need to build in more functionality to return a list of (len(Y))?

Cheers!

0 Likes

#2

Hi Aaron,

It is not recommended to create actionDecl metrics unless it is absolutely necessary. The simple reason is that the platform does not have a good way of optimizing actionDecl metrics since it is user-specific.

When you don’t know how to create a metric using the current expression engine functions, please post on community.c3.ai and ask. There is usually a solution (may not be elegant, but optimized) to achieve what you want. If there is absolutely no way to achieve what you want, the platform team will decide if it is common enough to be added to the expression engine functions or if it needs to be achieved through actionDecl.

Now, let’s talk about the case you want. For any metric you create, you need to think about what is the expected return value for EVERY time stamp. I’m assuming you want to create a compound metric. Let’s use a concrete example.

Can you tell me, in the following example, when you just evaluate the metric from timestamp 3 to time stamp 5, what results are you expecting? What are the three values and how did you calculate them?

timestamp: 1, 2, 3, 4, 5, 6, 7, 8, 9
time series A: 11, 12, 13, 14, 15, 16, 17, 18, 19
time series B: 21, 22, 23, 24, 25, 26, 27, 28, 29
aaron’s corr: -, -, ?, ?, ?, -, -, -, -

0 Likes

#3

Hi Qiwei,

Corr coef will only work on a range of of values 3-5 as in a window.
This returns 1.

This question is more directed at understanding what the data/object is that is going in and out of the function.
In the case below I have passed in a dictionary and I don’t know what c3 platform will pass to my function. I am assuming its a json object? This is what I need to know.

Then what do I return in the js example (link Metric referencing actionDecl-based metric returns empty timeseries)

It was diplayed as:
return transformToTimeseries(results, consumption);

Do I need to use the same expression as I am assuming so… this is what I need the confirmation on.

  • Can I use the expression the same way or is it different for python.

Please see the expanded use case Creating Expression Engine Function and Add to Window

I only used the try except as I am unsure if functools is on the platform but is built in to py3 I’m assuming.

import numpy as np
import functools as fc


def simpleCorrelation(Y,x):

	try:
		indi = list(reduce(lambda x, y: x + y, Y.items()))
		depen = list(reduce(lambda x, y: x + y, x.items()))
	except NameError:
		indi = list(fc.reduce(lambda x, y: x + y, Y.items()))
		depen = list(fc.reduce(lambda x, y: x + y, x.items()))		
	
	# print (indi[1])
	a = (np.corrcoef(indi[1], depen[1]))

	flat_list = [item for sublist in a for item in sublist]
	r = (flat_list[1])
	

	return (r)



one = {'WellMeasurementWaterRate' : [13,14,15]}
two = {'WellMeasurementGasRate' : [23, 24, 25]}
a = simpleCorrelation(one,two)
print (a)
0 Likes

#4

I completely understood your question. I’m trying to help you think this problem in the C3 way. Please allow me.

Given this example,

timestamp: 1, 2, 3, 4, 5, 6, 7, 8, 9
time series A: 11, 12, 13, 14, 15, 16, 17, 18, 19
time series B: 21, 22, 23, 24, 25, 26, 27, 28, 29

Let’s say you just want to create the metric (A+B)
Metric (A+B), evaluated from timestamp 3 to timestamp 5, will return
(A+B): -, -, 13+23, 14+24, 15+25, -, -, -, -
which is,
(A+B): -, -, 36, 38, 40, -, -, -, -

So for you metric, evaluated from timestamp 3 to timestamp 5, THREE values need to be returned.
(aaron’s corr): -, -, ?, ?, ?, -, -, -, -

I don’t understand when you said “Corr coef will only work on a range of of values 3-5 as in a window.
This returns 1.”

Do you mean the returned time series is the following?
(aaron’s corr): -, -, 1, 1, 1, -, -, -, -

Remember: your logic of the metric, should return one value for EVERY timestamp.

0 Likes

#5

yes for every row the correlation will be a value between -1 and 1

This can be used to explain causality.
or
This can then be translated into additional metrics that are binary
such as +ve / -ve corr for a given period on a specified threshold of an acceptable confidence level.

(aaron’s corr): -, -, +ve, +ve, +ve, -, -, -, -
but actually returned as a 0 or 1

0 Likes

#6

In that example, the transformToTimeseries was just a javascript function in the same file as calculateElectrictySpending and not declared in the *.c3typ file at all (THIS IS BAD PRACTICE - one reason being that users won’t know what the inputs and outputs are…).

Here it is, the critical functions are Timeseries.makeNorm and Timeseries.fromObjs. These can be found by examining the Timeseries type.


function transformToTimeseries(results, consumption) {
  var costItems = CalculatedTariffCostItem.array(results);
  var objArry = ObjArry.make({ value: costItems });
  var tsSpec = TSEvalSpec.make({start : consumption.start(), end : consumption.end(), interval : consumption.interval()});
  var asTsSpec = AsTimeseriesSpec.make({
    treatment: "INTEGRAL",
    startPath: "start",
    endPath: "end",
    valuePath: "cost"
  });
  var tsObj = Timeseries.fromObjs(objArry, tsSpec, asTsSpec);

  return Timeseries.makeNorm(NormTimeseriesDoubleSpec.make({
    missing: consumption.missing()
  }), tsObj);
}
0 Likes

Is it possible to to evaluate a metric using passed-in data?