Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving relevant files to use the future annotations import for 'in-class' return values #286

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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