antoinecarme / pyaf

PyAF is an Open Source Python library for Automatic Time Series Forecasting built on top of popular pydata modules.
BSD 3-Clause "New" or "Revised" License
459 stars 73 forks source link

When creating the Grouped Hierarchy, the group order specified is not preserved #56

Closed tomaszb closed 7 years ago

tomaszb commented 7 years ago

Currently to recreate the hierarchy from the lHierarchy object when running:

lEngine = hautof.cHierarchicalForecastEngine() lSignalHierarchy = lEngine.plot_Hierarchy()

the groups are sorted in reverse alphabetical order and ignore the actual 'GroupOrder' list of the lHierarchy object. This is because in file pyaf/TS/Signal_Grouping.py it is using the sorted function (line 54):

array1 = [ lGroups[k] for k in sorted(self.mHierarchy['GroupOrder'] , reverse=True) ];

The built in sorted function sorts by alphanumeric order by default.

antoinecarme commented 7 years ago

Hi @tomaszb

Nice to see you again. Can you provide some examples? This seems to be an early step (hierarchy creation).

I will look at this later. I don't use the grouping so much. Can you elaborate on your use case ?

In the meantime, if you have a solution or a work-around, you can send me a pull request.

antoinecarme commented 7 years ago

@tomaszb

If you don't feel comfortable with sharing info on github, you can use my email (my email is in the header of pyaf/TS/Signal_Grouping.py )

tomaszb commented 7 years ago

Hi @antoinecarme

We can recreate the issue from your document by renaming the Grouping structure like so:

# Grouping definition
lGroups = {}
lGroups["ZCountry"] = Countries
lGroups["YVariant"] = Variants
lGroups["XWine"] = Wines

lHierarchy = {};
lHierarchy['Levels'] = None;
lHierarchy['Data'] = None;
lHierarchy['Groups']= lGroups;
# the most important !!!!
lHierarchy['GroupOrder']= ["XWine", "YVariant", "ZCountry"]; # group by Wine first, then by variant, etc
lHierarchy['Type'] = "Grouped";

Now when running:

import pyaf.HierarchicalForecastEngine as hautof
lEngine = hautof.cHierarchicalForecastEngine()

lSignalHierarchy = lEngine.plot_Hierarchy(French_Wine_Export_in_Euros_DF , "Month", "Signal", 1, 
                                          lHierarchy, None);

lSignalHierarchy.mStructure

The mStructure object will be empty.

antoinecarme commented 7 years ago

Hi @tomaszb

Can you please test the fix I just pushed and give me your feedback ?