Skip to content

Commit 2a6e421

Browse files
committed
change static variable to getter and setter
1 parent 5d65358 commit 2a6e421

File tree

9 files changed

+340
-59
lines changed

9 files changed

+340
-59
lines changed

pipeline_lib/core/data_container.py

Lines changed: 314 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import pickle
88
import sys
9-
from typing import Optional, Union
9+
from typing import Optional, Union, Any
1010

1111
import yaml
1212

@@ -21,23 +21,6 @@ class DataContainer:
2121
A dictionary to store data items.
2222
"""
2323

24-
RAW = "raw"
25-
CLEAN = "clean"
26-
TRAIN = "train"
27-
VALIDATION = "validation"
28-
TEST = "test"
29-
MODEL = "model"
30-
MODEL_INPUT = "model_input"
31-
MODEL_OUTPUT = "model_output"
32-
METRICS = "metrics"
33-
PREDICTIONS = "predictions"
34-
EXPLAINER = "explainer"
35-
TUNING_PARAMS = "tuning_params"
36-
TARGET = "target"
37-
IMPORTANCE = "importance"
38-
DROP_COLUMNS = "drop_columns"
39-
FEATURES = "features"
40-
4124
def __init__(self, initial_data: Optional[dict] = None):
4225
"""
4326
Initialize the DataContainer with an empty dictionary or provided data.
@@ -296,6 +279,319 @@ def from_yaml(cls, file_path: str) -> DataContainer:
296279
# The loaded data is used as the initial data for the DataContainer instance
297280
return cls(initial_data=data)
298281

