Skip to content

Commit

Permalink
Fixes for flake8 linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rajithkrishnegowda committed Nov 4, 2024
1 parent b8284c9 commit 20ee2c9
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 58 deletions.
2 changes: 2 additions & 0 deletions openfl-workspace/torch_template/src/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from openfl.federated import PyTorchDataLoader


class TemplateDataLoader(PyTorchDataLoader):
"""Template dataloader for PyTorch.
This class should be used as a template to create a custom DataLoader for your specific dataset.
Expand Down Expand Up @@ -40,6 +41,7 @@ def __init__(self, data_path, batch_size, **kwargs):
self.X_valid = X_valid
self.y_valid = y_valid


def load_dataset(data_path, **kwargs):
"""
Load your dataset here.
Expand Down
2 changes: 2 additions & 0 deletions openfl-workspace/torch_template/src/taskrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from openfl.federated import PyTorchTaskRunner
from openfl.utilities import Metric


class TemplateTaskRunner(PyTorchTaskRunner):
"""Template Task Runner for PyTorch.
Expand Down Expand Up @@ -102,6 +103,7 @@ def validate_(
accuracy = None # Placeholder for accuracy calculation.
return Metric(name="accuracy", value=np.array(accuracy))


raise NotImplementedError(
"Use <workspace>/src/taskrunner.py template to create a custom Task Runner "
"with your model definition and training/validation logic. Then remove this line."
Expand Down
17 changes: 15 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
[flake8]
ignore =
# Conflicts with black
E203
E203
# Line break occurred before a binary operator. Update by W504 Line
W503
# Allow "import torch.nn.functional as F"
N812
# Line length, handled separately by max-line-length
E501
# too many leading '#' for block comments
E266


per-file-ignores =
# Unused imports in __init__.py are OK
**/__init__.py:F401

exclude =
*_pb2*,
.git,
__pycache__,
build,
dist,
.venv

max-line-length = 100
max-line-length = 120

copyright-check = True

# Enable specific checks or plugins
select = B,C,E,F,W,T4,B9
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start(self):
)
self.collaborators = self.runtime.collaborators

validate_agg_private_attr(self, "start", aggr = ["test_loader_agg"], collabs =["train_loader", "test_loader"])
validate_agg_private_attr(self, "start", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"])

self.exclude_agg_to_agg = 10
self.include_agg_to_agg = 100
Expand All @@ -49,7 +49,7 @@ def aggregator_step(self):
Testing whether Agg private attributes are accessible in next agg step.
Collab private attributes should not be accessible here
"""
validate_agg_private_attr(self, "aggregator_step", aggr = ["test_loader_agg"], collabs =["train_loader", "test_loader"])
validate_agg_private_attr(self, "aggregator_step", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"])

self.include_agg_to_collab = 42
self.exclude_agg_to_collab = 40
Expand All @@ -66,7 +66,7 @@ def collaborator_step_a(self):
Aggregator private attributes should not be accessible here
"""
validate_collab_private_attrs(
self, "collaborator_step_a", aggr = ["test_loader_agg"], collabs =["train_loader", "test_loader"]
self, "collaborator_step_a", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"]
)

self.exclude_collab_to_collab = 2
Expand All @@ -81,7 +81,7 @@ def collaborator_step_b(self):
"""

validate_collab_private_attrs(
self, "collaborator_step_b", aggr = ["test_loader_agg"], collabs =["train_loader", "test_loader"]
self, "collaborator_step_b", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"]
)
self.exclude_collab_to_agg = 10
self.include_collab_to_agg = 12
Expand Down Expand Up @@ -152,7 +152,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])
# Aggregator should only be able to access its own attributes

Expand All @@ -168,7 +168,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
)

# check for collaborator private attributes that should not be accessible
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self,attr)]
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self, attr)]
if breached_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
step_name + "_collaborator_attributes_found"
Expand All @@ -177,9 +177,6 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
f"{bcolors.FAIL} ... Attribute test failed in {step_name} - collaborator"
+ f"private attributes accessible:{','.join(breached_collab_attrs)} {bcolors.ENDC}"
)



