From bd4b0230ca5a4bb06155bf0e4099077a55396bec Mon Sep 17 00:00:00 2001 From: Pau Andrio Date: Thu, 17 Oct 2024 13:45:21 +0200 Subject: [PATCH] [TYPING](ALL): Fix typing errors --- biobb_pytorch/docs/source/conf.py | 15 +++++++-------- biobb_pytorch/mdae/apply_mdae.py | 10 ++++++---- biobb_pytorch/mdae/common.py | 10 +++++----- biobb_pytorch/mdae/mdae.py | 5 ++--- biobb_pytorch/mdae/train_mdae.py | 31 +++++++++++++++++-------------- 5 files changed, 37 insertions(+), 34 deletions(-) diff --git a/biobb_pytorch/docs/source/conf.py b/biobb_pytorch/docs/source/conf.py index 81f900a..bdd6623 100644 --- a/biobb_pytorch/docs/source/conf.py +++ b/biobb_pytorch/docs/source/conf.py @@ -13,7 +13,6 @@ # serve to show the default. import sys -from typing import List, Dict from pathlib import Path # If extensions (or modules to document with autodoc) are in another directory, @@ -95,9 +94,9 @@ # Else, today_fmt is used as the format for a strftime call. # today_fmt = '%B %d, %Y' -# List of patterns, relative to source directory, that match files and +# list of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns: List[str] = [] +exclude_patterns: list[str] = [] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -123,7 +122,7 @@ # If true, keep warnings as "system message" paragraphs in the built documents. # keep_warnings = False -# If true, `todo` and `todoList` produce output, else they produce nothing. +# If true, `todo` and `todolist` produce output, else they produce nothing. todo_include_todos = False @@ -232,7 +231,7 @@ def setup(app): # -- Options for LaTeX output --------------------------------------------- -latex_elements: Dict[str, str] = { +latex_elements: dict[str, str] = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', @@ -246,7 +245,7 @@ def setup(app): # 'figure_align': 'htbp', } -# Grouping the document tree into LaTeX files. List of tuples +# Grouping the document tree into LaTeX files. list of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ @@ -277,7 +276,7 @@ def setup(app): # -- Options for manual page output --------------------------------------- -# One entry per manual page. List of tuples +# One entry per manual page. list of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'biobb_pytorch', u'biobb_pytorch Documentation', @@ -290,7 +289,7 @@ def setup(app): # -- Options for Texinfo output ------------------------------------------- -# Grouping the document tree into Texinfo files. List of tuples +# Grouping the document tree into Texinfo files. list of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ diff --git a/biobb_pytorch/mdae/apply_mdae.py b/biobb_pytorch/mdae/apply_mdae.py index 91d0e3b..290c6eb 100755 --- a/biobb_pytorch/mdae/apply_mdae.py +++ b/biobb_pytorch/mdae/apply_mdae.py @@ -2,10 +2,12 @@ """Module containing the ApplyMDAE class and the command line interface.""" import torch +import torch.utils.data import numpy as np import argparse +from typing import Optional import time -from typing import Optional, Tuple, Dict +from typing import Optional from biobb_common.generic.biobb_object import BiobbObject from biobb_common.configuration import settings from biobb_common.tools import file_utils as fu @@ -60,7 +62,7 @@ def __init__(self, input_model_pth_path: str, output_reconstructed_data_npy_path: str, output_latent_space_npy_path: Optional[str] = None, - properties: Optional[Dict] = None, **kwargs) -> None: + properties: Optional[dict] = None, **kwargs) -> None: properties = properties or {} # Call parent class constructor @@ -131,7 +133,7 @@ def launch(self) -> int: self.check_arguments(output_files_created=True, raise_exception=False) return 0 - def apply_model(self, dataloader: torch.utils.data.DataLoader) -> Tuple[np.ndarray, np.ndarray]: + def apply_model(self, dataloader: torch.utils.data.DataLoader) -> tuple[np.ndarray, np.ndarray]: self.model.to(self.model.device) start_time: float = time.time() fu.log("Applying model:", self.out_log) @@ -153,7 +155,7 @@ def apply_model(self, dataloader: torch.utils.data.DataLoader) -> Tuple[np.ndarr def applyMDAE(input_data_npy_path: str, input_model_pth_path: str, output_reconstructed_data_npy_path: str, output_latent_space_npy_path: Optional[str] = None, - properties: Optional[Dict] = None, **kwargs) -> int: + properties: Optional[dict] = None, **kwargs) -> int: """Execute the :class:`ApplyMDAE ` class and execute the :meth:`launch() ` method.""" diff --git a/biobb_pytorch/mdae/common.py b/biobb_pytorch/mdae/common.py index ced2bb4..ba4ba65 100644 --- a/biobb_pytorch/mdae/common.py +++ b/biobb_pytorch/mdae/common.py @@ -2,7 +2,7 @@ import numpy as np import torch from pathlib import Path -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union def ndarray_normalization(ndarray: np.ndarray, max_values: np.ndarray, min_values: np.ndarray) -> np.ndarray: @@ -69,11 +69,11 @@ def get_optimizer_function(optimizer_function: str) -> Callable: raise ValueError(f'Invalid optimizer function: {optimizer_function}') -def execute_model(model: torch.nn.Module, dataloader: torch.utils.data.DataLoader, input_dimensions: int, latent_dimensions: int, loss_function: Optional[torch.nn.modules.loss._Loss] = None) -> Tuple[float, np.ndarray, np.ndarray]: +def execute_model(model: torch.nn.Module, dataloader: torch.utils.data.DataLoader, input_dimensions: int, latent_dimensions: int, loss_function: Optional[torch.nn.modules.loss._Loss] = None) -> tuple[float, np.ndarray, np.ndarray]: model.eval() - losses: List[float] = [] - z_list: List[float] = [] - x_hat_list: List[float] = [] + losses: list[float] = [] + z_list: list[float] = [] + x_hat_list: list[float] = [] with torch.no_grad(): for data in dataloader: data = data[0].to(model.device) diff --git a/biobb_pytorch/mdae/mdae.py b/biobb_pytorch/mdae/mdae.py index ac989c6..962e8d9 100644 --- a/biobb_pytorch/mdae/mdae.py +++ b/biobb_pytorch/mdae/mdae.py @@ -1,6 +1,5 @@ """Module containing the MDAutoEncoder class and the command line interface.""" import torch -from typing import List class MDAE(torch.nn.Module): @@ -16,7 +15,7 @@ def __init__(self, input_dimensions: int, num_layers: int, latent_dimensions: in self.leaky_relu: float = leaky_relu # Encoder - encoder: List = [] + encoder: list = [] nunits: int = self.input_dimensions for _ in range(self.num_layers-1): encoder.append(torch.nn.Linear(nunits, nunits - self.delta)) @@ -32,7 +31,7 @@ def __init__(self, input_dimensions: int, num_layers: int, latent_dimensions: in torch.nn.Sigmoid()) # Decoder - decoder: List = [] + decoder: list = [] nunits = self.latent_dimensions for _ in range(self.num_layers-1): decoder.append(torch.nn.Linear(nunits, nunits + self.delta)) diff --git a/biobb_pytorch/mdae/train_mdae.py b/biobb_pytorch/mdae/train_mdae.py index 37f6c6b..18e54d8 100755 --- a/biobb_pytorch/mdae/train_mdae.py +++ b/biobb_pytorch/mdae/train_mdae.py @@ -2,10 +2,14 @@ """Module containing the TrainMDAE class and the command line interface.""" import torch +import torch.utils.data import numpy as np import time import argparse -from typing import Optional, List, Tuple, Dict +from typing import Optional +from typing import Optional +from torch.optim.optimizer import Optimizer +from torch.optim.adam import Adam from biobb_common.generic.biobb_object import BiobbObject from biobb_common.configuration import settings from biobb_common.tools import file_utils as fu @@ -87,7 +91,7 @@ def __init__(self, input_train_npy_path: str, input_model_pth_path: Optional[str] = None, output_train_data_npz_path: Optional[str] = None, # npz of train_losses, valid_losses output_performance_npz_path: Optional[str] = None, # npz of evaluate_losses, latent_space, reconstructed_data - properties: Optional[Dict] = None, **kwargs) -> None: + properties: Optional[dict] = None, **kwargs) -> None: properties = properties or {} # Call parent class constructor @@ -177,12 +181,11 @@ def __init__(self, input_train_npy_path: str, optimizer_str: str = properties.get('optimizer', '') try: - self.optimizer: torch.optim.Optimizer = get_optimizer_function(optimizer_str)(self.model.parameters(), lr=self.lr) + self.optimizer = get_optimizer_function(optimizer_str)(self.model.parameters(), lr=self.lr) fu.log(f'Using optimizer: {self.optimizer}', self.out_log) except ValueError: fu.log(f'Invalid optimizer: {optimizer_str}', self.out_log) - fu.log('Using default optimizer: Adam', self.out_log) - self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr) + self.optimizer = Adam(self.model.parameters(), lr=self.lr) @launchlogger def launch(self) -> int: @@ -225,10 +228,10 @@ def launch(self) -> int: self.check_arguments(output_files_created=True, raise_exception=False) return 0 - def train_model(self) -> Tuple[List[float], List[float], Dict, int]: + def train_model(self) -> tuple[list[float], list[float], dict, int]: self.model.to(self.model.device) - train_losses: List[float] = [] - validation_losses: List[float] = [] + train_losses: list[float] = [] + validation_losses: list[float] = [] best_valid_loss: float = float('inf') # Initialize best valid loss to infinity start_time: float = time.time() @@ -280,16 +283,16 @@ def train_model(self) -> Tuple[List[float], List[float], Dict, int]: # Save best model if avg_validation_loss < best_valid_loss: best_valid_loss = avg_validation_loss - best_model: Dict = self.model.state_dict() + best_model: dict = self.model.state_dict() best_model_epoch: int = epoch_index fu.log(f"End Training, total time: {format_time((time.time() - start_time))}", self.out_log) return train_losses, validation_losses, best_model, best_model_epoch - def training_step(self, dataloader: torch.utils.data.DataLoader, optimizer: torch.optim.Optimizer, loss_function: torch.nn.modules.loss._Loss) -> Tuple[float, float]: + def training_step(self, dataloader: torch.utils.data.DataLoader, optimizer: Optimizer, loss_function: torch.nn.modules.loss._Loss) -> tuple[float, float]: self.model.train() - train_losses: List[float] = [] + train_losses: list[float] = [] for data in dataloader: data = data[0].to(self.model.device) _, output = self.model(data) @@ -300,7 +303,7 @@ def training_step(self, dataloader: torch.utils.data.DataLoader, optimizer: torc train_losses.append(loss.item()) self.model.eval() - valid_losses: List[float] = [] + valid_losses: list[float] = [] with torch.no_grad(): for data in dataloader: data = data[0].to(self.model.device) @@ -310,13 +313,13 @@ def training_step(self, dataloader: torch.utils.data.DataLoader, optimizer: torc return float(np.mean(train_losses)), float(torch.mean(torch.tensor(valid_losses))) - def evaluate_model(self, dataloader: torch.utils.data.DataLoader, loss_function: torch.nn.modules.loss._Loss) -> Tuple[float, np.ndarray, np.ndarray]: + def evaluate_model(self, dataloader: torch.utils.data.DataLoader, loss_function: torch.nn.modules.loss._Loss) -> tuple[float, np.ndarray, np.ndarray]: return execute_model(self.model, dataloader, self.input_dimensions, self.latent_dimensions, loss_function) def trainMDAE(input_train_npy_path: str, output_model_pth_path: str, input_model_pth_path: Optional[str] = None, output_train_data_npz_path: Optional[str] = None, output_performance_npz_path: Optional[str] = None, - properties: Optional[Dict] = None, **kwargs) -> int: + properties: Optional[dict] = None, **kwargs) -> int: """Execute the :class:`TrainMDAE ` class and execute the :meth:`launch() ` method."""