Skip to content

Commit

Permalink
unit test debugging 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaminietta committed Oct 14, 2024
1 parent 993a211 commit c8180db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mcda/models/ProMCDA.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, input_matrix: pd.DataFrame, polarity: Tuple[str, ...], sensit
df_normalized = promcda.normalize()
df_aggregated = promcda.aggregate()
"""
self.logger = logging.getLogger("ProMCDA")
self.input_matrix = input_matrix
self.polarity = polarity
self.sensitivity = sensitivity
Expand All @@ -51,7 +52,6 @@ def __init__(self, input_matrix: pd.DataFrame, polarity: Tuple[str, ...], sensit
self.aggregated_matrix = None
self.ranked_matrix = None

self.logger = logging.getLogger("ProMCDA")

def validate_inputs(self) -> Tuple[int, int, list, Union[list, List[list], dict], dict]:
"""
Expand Down
32 changes: 27 additions & 5 deletions mcda/utils/utils_for_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,34 @@ def save_config(config: dict, folder_path: str, filename: str):

try:
with open(full_output_path, 'w') as fp:
json.dump(config, fp)
serializable_config = _prepare_config_for_json(config)
json.dump(serializable_config, fp)
except IOError as e:
logging.error(f"Error while dumping the configuration into a JSON file: {e}")


def _convert_dataframe_to_serializable(df):
"""
Convert a pandas DataFrame into a serializable dictionary format.
"""
return {
'data': df.values.tolist(), # Convert data to list of lists
'columns': df.columns.tolist(), # Convert column names to list
'index': df.index.tolist() # Convert index labels to list
}


def _prepare_config_for_json(config):
"""
Prepare the config dictionary by converting non-serializable objects into serializable ones.
"""
config_copy = config.copy() # Create a copy to avoid modifying the original config
if isinstance(config_copy['input_matrix'], pd.DataFrame):
# Convert DataFrame to serializable format
config_copy['input_matrix'] = _convert_dataframe_to_serializable(config_copy['input_matrix'])
return config_copy


def check_path_exists(path: str):
"""
Check if a directory path exists, and create it if it doesn't.
Expand Down Expand Up @@ -612,7 +635,7 @@ def run_mcda_without_indicator_uncertainty(extracted_values: dict, is_robustness
# Extract relevant values
input_matrix = extracted_values["input_matrix"]
alternatives_column_name = input_matrix.columns[0]
input_matrix = input_matrix.set_index(alternatives_column_name)
# input_matrix = input_matrix.set_index(alternatives_column_name)
index_column_name = input_matrix.index.name
index_column_values = input_matrix.index.tolist()
input_matrix_no_alternatives = check_input_matrix(input_matrix)
Expand All @@ -621,7 +644,7 @@ def run_mcda_without_indicator_uncertainty(extracted_values: dict, is_robustness
f_norm = extracted_values["normalization"]
f_agg = extracted_values["aggregation"]

mcda_no_uncert\
mcda_no_uncert \
= MCDAWithoutRobustness(extracted_values, input_matrix_no_alternatives)
logger.info("Start ProMCDA without robustness of the indicators")

Expand Down Expand Up @@ -670,8 +693,7 @@ def run_mcda_without_indicator_uncertainty(extracted_values: dict, is_robustness


def run_mcda_with_indicator_uncertainty(extracted_values: dict, weights: Union[List[str], List[pd.DataFrame],
dict, None]) -> None:

dict, None]) -> None:
"""
Runs ProMCDA with uncertainty on the indicators, i.e. with a robustness analysis.
Expand Down

0 comments on commit c8180db

Please sign in to comment.