I want to do the exact same thing as this post, but with a 2d array of objects instead of just a 2d array of numbers.
class ExchangeRate:
rate = None
name = None
otherUsefulProperty = None
def __init__(self, _rate, _name, _otherUsefulProperty):
self.rate = _rate
self.name = _name
self.otherUsefulProperty = _otherUsefulProperty
This is my object's class. The rate
property would be the one that is used when merging the graphs together.
The below code works great with a 2d array of numbers, but I haven't been able to figure out how to do the same thing efficiently with a 2d array of objects. Note, performance is critical in what I'm doing. This is assuming a 2d array of objects is indeed performant. If it's not and there's a more performant way, please let me know.
import numpy as np
graph = np.ndarray(shape=(4, 3, 3), dtype=float, order='F')
graph[0] = [[0, 0, 1], [1, 0, 1], [2, 0, 0]]
graph[1] = [[0, 0, 1], [1, 0, 1], [2, 0, 0]]
graph[2] = [[5, 0, 0], [1, 0, 1], [2, 0, 0]]
graph[3] = [[2, 1, 0], [9, 0, 1], [0, 0, 0]]
PrintAndLog("graph of type " + str(type(graph)) + " = \n" + str(graph))
PrintAndLog("\n\n")
resultGraph = graph.max(axis=0)
PrintAndLog("resultGraph of type " + str(type(resultGraph)) + " = \n" + str(resultGraph))
Output:
graph of type <class 'numpy.ndarray'> =
[[[ 0. 0. 1.]
[ 1. 0. 1.]
[ 2. 0. 0.]]
[[ 0. 0. 1.]
[ 1. 0. 1.]
[ 2. 0. 0.]]
[[ 5. 0. 0.]
[ 1. 0. 1.]
[ 2. 0. 0.]]
[[ 2. 1. 0.]
[ 9. 0. 1.]
[ 0. 0. 0.]]]
resultGraph of type <class 'numpy.ndarray'> =
[[ 5. 1. 1.]
[ 9. 0. 1.]
[ 2. 0. 0.]]
Possible final solution:
Hopefully someone else finds this useful. I just did a ton of performance testing and there is a clear winner.
TLDR: Winner = np.array
+ ExchangeRate object
+ graph.max(axis=0)
. This is by far the fastest way I've tried. And again, the goal is to merge many rates graphs, where each rate also has metadata associated with it that needs to merge along with it
Here are the test results from the final methods I narrowed it down to. Each test was timed based on merging only (where I merge several graphs into one based on rate). I recorded the following data points: average duration over 200 runs, and the duration of the very first run. The very first run was important because it seemed to sometimes be longer than the average. It may have to do with caching.
test_graph_4 200 run average = 0.0003026 seconds (first run, ~same)
test_graph_3 200 run average = 0.0003836 seconds (first run, ~same)
test_graph_2b 200 run average = 0.000018 seconds (first run 0.000092 seconds)
test_graph_2a 200 run average = 0.000066 seconds (first run 0.000143 seconds)