Skip to content
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

Feature/parents lookup #372

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Feature/parents lookup #372

wants to merge 19 commits into from

Conversation

szvsw
Copy link
Collaborator

@szvsw szvsw commented Aug 9, 2022

So this implements the determination of parents at runtime using the new _CREATED_OBJECTS functionality.

The only thing that is a little off about it is that the wrapper objects don't have it implemented - e.g. MassRatios don't have parents (however the Material inside of a MassRatio will accurately find the Parents/ParentTemplates, same for a Material inside of a Layer finding the parent construction or a WeekSchedule inside of a WeekSchedulePart finding the parent YearSchedule).

Not a big deal IMO, since all UmiBase objects have it at least and it works properly.

There's a little bit of weirdness with the imports - rather than using from x import y we use import x as y in order to avoid circular imports. This is because we need to use the parent classes' _CREATED_OBJECTS in the children, so the children must import the parents, but... the parents are also already importing the children. A way to avoid this would be to go back to storing the _CREATED_OBJECTS in UmiBase, but using a key for each parent class. That way all the children don't need to import the parent classes...

@szvsw szvsw requested a review from samuelduchesne August 9, 2022 23:07
@szvsw
Copy link
Collaborator Author

szvsw commented Aug 9, 2022

Hmm will take a look at the test errors in the morning and resolve

@szvsw
Copy link
Collaborator Author

szvsw commented Aug 10, 2022

@samuelduchesne I'm not sure why this test is failing

tests/test_schedules.py::TestSchedule::test_plot

It passes fine on my machine and as far as I can tell I have not done anything that would affect that module. Everything else seems to be passing though.

@szvsw
Copy link
Collaborator Author

szvsw commented Aug 11, 2022

Okay, refactored this so that the PR is significantly simpler/less messy:

  • _CREATED_OBJECTS lives in UmiBase again, but stores a dict with class names as keys
  • UmiBase has a CREATED_OBJECTS property which uses the calling object's clas to get the list of created objects of that class
  • UmiBase has a single Parents and ParentTemplates property which works for all classes
  • Each class just has to define which classes may be a parent and what keys it may be found at
  • Retains original import structure and all the arrow type returns

Comes with two tests:

  • One which tests that basic functionality is working
  • One which tests that every component in a library which has unique_components(keep_orphaned=False) called on it has at least one parent template from that library.

The only test that is failing is the test_schedules::TestSchedule::test_plot, with error report below

Really scratching my head on this one, as it passes fine on my machine when running it in isolation at least. Any ideas, @samuelduchesne ?

____________________________ TestSchedule.test_plot ____________________________
[gw1] linux -- Python 3.8.13 /opt/hostedtoolcache/Python/3.8.13/x64/bin/python

self = <tests.test_schedules.TestSchedule object at 0x7f1b54660190>
schedules_in_necb_specific = <archetypal.schedule.Schedule object at 0x7f1b5186efa0>

    def test_plot(self, schedules_in_necb_specific):
>       schedules_in_necb_specific.plot(drawstyle="steps-post")

tests/test_schedules.py:57: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
archetypal/schedule.py:1389: in plot
    return self.series.plot(**kwargs)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_core.py:972: in __call__
    return plot_backend.plot(data, kind=kind, **kwargs)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py:71: in plot
    plot_obj.generate()
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:329: in generate
    self._make_plot()
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:1226: in _make_plot
    newlines = plotf(
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:1276: in _ts_plot
    lines = cls._plot(ax, data.index, data.values, style=style, **kwds)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:1256: in _plot
    lines = MPLPlot._plot(ax, x, y_values, style=style, **kwds)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/converter.py:91: in wrapper
    return func(*args, **kwargs)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:755: in _plot
    return ax.plot(*args, **kwds)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py:1502: in plot
    self.auto_scale_xyz(xs, ys, zs, had_data)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py:652: in auto_scale_xyz
    self.xy_dataLim.update_from_data_xy(
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/matplotlib/transforms.py:967: in update_from_data_xy
    path = Path(xy)
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/matplotlib/path.py:129: in __init__
    vertices = _to_unmasked_float_array(vertices)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

x = array([[Period('2018-01-01 00:00', 'H'), 18],
       [Period('2018-01-01 01:00', 'H'), 18],
       [Period('2018-01-01..., 'H'), 18],
       [Period('2018-12-31 22:00', 'H'), 18],
       [Period('2018-12-31 23:00', 'H'), 18]], dtype=object)

    def _to_unmasked_float_array(x):
        """
        Convert a sequence to a float array; if input was a masked array, masked
        values are converted to nans.
        """
        if hasattr(x, 'mask'):
            return np.ma.asarray(x, float).filled(np.nan)
        else:
>           return np.asarray(x, float)
E           TypeError: float() argument must be a string or a number, not 'Period'

/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/matplotlib/cbook/__init__.py:1298: TypeError

@samuelduchesne
Copy link
Owner

Will try to take a look tomorrow!

Copy link
Owner

@samuelduchesne samuelduchesne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a new function to traverse the hierarchy that is not parent_key_child_traversal?

return cls._CREATED_OBJECTS[object_class.__name__ + "s"]

@property
def CREATED_OBJECTS(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a private property instead of a public one? Trying to keep it clean as much as possible

return templates

@classmethod
def CREATED_OBJECTS(cls, object_class):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the same name as the property bellow?

@@ -437,6 +457,57 @@ def get_unique(self):

return obj

@property
def Parents(self):
""" Get the parents of an UmiBase Object"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid but can you remove the first space before each comment lol

@szvsw
Copy link
Collaborator Author

szvsw commented Aug 12, 2022

Not for this pr - the ParentTemplates method illustrates climbing up the tree from a given leaf though. I don't think it's of utility in a top down approach.

For me the idea for this PR is to just provide a simple and fairly minimal way to be able to determine ParentTemplates in the short term, so that we can then move to get feature/measures finished and merged.

The other aspect is that this can easily be upgraded if we build out the real-time edge tracking, since it essentially would just require updating the Parents property to use the data from the real-time graph rather than computing the parents - everything else should be able to stay the same though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants