AI4S2S / s2spy

A high-level python package integrating expert knowledge and artificial intelligence to boost (sub) seasonal forecasting
https://ai4s2s.readthedocs.io/
Apache License 2.0
20 stars 7 forks source link

Refactoring calendars to BaseCalendar class and subclasses #60

Closed BSchilperoort closed 2 years ago

BSchilperoort commented 2 years ago

This PR refactors the current calendars into a BaseCalendar class, where most methods are implemented, and subclasses like AdventCalendar or MonthlyCalendar.

As an example I implemented two extra calendars; MonthlyCalendar: where a user inputs an anchor month, and a freq in the form 1M WeeklyCalendar: where a user inputs an anchor week (as in, week of year), and a freq in the form 1W

Additionally, the implementation of resample has been moved to a separate module, as s2spy.time had gotten too large. Resample now also works as discussed in #50


List of changes:


Demo of functionality

>>> from s2spy.time import MonthlyCalendar
>>> cal = MonthlyCalendar(anchor='Dec', freq='6M')
>>> cal
MonthlyCalendar(month=12, freq=6M, n_targets=1, max_lag=None)

>>> cal.map_years(2020, 2021)
>>> cal.show()
i_interval             (target) 0                     1
anchor_year                                            
2021         (2021 Jun, 2021 Dec]  (2020 Dec, 2021 Jun]
2020         (2020 Jun, 2020 Dec]  (2019 Dec, 2020 Jun]

>>> cal.get_intervals()
i_interval                          0                         1
anchor_year                                                    
2021         (2021-06-30, 2021-12-31]  (2020-12-31, 2021-06-30]
2020         (2020-06-30, 2020-12-31]  (2019-12-31, 2020-06-30]

>>> from s2spy.time import resample
>>> ds_resampled = resample(cal, ds) # Resampled to the intervals shown above

Still to do:

Also:


This PR: Closes #33 Closes #41 Closes #50

review-notebook-app[bot] commented 2 years ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

sonarcloud[bot] commented 2 years ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

92.8% 92.8% Coverage
0.0% 0.0% Duplication

Peter9192 commented 2 years ago

Thanks for addressing. Indeed, I think it's fine to merge this and restructure in another PR. One more comment that I forgot to write before: with the addition of a weekly and monthly calendar, I think the adventcalendar feels a bit awkward now. Shall we consider changing it to daily calendar? And/or shouldn't they be called NDayCalendar, NWeekCalendar, NMonthCalendar?

BSchilperoort commented 2 years ago

Hm. An alternative could also to have the users interface with a single calendar object, e.g. AdventCalendar (or CountdownCalendar or any other suitable name). Where the following would all be valid inputs;

daycalendar = AdventCalendar(anchor=(31, 12), freq='4d')
weekcalendar = AdventCalendar(anchor=40, freq='2W')
monthcalendar = AdventCalendar(anchor='Jan', freq='1M')