Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.py 5.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  1. import os
  2. import mlflow
  3. from dotenv import load_dotenv
  4. from tqdm import tqdm
  5. load_dotenv("./.env")
  6. import logging
  7. from logger import configure_logger
  8. from tools.dataset import get_dataset
  9. from tools.models import get_model_trainer
  10. from tools.preprocess import timeseries_split
  11. logger = logging.getLogger("SCM-4.0")
  12. # is_extra_feature_enabled = False
  13. ablation = -1 # 1024 * 5 # set to -1 to select entire dataset, otherwise an integer number
  14. unique_mlops_exp_prefix = "macebm"
  15. def calculate_time_period(series):
  16. difference_txt = ""
  17. # Get the difference between the maximum and minimum dates
  18. date_difference = series.max() - series.min()
  19. # # Extract individual components
  20. # years = date_difference.days // 365
  21. # months = (date_difference.days % 365) // 30
  22. # days = (date_difference.days % 365) % 30
  23. # hours = date_difference.seconds // 3600
  24. # minutes = (date_difference.seconds % 3600) // 60
  25. # seconds = date_difference.seconds % 60
  26. # for unit, val in zip(("Y", "M", "D", "H", "m", "s"), (years, months, days, hours, minutes, seconds)):
  27. # if val > 0 or unit in ("H", "m", "s"):
  28. # difference_txt = f"{difference_txt} {val}{unit}"
  29. return date_difference.seconds, str(date_difference) # , difference_txt.strip()
  30. def experiment(model_name, dataset_name, is_extra_feature_enabled, extra_feat_txt="", ablation_txt=""):
  31. meta_info = "%s%s" % (extra_feat_txt, ablation_txt)
  32. logger.info(f"{dataset_name}:{model_name}:{meta_info} Preparing model and datasets")
  33. df, target, timeseries_col, dataset_name = get_dataset(dataset_name, ablation_limit=ablation,
  34. is_extra_feature_enabled=is_extra_feature_enabled)
  35. logger.info(f"{dataset_name}:DF INFO:\n{df.info()}")
  36. model_trainer = get_model_trainer(model_name)
  37. x_train, x_test, y_train, y_test = timeseries_split(df, target, train_size=.8)
  38. total_diff = calculate_time_period(df[timeseries_col])
  39. train_diff = calculate_time_period(x_train[timeseries_col])
  40. test_diff = calculate_time_period(x_test[timeseries_col])
  41. mlflow.log_params(dict(
  42. train_size=len(x_train),
  43. test_size=len(x_test),
  44. feature_count=len(x_train.columns),
  45. time_period=total_diff[0],
  46. time_period_sec_train=train_diff[0],
  47. time_period_sec_test=test_diff[0],
  48. features=list(x_train.columns.values),
  49. time_period_sec_txt=total_diff[1],
  50. time_period_train_txt=train_diff[1],
  51. time_period_test_txt=test_diff[1],
  52. ))
  53. logger.info(f"{dataset_name}:{model_name}: Creating timeseries features for plotting")
  54. if timeseries_col:
  55. x_train_timeseries = x_train[timeseries_col]
  56. x_test_timeseries = x_test[timeseries_col]
  57. logger.info(f"{dataset_name}:{model_name}: Dropping datetime before training")
  58. x_train.drop(columns=[timeseries_col], inplace=True)
  59. x_test.drop(columns=[timeseries_col], inplace=True)
  60. else:
  61. x_train_timeseries = list(range(len(x_train)))
  62. x_test_timeseries = list(range(len(x_test)))
  63. logger.info(f"{dataset_name}:{model_name}: Start training... WITH DATASIZE: {x_train.shape}")
  64. model, feature_importance = model_trainer.fit(x_train, y_train, x_test, y_test)
  65. if model_name == "explainable_boosting":
  66. feature_importance.write_html(f"output/{dataset_name}-{model_name}{meta_info}.html")
  67. feature_importance = "are saved as plotly html."
  68. logger.info(f"{model_name}:{dataset_name}: feature_importance {feature_importance}")
  69. logger.info(f"{dataset_name}:{model_name}: Start evaluation... WITH DATASIZE: {x_test.shape}")
  70. model_trainer.evaluate(model_name, dataset_name, f"train", model, x_train, y_train, x_train_timeseries,
  71. meta_info=meta_info)
  72. model_trainer.evaluate(model_name, dataset_name, f"test", model, x_test, y_test, x_test_timeseries,
  73. meta_info=meta_info)
  74. logger.info(f"{dataset_name}:{model_name}:{meta_info} Done")
  75. return True
  76. def main():
  77. mlflow.set_tracking_uri(os.environ["MLFLOW_TRACKING_URI"])
  78. experiments = []
  79. # global is_extra_feature_enabled
  80. for dataset_name in ["online_retail", "online_retail_2", "product_demand", "livestock_meat_import",
  81. "future_sales", ]:
  82. for model_name in ["explainable_boosting"]:
  83. experiments.append((model_name, dataset_name))
  84. print("")
  85. for is_extra_feature_enabled in [False, True]:
  86. for model_name, dataset_name in tqdm(experiments):
  87. extra_feat_txt = "-exf" if is_extra_feature_enabled else ""
  88. ablation_txt = f"-abl{ablation}" if ablation > 0 else ""
  89. exp_name = f"{unique_mlops_exp_prefix}-{dataset_name}{extra_feat_txt}{ablation_txt}"
  90. experiment_tracking_file = f"output/tracking/{dataset_name}-{model_name}{extra_feat_txt}{ablation_txt}"
  91. if not os.path.exists(experiment_tracking_file):
  92. logger.info("%s [Executing...]" % experiment_tracking_file)
  93. mlflow.set_experiment(experiment_name=exp_name)
  94. with mlflow.start_run(description=exp_name):
  95. mlflow.log_params(dict(
  96. model=model_name,
  97. dataset=dataset_name,
  98. extra_feat=is_extra_feature_enabled,
  99. ablation=ablation,
  100. ))
  101. experiment(model_name, dataset_name, is_extra_feature_enabled, extra_feat_txt, ablation_txt)
  102. mlflow.end_run()
  103. open(experiment_tracking_file, "w")
  104. else:
  105. logger.info("%s [DONE...]" % experiment_tracking_file)
  106. logger.info("All experiments are done")
  107. if __name__ == '__main__':
  108. configure_logger(logging.DEBUG) # logger
  109. # dataset_name = "product_demand"
  110. # df, target, timeseries_col, dataset_name = get_dataset(dataset_name, ablation_limit=-1,
  111. # is_extra_feature_enabled=False,label_encoding=False)
  112. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...