NOTE: The post looks longer than it ought to because of docstrings and an array consisting of 40 datetimes.
I have some time-series data. For examples sake, let's say I have three parameters, each consisting of 40 data points: datetimes (given by dts
), speed (given by vobs
), and elapsed hour (given by els
), which are combined by key into a dictionary data_dict
.
dts = np.array(['2006/01/01 02:30:04', '2006/01/01 03:30:04', '2006/01/01 03:54:04'
,'2006/01/01 05:30:04', '2006/01/01 06:30:04', '2006/01/01 07:30:04'
,'2006/01/01 08:30:04', '2006/01/01 09:30:04', '2006/01/01 10:30:04'
,'2006/01/01 11:30:04', '2006/01/01 12:30:04', '2006/01/01 13:30:04'
,'2006/01/01 14:30:04', '2006/01/01 15:30:04', '2006/01/01 16:30:04'
,'2006/01/01 17:30:04', '2006/01/01 18:30:04', '2006/01/01 19:30:04'
,'2006/01/01 20:30:04', '2006/01/01 21:30:04', '2006/01/01 21:54:05'
,'2006/01/01 23:30:04', '2006/01/02 00:30:04', '2006/01/02 01:30:04'
,'2006/01/02 02:30:04', '2006/01/02 03:30:04', '2006/01/02 04:30:04'
,'2006/01/02 05:30:04', '2006/01/02 06:30:04', '2006/01/02 07:30:04'
,'2006/01/02 08:30:04', '2006/01/02 09:30:04', '2006/01/02 10:30:04'
,'2006/01/02 11:30:04', '2006/01/02 12:30:04', '2006/01/02 13:30:04'
,'2006/01/02 14:30:04', '2006/01/02 15:30:04', '2006/01/02 16:30:04'
,'2006/01/02 17:30:04'])
vobs = np.array([158, 1, 496, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 823, 1, 1, 1, 1, 303, 1, 1, 1, 1, 253, 1, 1, 1, 408, 1
, 1, 1, 1, 321])
els = np.array([i for i in range(len(vobs))])
data_dictionary = {'datetime' : dts, 'values' : vobs, 'elapsed' : els}
I have a function that takes a dictionary as an input and outputs a single scalar value of type <float>
or type <int>
. The function given below is simpler than my actual use case and is given for examples sake.
def get_z(dictionary):
""" This function returns a scalar value. """
return np.sum(dictionary['elapsed'] / dictionary['values'])
I would like to see how this function output changes as the time-interval changes. So, I've created a function that takes a dictionary as input and outputs a new dictionary, the array values of which are sliced at the input indices for each of the keys in the input dictionary. Note that the consecutive elapsed hours can serve as indices.
def subsect(dictionary, indices):
""" This function returns a dictionary, the array values
of which are sliced at the input indices. """
return {key : dictionary[key][indices] for key in list(dictionary.keys())}
To verify that the above functions work, one can run the for-loop containing the function read_dictionary(...)
below.
def read_dictionary(dictionary):
""" This function prints the input dictionary as a check. """
for key in list(dictionary.keys()):
print(" .. KEY = {}\n{}\n".format(key, dictionary[key]))
print("\nORIGINAL DATA DICTIONARY\n")
read_dictionary(data_dictionary)
# for i in range(1, 38):
# mod_dictionary = subsect(data_dictionary, indices=slice(i, 39, 1))
# print("\n{}th MODIFIED DATA DICTIONARY\n".format(i))
# read_dictionary(mod_dictionary)
My issue is that I would like a contour plot. The x-axis will contain the lower bound of the datetime interval (the first entry of mod_dictionary[i]
) while the y-axis will contain the upper bound of the datetime interval (the last entry of mod_dictioary[i]
). Normally when making a contour plot, one has an array of (x,y)
values that are made into a grid (X,Y)
via numpy.meshgrid
. As my actual function (not the one in the example) is not vectorized, I can use X.copy().reshape(-1)
and reshape my result back using (...).reshape(X.shape)
.
My exact problem is that I do not know how I can make a grid of different parameters using a single dictionary as an input for a function that outputs a single scalar value. Is there a way to do this?