New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aggregated read functionality #87
Conversation
6912a63
to
57350fa
Compare
3a5e69b
to
809a20f
Compare
57350fa
to
7ba5b20
Compare
7ba5b20
to
390f48c
Compare
809a20f
to
3685de1
Compare
@swillner are you happy with the tests that have been added? |
Very! ;) |
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
aaf9569
to
4c389cc
Compare
@swillner this is good to go from my end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid there are some issues with this approach:
- Why do we need to catch NameError?
- The unit converter converts between this parameter's unit and the requested one, so the one of the child parameter might be different (same for the timeframe)
- Once we attempt an aggregated view, shall we allow added children?
- Minor: Return type is missing
- Minor: We should probably make this a protected ("_"-prefixed) method
- Major: Rather than summing and then caching the sum in
self._parameter._data
I would collect views to the children's values and sum them onget
(comes with the penality of always doing the sum for each get, even if there have been no changes, but the conversion is done and changes in the child parameters reflect on a newget
as is the idea of the parameters views) - The recursion can be much simplyfied similar to:
def _sum_child_data(self) -> float:
"""
Sum child data.
Returns
-------
float
Sum of all child data
"""
if self._parameter._children:
data = 0
for _, cp in self._parameter._children.items():
data += self._unit_converter.convert_from(cp._sum_child_data()) # TODO fix unit conversion
else:
return self._parameter._data
(similar for timeseries).
We don't, it's just another way to do these loops where you initialise in the first iteration then add later. These are all equivalent I think from timeit import default_timer as timer
def sum_iterable(nums):
data = 0 # you have to know the type in advance
for j, a in enumerate(nums):
data += a
return data
start = timer()
numbers = [1, 3, 5]
for i in range(10000):
res = sum_iterable(numbers)
print("result = {}".format(res))
end = timer()
print("{:.2f}ms".format((end - start) * 1000)) ~7ms from timeit import default_timer as timer
def sum_iterable(nums):
for j, a in enumerate(nums):
data = a if j == 0 else data + a
return data
start = timer()
numbers = [1, 3, 5]
for i in range(10000):
res = sum_iterable(numbers)
print("result = {}".format(res))
end = timer()
print("{:.2f}ms".format((end - start) * 1000)) ~8ms from timeit import default_timer as timer
def sum_iterable(nums):
for a in nums:
try:
data += a
except NameError:
data = a
return data
start = timer()
numbers = [1, 3, 5]
for i in range(10000):
res = sum_iterable(numbers)
print("result = {}".format(res))
end = timer()
print("{:.2f}ms".format((end - start) * 1000)) ~10.23ms Looking at above I'll move to initialising as numpy arrays can add to scalars without issue so initialising with zero shouldn't be a problem. |
good pick up, will change
Thinking about it more, not sure. Yes means more flexibility but you can get confused as the same view will give different answers depending on when it's called.
Yep
Yep
nice |
Alright let's have another go. I think the only thing I didn't implement from your suggestions is this.
Thinking about it more, not sure. Yes means more flexibility but you can get confused as the same view will give different answers depending on when it's called. Maybe that's the behaviour we want, how much do we want to 'lock the low level'/do views normally imply the data doesn't change? |
b5710c8
to
403f7f2
Compare
still having a lot of comments. mind if i make a pr to this one later? |
yep, no rush |
Well, the point of the views is that they yield the most up-to-date data for each read via |
Just saw, that I disallow adding child parameters once a parameter has been read from anyway, which makes sense for non-aggregating reads, so we just apply the same rationale here. |
@swillner should be good to go now |
nice! |
Pull request
Please confirm that this pull request has done the following:
CHANGELOG.rst
addedmake black
Adding to CHANGELOG.rst
Please add a single line in the changelog notes similar to one of the following: