diff --git a/examples/fedopt_example/client_data.py b/examples/fedopt_example/client_data.py index bae9a2eff..e0117dba7 100644 --- a/examples/fedopt_example/client_data.py +++ b/examples/fedopt_example/client_data.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from pathlib import Path from typing import Dict, List, Optional, Tuple @@ -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))) @@ -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()} @@ -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: @@ -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) diff --git a/examples/fedopt_example/metrics.py b/examples/fedopt_example/metrics.py index 52e5a8e6c..b0a37905c 100644 --- a/examples/fedopt_example/metrics.py +++ b/examples/fedopt_example/metrics.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from logging import INFO from typing import Dict, List, Optional @@ -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] @@ -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 diff --git a/fl4health/feature_alignment/string_columns_transformer.py b/fl4health/feature_alignment/string_columns_transformer.py index 8237cfe28..74bc3065f 100644 --- a/fl4health/feature_alignment/string_columns_transformer.py +++ b/fl4health/feature_alignment/string_columns_transformer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Optional import pandas as pd @@ -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 @@ -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 diff --git a/fl4health/feature_alignment/tab_features_info_encoder.py b/fl4health/feature_alignment/tab_features_info_encoder.py index 07c061704..04a98aae4 100644 --- a/fl4health/feature_alignment/tab_features_info_encoder.py +++ b/fl4health/feature_alignment/tab_features_info_encoder.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from typing import Dict, List, Optional, Union @@ -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 @@ -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"])], diff --git a/fl4health/feature_alignment/tabular_feature.py b/fl4health/feature_alignment/tabular_feature.py index ee4962409..797b02cd5 100644 --- a/fl4health/feature_alignment/tabular_feature.py +++ b/fl4health/feature_alignment/tabular_feature.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from typing import Optional, Union @@ -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"]),