for idx, collab in enumerate(self.collaborators):
# Collaborator attributes should not be accessible in aggregator step
if (
Expand All @@ -206,13 +203,13 @@ def validate_collab_private_attrs(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])

# Collaborator should only be able to access its own attributes

# check for missing collaborators attributes
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self,attr)]
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self, attr)]

if inaccessible_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start(self):
)
self.collaborators = self.runtime.collaborators

validate_agg_private_attr(self,"start", aggr = ["test_loader_agg_via_callable"], collabs = ["train_loader_via_callable", "test_loader_via_callable"])
validate_agg_private_attr(self, "start", aggr=["test_loader_agg_via_callable"], collabs=["train_loader_via_callable", "test_loader_via_callable"])

self.exclude_agg_to_agg = 10
self.include_agg_to_agg = 100
Expand All @@ -49,7 +49,7 @@ def aggregator_step(self):
Testing whether Agg private attributes are accessible in next agg step.
Collab private attributes should not be accessible here
"""
validate_agg_private_attr(self, "aggregator_step", aggr = ["test_loader_agg_via_callable"], collabs = ["train_loader_via_callable", "test_loader_via_callable"])
validate_agg_private_attr(self, "aggregator_step", aggr=["test_loader_agg_via_callable"], collabs=["train_loader_via_callable", "test_loader_via_callable"])

self.include_agg_to_collab = 42
self.exclude_agg_to_collab = 40
Expand All @@ -66,7 +66,7 @@ def collaborator_step_a(self):
Aggregator private attributes should not be accessible here
"""
validate_collab_private_attrs(
self, "collaborator_step_a", aggr = ["test_loader_agg_via_callable"], collabs = ["train_loader_via_callable", "test_loader_via_callable"]
self, "collaborator_step_a", aggr=["test_loader_agg_via_callable"], collabs=["train_loader_via_callable", "test_loader_via_callable"]
)

self.exclude_collab_to_collab = 2
Expand All @@ -81,7 +81,7 @@ def collaborator_step_b(self):
"""

validate_collab_private_attrs(
self, "collaborator_step_b", aggr = ["test_loader_agg_via_callable"], collabs = ["train_loader_via_callable", "test_loader_via_callable"]
self, "collaborator_step_b", aggr=["test_loader_agg_via_callable"], collabs=["train_loader_via_callable", "test_loader_via_callable"]
)
self.exclude_collab_to_agg = 10
self.include_collab_to_agg = 12
Expand Down Expand Up @@ -152,7 +152,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])
# Aggregator should only be able to access its own attributes

Expand All @@ -168,7 +168,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
)

# check for collaborator private attributes that should not be accessible
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self,attr)]
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self, attr)]
if breached_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
step_name + "_collaborator_attributes_found"
Expand All @@ -177,9 +177,6 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
f"{bcolors.FAIL} ... Attribute test failed in {step_name} - collaborator"
+ f"private attributes accessible:{','.join(breached_collab_attrs)} {bcolors.ENDC}"
)



for idx, collab in enumerate(self.collaborators):
# Collaborator attributes should not be accessible in aggregator step
if (
Expand All @@ -206,13 +203,13 @@ def validate_collab_private_attrs(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])

# Collaborator should only be able to access its own attributes

# check for missing collaborators attributes
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self,attr)]
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self, attr)]

if inaccessible_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start(self):
)
self.collaborators = self.runtime.collaborators

validate_agg_private_attr(self,"start", aggr = ["test_loader_agg"], collabs = ["train_loader", "test_loader"])
validate_agg_private_attr(self, "start", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"])

self.exclude_agg_to_agg = 10
self.include_agg_to_agg = 100
Expand All @@ -49,7 +49,7 @@ def aggregator_step(self):
Testing whether Agg private attributes are accessible in next agg step.
Collab private attributes should not be accessible here
"""
validate_agg_private_attr(self,"aggregator_step", aggr = ["test_loader_agg"], collabs = ["train_loader", "test_loader"])
validate_agg_private_attr(self, "aggregator_step", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"])

self.include_agg_to_collab = 42
self.exclude_agg_to_collab = 40
Expand All @@ -66,7 +66,7 @@ def collaborator_step_a(self):
Aggregator private attributes should not be accessible here
"""
validate_collab_private_attrs(
self, "collaborator_step_a", aggr = ["test_loader_agg"], collabs = ["train_loader", "test_loader"]
self, "collaborator_step_a", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"]
)

self.exclude_collab_to_collab = 2
Expand All @@ -81,7 +81,7 @@ def collaborator_step_b(self):
"""

validate_collab_private_attrs(
self, "collaborator_step_b", aggr = ["test_loader_agg"], collabs = ["train_loader", "test_loader"]
self, "collaborator_step_b", aggr=["test_loader_agg"], collabs=["train_loader", "test_loader"]
)
self.exclude_collab_to_agg = 10
self.include_collab_to_agg = 12
Expand Down Expand Up @@ -152,7 +152,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])
# Aggregator should only be able to access its own attributes

