taipy icon indicating copy to clipboard operation
taipy copied to clipboard

Added Filter Scenarios by time on CycleManager Class

Open Forchapeatl opened this issue 1 year ago • 6 comments

This PR continues https://github.com/Avaiga/taipy/pull/646

Forchapeatl avatar Jan 18 '24 11:01 Forchapeatl

Hello @trgiangdo , I have been guided by this comment it helped me a lot, thank you. What worries me is :

  1. If I use the _CycleManager._get_all() , I will get a list of cycles but I have n't found a means to extract scenarios form a cycle. Even this get_cycles_scenarios method is from get_scenarios calls the scenarioManager Class. I think it would be hard to avoid the local import of the scenario Manager. ( I may be wrong )

  2. I am not really sure of my logic. If there is a way to get the scenarios of a cycle, please help me with hints .

Forchapeatl avatar Jan 23 '24 18:01 Forchapeatl

You can get the scenarios by cycle using _ScenarioManager._get_all_by_cycle() method. But this should not be called inside the _CycleManager, it should be called at the API

trgiangdo avatar Jan 24 '24 07:01 trgiangdo

Example Usage

from taipy.config import Config, Frequency, Scope
import taipy as tp
import datetime as dt
from datetime import date

import pandas as pd


def filter_by_month(df, month):
    df['Date'] = pd.to_datetime(df['Date']) 
    df = df[df['Date'].dt.month == month]
    return df


historical_data_cfg = Config.configure_csv_data_node(id="historical_data",
                                                     default_path="time_series.csv",
                                                     scope=Scope.GLOBAL)
month_cfg =  Config.configure_data_node(id="month",
                                        scope=Scope.CYCLE)
month_values_cfg =  Config.configure_data_node(id="month_data",
                                               scope=Scope.CYCLE)


task_filter_cfg = Config.configure_task(id="filter_by_month",
                                        function=filter_by_month,
                                        input=[historical_data_cfg, month_cfg],
                                        output=month_values_cfg)


scenario_cfg = Config.configure_scenario(id="my_scenario",
                                         task_configs=[task_filter_cfg],
                                         frequency=Frequency.MONTHLY)


if __name__ == '__main__':
    tp.Core().run()

    scenario_1 = tp.create_scenario(scenario_cfg,
                                    creation_date=dt.datetime(2022,10,7),
                                    name="Scenario 2022/10/7")
    scenario_2 = tp.create_scenario(scenario_cfg,
                                    creation_date=dt.datetime(2022,10,5),
                                    name="Scenario 2022/10/5")

    scenario_1.month.write(10)
    scenario_1.submit()
    cycle = scenario_1.cycle
    b = dt.datetime(2022,10,12,0,0)

    print('<<<<<<<<<Scenarios>>>>>>>>>',tp.get_scenarios_by_time(b))

Forchapeatl avatar Jan 31 '24 19:01 Forchapeatl

Hello @Forchapeatl, the get_scenarios_by_time() method here is not what discussed in issue https://github.com/Avaiga/taipy/issues/413.

I believe you are on the right track and to ensure alignment with our objectives, I recommend revisiting the specifics of the issue and refining the get_scenarios() method accordingly

trgiangdo avatar Feb 07 '24 06:02 trgiangdo

@trgiangdo , you are right . This PR no-longer addresses issue #413. I am just holding on to this comment .

Forchapeatl avatar Feb 07 '24 11:02 Forchapeatl

Yes, I believe that comment is obsolete since we were in the discussion phase. Can you help us implement the API as discussed in the issue? I believe we can use a lot of the code you already implemented

trgiangdo avatar Feb 07 '24 12:02 trgiangdo

Closed due to inactivity

trgiangdo avatar Mar 13 '24 10:03 trgiangdo