diff --git a/Demand.py b/Demand.py index 6f718b4..b137b81 100644 --- a/Demand.py +++ b/Demand.py @@ -2,6 +2,8 @@ from prophet import Prophet from math import pi from math import cos, floor +import pkgutil +import csv class Demand: """ @@ -19,7 +21,7 @@ def __init__(self, demand: float = 20, var_per_day: float = 0.1 , var_per_season def set_forcast_periods(self, periods) -> None: self.__periods = periods - def set_data_csv(self, bind = None, init_date: str = "2017-01-01", delimiter: str = ";", column: str = "Total Ventes"): + def set_data_csv(self, bind = None, raw_data = None, init_date: str = "2017-01-01", delimiter: str = ";", column: str = "Total Ventes"): """ The method must get a dataset with at least 3 columns - month : int, @@ -28,8 +30,23 @@ def set_data_csv(self, bind = None, init_date: str = "2017-01-01", delimiter: st The method also use a forcast model from prophet to predict future demand. The periods can be set by set_forcast_periods. - """ - data = pd.read_csv(bind, delimiter = delimiter) + """ + if raw_data is not None : + data = pd.DataFrame(raw_data) + #set columns & index + header = data.iloc[0] + data = data[1:] + data.columns = header + data = data.reset_index(drop=True) + for col in data.columns.tolist(): + try: + # convert numeric values + data[col] = pd.to_numeric(data[col]) + except: + pass + else : + data = pd.read_csv(bind, delimiter = delimiter) + data["date"]= data ["month"].astype("str") + "-" + data ["year"].astype("str") data["datetime"] = pd.to_datetime(data["date"]) data_to_use = pd.DataFrame() @@ -46,6 +63,18 @@ def set_data_csv(self, bind = None, init_date: str = "2017-01-01", delimiter: st self.data_demand = data_to_use.append(fcst,ignore_index=True) return self.data_demand + + def set_data_to(self, dataset, delimiter: str=";"): + if dataset == "Toamasina": + #by defaut we keep it "Toamasina" + data = pkgutil.get_data('mixsimulator', '/data/RIToamasina/DIR-TOAMASINA_concat.csv') + data = csv.reader(data.decode('utf-8').splitlines(), delimiter = delimiter) + self.set_data_csv(raw_data=data) + else : + #by defaut we keep it "Toamasina" + data = pkgutil.get_data('mixsimulator', '/data/RIToamasina/DIR-TOAMASINA_concat.csv') + data = csv.reader(data.decode('utf-8').splitlines(), delimiter = delimiter) + self.set_data_csv(raw_data=data) def get_demand(self, t): self.data_demand.reset_index() diff --git a/MixSimulator.py b/MixSimulator.py index e4db9e8..f0580aa 100644 --- a/MixSimulator.py +++ b/MixSimulator.py @@ -129,7 +129,7 @@ def set_data_to(self, dataset, delimiter: str=";"): self.set_data_csv(raw_data=data) else : #by defaut we keep it "Toamasina" - data = pkgutil.get_data('mixsimulator', '/data/RIToamasina/dataset_RI_Toamasina.csv') + data = pkgutil.get_data('mixsimulator', '/data/RIToamasina/dataset_RI_Toamasina_v2.csv') data = csv.reader(data.decode('utf-8').splitlines(), delimiter = delimiter) self.set_data_csv(raw_data=data) diff --git a/test.py b/test.py index df25c83..b009e19 100644 --- a/test.py +++ b/test.py @@ -12,10 +12,17 @@ #Set data to use mix.set_data_csv("MixSimulator/data/RIToamasina/dataset_RI_Toamasina_v2.csv",delimiter=";") +#or for default dataset +#mix.set_data_to("Toamasina") + +#For variation limits dataset, there is not yet default dataset # mix.set_variation_csv("MixSimulator/data/RIToamasina/dataset_RI_Toamasina_variation_template.csv",delimiter=";") + demand = dm.Demand() data_demand = demand.set_data_csv("MixSimulator/data/RIToamasina/DIR-TOAMASINA_concat.csv", delimiter = ",") -# data_demand.to_csv("data_demand.csv") +#or for default dataset +#demand.set_data_to("Toamasina",delimiter=",") + mix.set_demand(demand) #Optimize the mix