Skip to content

Commit

Permalink
Merge branch 'main' into add_more_reports
Browse files Browse the repository at this point in the history
  • Loading branch information
scarere authored Nov 18, 2024
2 parents b51bcf7 + 416e1c6 commit f95cf20
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
10 changes: 6 additions & 4 deletions examples/fedopt_example/client_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from pathlib import Path
from typing import Dict, List, Optional, Tuple
Expand All @@ -20,7 +22,7 @@ def __init__(self, classes: List[str], label_to_class: Dict[int, str], class_to_
self.class_to_label = class_to_label

@staticmethod
def encoder_from_dataframe(df: pd.DataFrame, class_column: str) -> "LabelEncoder":
def encoder_from_dataframe(df: pd.DataFrame, class_column: str) -> LabelEncoder:
categories = df[class_column].astype("category")
categories_str = [str(category) for category in categories.to_list()]
label_to_class = dict(set(zip(categories.cat.codes, categories_str)))
Expand All @@ -29,7 +31,7 @@ def encoder_from_dataframe(df: pd.DataFrame, class_column: str) -> "LabelEncoder
return LabelEncoder(classes, label_to_class, class_to_label)

@staticmethod
def from_json(json_str: str) -> "LabelEncoder":
def from_json(json_str: str) -> LabelEncoder:
attributes = json.loads(json_str)
# need to cast string keys to int
label_to_class = {int(label): category for label, category in json.loads(attributes["label_to_class"]).items()}
Expand Down Expand Up @@ -59,7 +61,7 @@ def __init__(self, vocabulary_dict: Optional[Dict[str, int]], train_set: Optiona
elif train_set is not None:
self._create_vocabulary(train_set)
else:
raise ValueError("Must provide either precumputed dictionary or training set to create vocabulary")
raise ValueError("Must provide either precomputed dictionary or training set to create vocabulary")
self.vocabulary_size = len(self.word2index)

def _create_vocabulary(self, train_set: List[List[str]]) -> None:
Expand Down Expand Up @@ -95,7 +97,7 @@ def to_json(self) -> str:
return json.dumps(self.word2index)

@staticmethod
def from_json(json_str: str) -> "Vocabulary":
def from_json(json_str: str) -> Vocabulary:
return Vocabulary(json.loads(json_str), None)


Expand Down
6 changes: 4 additions & 2 deletions examples/fedopt_example/metrics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from logging import INFO
from typing import Dict, List, Optional
Expand Down Expand Up @@ -43,7 +45,7 @@ def summarize(self) -> Dict[str, float]:
}

@staticmethod
def from_results_dict(class_name: str, stats_string: str) -> "Outcome":
def from_results_dict(class_name: str, stats_string: str) -> Outcome:
outcome = Outcome(class_name)
stats = json.loads(stats_string)
outcome.true_positive = stats[0]
Expand All @@ -52,7 +54,7 @@ def from_results_dict(class_name: str, stats_string: str) -> "Outcome":
return outcome

@staticmethod
def merge_outcomes(outcome_1: "Outcome", outcome_2: "Outcome") -> "Outcome":
def merge_outcomes(outcome_1: "Outcome", outcome_2: "Outcome") -> Outcome:
assert outcome_1.class_name == outcome_2.class_name
outcome_1.true_positive += outcome_2.true_positive
outcome_1.false_negative += outcome_2.false_negative
Expand Down
6 changes: 4 additions & 2 deletions fl4health/feature_alignment/string_columns_transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Optional

import pandas as pd
Expand All @@ -15,7 +17,7 @@ class TextMulticolumnTransformer(BaseEstimator, TransformerMixin):
def __init__(self, transformer: TextFeatureTransformer):
self.transformer = transformer

def fit(self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None) -> "TextMulticolumnTransformer":
def fit(self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None) -> TextMulticolumnTransformer:
joined_X = X.apply(lambda x: " ".join(x), axis=1)
self.transformer.fit(joined_X)
return self
Expand All @@ -34,7 +36,7 @@ class TextColumnTransformer(BaseEstimator, TransformerMixin):
def __init__(self, transformer: TextFeatureTransformer):
self.transformer = transformer

def fit(self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None) -> "TextColumnTransformer":
def fit(self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None) -> TextColumnTransformer:
assert isinstance(X, pd.DataFrame) and X.shape[1] == 1
self.transformer.fit(X[X.columns[0]])
return self
Expand Down
6 changes: 4 additions & 2 deletions fl4health/feature_alignment/tab_features_info_encoder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from typing import Dict, List, Optional, Union

Expand Down Expand Up @@ -87,7 +89,7 @@ def encoder_from_dataframe(
id_column: str,
target_columns: Union[str, List[str]],
fill_values: Optional[Dict[str, Scalar]] = None,
) -> "TabularFeaturesInfoEncoder":
) -> TabularFeaturesInfoEncoder:
features_list = sorted(df.columns.values.tolist())
features_list.remove(id_column)
# Leverage cyclops to perform type inference
Expand Down Expand Up @@ -119,7 +121,7 @@ def to_json(self) -> str:
)

@staticmethod
def from_json(json_str: str) -> "TabularFeaturesInfoEncoder":
def from_json(json_str: str) -> TabularFeaturesInfoEncoder:
attributes = json.loads(json_str)
return TabularFeaturesInfoEncoder(
[TabularFeature.from_json(tab_str) for tab_str in json.loads(attributes["tabular_features"])],
Expand Down
4 changes: 3 additions & 1 deletion fl4health/feature_alignment/tabular_feature.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from typing import Optional, Union

Expand Down Expand Up @@ -70,7 +72,7 @@ def to_json(self) -> str:
)

@staticmethod
def from_json(json_str: str) -> "TabularFeature":
def from_json(json_str: str) -> TabularFeature:
attributes = json.loads(json_str)
return TabularFeature(
json.loads(attributes["feature_name"]),
Expand Down

0 comments on commit f95cf20

Please sign in to comment.