282+
@property
283+
def clean(self) -> Any:
284+
"""
285+
Get the clean data from the DataContainer.
286+
287+
Returns
288+
-------
289+
Any
290+
The clean data stored in the DataContainer.
291+
"""
292+
return self["clean"]
293+
294+
@clean.setter
295+
def clean(self, value: Any):
296+
"""
297+
Set the clean data in the DataContainer.
298+
299+
Parameters
300+
----------
301+
value
302+
The clean data to be stored in the DataContainer.
303+
"""
304+
self["clean"] = value
305+
306+
# create the same for raw
307+
@property
308+
def raw(self) -> Any:
309+
"""
310+
Get the raw data from the DataContainer.
311+
312+
Returns
313+
-------
314+
Any
315+
The raw data stored in the DataContainer.
316+
"""
317+
return self["raw"]
318+
319+
@raw.setter
320+
def raw(self, value: Any):
321+
"""
322+
Set the raw data in the DataContainer.
323+
324+
Parameters
325+
----------
326+
value
327+
The raw data to be stored in the DataContainer.
328+
"""
329+
self["raw"] = value
330+
331+
@property
332+
def train(self) -> Any:
333+
"""
334+
Get the train data from the DataContainer.
335+
336+
Returns
337+
-------
338+
Any
339+
The train data stored in the DataContainer.
340+
"""
341+
return self["train"]
342+
343+
@train.setter
344+
def train(self, value: Any):
345+
"""
346+
Set the train data in the DataContainer.
347+
348+
Parameters
349+
----------
350+
value
351+
The train data to be stored in the DataContainer.
352+
"""
353+
self["train"] = value
354+
355+
@property
356+
def validation(self) -> Any:
357+
"""
358+
Get the validation data from the DataContainer.
359+
360+
Returns
361+
-------
362+
Any
363+
The validation data stored in the DataContainer.
364+
"""
365+
return self["validation"]
366+
367+
@validation.setter
368+
def validation(self, value: Any):
369+
"""
370+
Set the validation data in the DataContainer.
371+
372+
Parameters
373+
----------
374+
value
375+
The validation data to be stored in the DataContainer.
376+
"""
377+
self["validation"] = value
378+
379+
@property
380+
def model(self) -> Any:
381+
"""
382+
Get the model from the DataContainer.
383+
384+
Returns
385+
-------
386+
Any
387+
The model stored in the DataContainer.
388+
"""
389+
return self["model"]
390+
391+
@model.setter
392+
def model(self, value: Any):
393+
"""
394+
Set the model in the DataContainer.
395+
396+
Parameters
397+
----------
398+
value
399+
The model to be stored in the DataContainer.
400+
"""
401+
self["model"] = value
402+
403+
@property
404+
def model_input(self) -> Any:
405+
"""
406+
Get the model input from the DataContainer.
407+
408+
Returns
409+
-------
410+
Any
411+
The model input stored in the DataContainer.
412+
"""
413+
return self["model_input"]
414+
415+
@model_input.setter
416+
def model_input(self, value: Any):
417+
"""
418+
Set the model input in the DataContainer.
419+
420+
Parameters
421+
----------
422+
value
423+
The model input to be stored in the DataContainer.
424+
"""
425+
self["model_input"] = value
426+
427+
@property
428+
def model_output(self) -> Any:
429+
"""
430+
Get the model output from the DataContainer.
431+
432+
Returns
433+
-------
434+
Any
435+
The model output stored in the DataContainer.
436+
"""
437+
return self["model_output"]
438+
439+
@model_output.setter
440+
def model_output(self, value: Any):
441+
"""
442+
Set the model output in the DataContainer.
443+
444+
Parameters
445+
----------
446+
value
447+
The model output to be stored in the DataContainer.
448+
"""
449+
self["model_output"] = value
450+
451+
@property
452+
def metrics(self) -> Any:
453+
"""
454+
Get the metrics from the DataContainer.
455+
456+
Returns
457+
-------
458+
Any
459+
The metrics stored in the DataContainer.
460+
"""
461+
return self["metrics"]
462+
463+
@metrics.setter
464+
def metrics(self, value: Any):
465+
"""
466+
Set the metrics in the DataContainer.
467+
468+
Parameters
469+
----------
470+
value
471+
The metrics to be stored in the DataContainer.
472+
"""
473+
self["metrics"] = value
474+
475+
@property
476+
def predictions(self) -> Any:
477+
"""
478+
Get the predictions from the DataContainer.
479+
480+
Returns
481+
-------
482+
Any
483+
The predictions stored in the DataContainer.
484+
"""
485+
return self["predictions"]
486+
487+
@predictions.setter
488+
def predictions(self, value: Any):
489+
"""
490+
Set the predictions in the DataContainer.
491+
492+
Parameters
493+
----------
494+
value
495+
The predictions to be stored in the DataContainer.
496+
"""
497+
self["predictions"] = value
498+
499+
@property
500+
def explainer(self) -> Any:
501+
"""
502+
Get the explainer from the DataContainer.
503+
504+
Returns
505+
-------
506+
Any
507+
The explainer stored in the DataContainer.
508+
"""
509+
return self["explainer"]
510+
511+
@explainer.setter
512+
def explainer(self, value: Any):
513+
"""
514+
Set the explainer in the DataContainer.
515+
516+
Parameters
517+
----------
518+
value
519+
The explainer to be stored in the DataContainer.
520+
"""
521+
self["explainer"] = value
522+
523+
@property
524+
def tuning_params(self) -> Any:
525+
"""
526+
Get the tuning parameters from the DataContainer.
527+
528+
Returns
529+
-------
530+
Any
531+
The tuning parameters stored in the DataContainer.
532+
"""
533+
return self["tuning_params"]
534+
535+
@tuning_params.setter
536+
def tuning_params(self, value: Any):
537+
"""
538+
Set the tuning parameters in the DataContainer.
539+
540+
Parameters
541+
----------
542+
value
543+
The tuning parameters to be stored in the DataContainer.
544+
"""
545+
self["tuning_params"] = value
546+
547+
@property
548+
def target(self) -> Any:
549+
"""
550+
Get the target from the DataContainer.
551+
552+
Returns
553+
-------
554+
Any
555+
The target stored in the DataContainer.
556+
"""
557+
return self["target"]
558+
559+
@target.setter
560+
def target(self, value: Any):
561+
"""
562+
Set the target in the DataContainer.
563+
564+
Parameters
565+
----------
566+
value
567+
The target to be stored in the DataContainer.
568+
"""
569+
self["target"] = value
570+
571+
@property
572+
def features(self) -> Any:
573+
"""
574+
Get the features from the DataContainer.
575+
576+
Returns
577+
-------
578+
Any
579+
The features stored in the DataContainer.
580+
"""
581+
return self["features"]
582+
583+
@features.setter
584+
def features(self, value: Any):
585+
"""
586+
Set the features in the DataContainer.
587+
588+
Parameters
589+
----------
590+
value
591+
The features to be stored in the DataContainer.
592+
"""
593+
self["features"] = value
594+
299595
def __eq__(self, other) -> bool:
300596
"""
301597
Compare this DataContainer with another for equality.

pipeline_lib/core/steps/calculate_features.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def execute(self, data: DataContainer) -> DataContainer:
7070
"""Execute the step."""
7171
self.logger.info("Calculating features")
7272

73-
df = data[DataContainer.CLEAN]
73+
df = data.clean
7474
created_features = []
7575

7676
if self.datetime_columns:
@@ -97,6 +97,6 @@ def execute(self, data: DataContainer) -> DataContainer:
9797

9898
self.logger.info(f"Created new features: {created_features}")
9999

100-
data[DataContainer.FEATURES] = df
100+
data.features = df
101101

102102
return data

pipeline_lib/core/steps/calculate_metrics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ def __init__(self) -> None:
1515

1616
def execute(self, data: DataContainer) -> DataContainer:
1717
self.logger.debug("Starting metric calculation")
18-
model_output = data[DataContainer.MODEL_OUTPUT]
18+
model_output = data.model_output
1919

20-
target_column_name = data.get(DataContainer.TARGET)
20+
target_column_name = data.target
2121

2222
if target_column_name is None:
2323
raise ValueError("Target column not found on any configuration.")
2424

2525
true_values = model_output[target_column_name]
26-
predictions = model_output[DataContainer.PREDICTIONS]
26+
predictions = model_output["predictions"]
2727

2828
mae = mean_absolute_error(true_values, predictions)
2929
rmse = np.sqrt(mean_squared_error(true_values, predictions))
3030

3131
results = {"MAE": str(mae), "RMSE": str(rmse)}
3232
self.logger.info(results)
33-
data[DataContainer.METRICS] = results
33+
data.metrics = results
3434
return data

0 commit comments

Comments
 (0)