Expand All @@ -168,7 +168,7 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
)

# check for collaborator private attributes that should not be accessible
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self,attr)]
breached_collab_attrs = [attr for attr in collab_attrs if hasattr(self, attr)]
if breached_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
step_name + "_collaborator_attributes_found"
Expand All @@ -177,9 +177,6 @@ def validate_agg_private_attr(self, step_name, **private_attrs_kwargs):
f"{bcolors.FAIL} ... Attribute test failed in {step_name} - collaborator"
+ f"private attributes accessible:{','.join(breached_collab_attrs)} {bcolors.ENDC}"
)



for idx, collab in enumerate(self.collaborators):
# Collaborator attributes should not be accessible in aggregator step
if (
Expand All @@ -205,13 +202,13 @@ def validate_collab_private_attrs(self, step_name, **private_attrs_kwargs):
step_name: Name of the step being validated
private_attr_kwargs: Keyword arguments specifying the names of private attributes for the aggregator and collaborators.
"""
agg_attrs = private_attrs_kwargs.get('aggr',[])
agg_attrs = private_attrs_kwargs.get('aggr', [])
collab_attrs = private_attrs_kwargs.get('collabs', [])

# Collaborator should only be able to access its own attributes

# check for missing collaborators attributes
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self,attr)]
inaccessible_collab_attrs = [attr for attr in collab_attrs if not hasattr(self, attr)]

if inaccessible_collab_attrs:
TestFlowPrivateAttributes.ERROR_LIST.append(
Expand Down
2 changes: 1 addition & 1 deletion tests/github/interactive_api_director/experiment_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def run_federation(shards: typing.Dict[str, Shard], director_path: str):
logger.info('Starting the experiment!')
running_processes = []
p = subprocess.Popen(
f"fx director start --disable-tls",
"fx director start --disable-tls",
shell=True,
cwd=os.path.join(director_path)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from openfl.interface.interactive_api.shard_descriptor import ShardDataset
from openfl.interface.interactive_api.shard_descriptor import ShardDescriptor
from openfl.utilities import validate_file_hash
from zipfile import ZipFile
import requests


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def validate(unet_model, val_loader, device):
samples = target.shape[0]
total_samples += samples
data, target = torch.tensor(data).to(device), \
torch.tensor(target).to(device, dtype=torch.int64)
torch.tensor(target).to(device, dtype=torch.int64)
output = unet_model(data)
val = soft_dice_coef(output, target)
val_score += val.sum().cpu().numpy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ def __init__(self, rank_worldsize: str = '1,1') -> None:
self.X_test = x_test[self.rank - 1::self.worldsize]
self.y_test = y_test[self.rank - 1::self.worldsize]


# Calculating data and target shapes
sample, _ = self[0]
self._sample_shape = [str(dim) for dim in sample.shape]
self._target_shape = ['0']


def __getitem__(self, index):
"""Return a item by the index."""
if index < len(self.X_train):
Expand Down
Loading

0 comments on commit 20ee2c9

Please sign in to comment.