Skip to content

Commit

Permalink
more linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBodor committed Jan 15, 2024
1 parent 5f0617f commit ec159ae
Show file tree
Hide file tree
Showing 42 changed files with 224 additions and 261 deletions.
132 changes: 63 additions & 69 deletions deeprank2/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class DeeprankDataset(Dataset):
def __init__( # pylint: disable=too-many-arguments
def __init__(
self,
hdf5_path: str | list[str],
subset: list[str] | None,
Expand Down Expand Up @@ -90,7 +90,7 @@ def _check_and_inherit_train(
inherited_params,
):
"""Check if the pre-trained model or training set provided are valid for validation and/or testing, and inherit the parameters."""
if isinstance(self.train_source, str): # pylint: disable=too-many-nested-blocks
if isinstance(self.train_source, str):
try:
if torch.cuda.is_available():
data = torch.load(self.train_source)
Expand All @@ -110,7 +110,7 @@ def _check_and_inherit_train(
for _, key in data["features_transform"].items():
if key["transform"] is None:
continue
key["transform"] = eval(key["transform"]) # pylint: disable=eval-used
key["transform"] = eval(key["transform"])
except pickle.UnpicklingError as e:
raise ValueError(
"""The path provided to `train_source` is not a valid DeepRank2 pre-trained model.
Expand Down Expand Up @@ -271,7 +271,7 @@ def _filter_targets(self, grp: h5py.Group) -> bool:
for operator_string in [">", "<", "==", "<=", ">=", "!="]:
operation = operation.replace(operator_string, f"{target_value}" + operator_string)

if not eval(operation): # pylint: disable=eval-used
if not eval(operation):
return False

elif target_condition is not None:
Expand All @@ -289,7 +289,7 @@ def len(self) -> int:
"""
return len(self.index_entries)

def hdf5_to_pandas( # noqa: MC0001, pylint: disable=too-many-locals
def hdf5_to_pandas(
self,
) -> pd.DataFrame:
"""Loads features data from the HDF5 files into a Pandas DataFrame in the attribute `df` of the class.
Expand Down Expand Up @@ -345,7 +345,7 @@ def hdf5_to_pandas( # noqa: MC0001, pylint: disable=too-many-locals

return df_final

def save_hist( # pylint: disable=too-many-arguments, too-many-branches, useless-suppression
def save_hist(
self,
features: str | list[str],
fname: str = "features_hist.png",
Expand Down Expand Up @@ -396,13 +396,12 @@ def save_hist( # pylint: disable=too-many-arguments, too-many-branches, useless
axs[row].hist(log_data, bins=bins)
else:
axs[row].hist(np.concatenate(self.df[feat].values), bins=bins)
elif log:
log_data = np.log(self.df[feat].values)
log_data[log_data == -np.inf] = 0
axs[row].hist(log_data, bins=bins)
else:
if log:
log_data = np.log(self.df[feat].values)
log_data[log_data == -np.inf] = 0
axs[row].hist(log_data, bins=bins)
else:
axs[row].hist(self.df[feat].values, bins=bins)
axs[row].hist(self.df[feat].values, bins=bins)
axs[row].set(
xlabel=f"{feat} (mean {means[row]}, std {devs[row]})",
ylabel="Count",
Expand All @@ -419,13 +418,12 @@ def save_hist( # pylint: disable=too-many-arguments, too-many-branches, useless
ax.hist(log_data, bins=bins)
else:
ax.hist(np.concatenate(self.df[features_df[0]].values), bins=bins)
elif log:
log_data = np.log(self.df[features_df[0]].values)
log_data[log_data == -np.inf] = 0
ax.hist(log_data, bins=bins)
else:
if log:
log_data = np.log(self.df[features_df[0]].values)
log_data[log_data == -np.inf] = 0
ax.hist(log_data, bins=bins)
else:
ax.hist(self.df[features_df[0]].values, bins=bins)
ax.hist(self.df[features_df[0]].values, bins=bins)
ax.set(
xlabel=f"{features_df[0]} (mean {means[0]}, std {devs[0]})",
ylabel="Count",
Expand Down Expand Up @@ -462,7 +460,7 @@ def _compute_mean_std(self):


class GridDataset(DeeprankDataset):
def __init__( # pylint: disable=too-many-arguments, too-many-locals
def __init__(
self,
hdf5_path: str | list,
subset: list[str] | None = None,
Expand Down Expand Up @@ -676,25 +674,24 @@ def load_one_grid(self, hdf5_path: str, entry_name: str) -> Data:
# target
if self.target is None:
y = None
elif targets.VALUES in grp and self.target in grp[targets.VALUES]:
y = torch.tensor([grp[targets.VALUES][self.target][()]], dtype=torch.float)

if self.task == targets.REGRESS and self.target_transform is True:
y = torch.sigmoid(torch.log(y))
elif self.task is not targets.REGRESS and self.target_transform is True:
raise ValueError(
f'Sigmoid transformation is not possible for {self.task} tasks. \
Please change `task` to "regress" or set `target_transform` to `False`.'
)
else:
if targets.VALUES in grp and self.target in grp[targets.VALUES]:
y = torch.tensor([grp[targets.VALUES][self.target][()]], dtype=torch.float)

if self.task == targets.REGRESS and self.target_transform is True:
y = torch.sigmoid(torch.log(y))
elif self.task is not targets.REGRESS and self.target_transform is True:
raise ValueError(
f'Sigmoid transformation is not possible for {self.task} tasks. \
Please change `task` to "regress" or set `target_transform` to `False`.'
)
else:
y = None
possible_targets = grp[targets.VALUES].keys()
if self.train_source is None:
raise ValueError(
f"Target {self.target} missing in entry {entry_name} in file {hdf5_path}, possible targets are {possible_targets}."
+ "\n Use the query class to add more target values to input data."
)
y = None
possible_targets = grp[targets.VALUES].keys()
if self.train_source is None:
raise ValueError(
f"Target {self.target} missing in entry {entry_name} in file {hdf5_path}, possible targets are {possible_targets}."
+ "\n Use the query class to add more target values to input data."
)

# Wrap up the data in this object, for the collate_fn to handle it properly:
data = Data(x=x, y=y)
Expand All @@ -704,7 +701,7 @@ def load_one_grid(self, hdf5_path: str, entry_name: str) -> Data:


class GraphDataset(DeeprankDataset):
def __init__( # noqa: MC0001, pylint: disable=too-many-arguments, too-many-locals
def __init__(
self,
hdf5_path: str | list,
subset: list[str] | None = None,
Expand Down Expand Up @@ -876,7 +873,7 @@ def get(self, idx: int) -> Data:
fname, mol = self.index_entries[idx]
return self.load_one_graph(fname, mol)

def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disable = too-many-locals # noqa: MC0001
def load_one_graph(self, fname: str, entry_name: str) -> Data: # noqa: PLR0915 (too-many-statements)
"""Loads one graph.
Args:
Expand Down Expand Up @@ -925,11 +922,10 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl
vals = vals.reshape(-1, 1)
if standard:
vals = (vals - self.means[feat]) / self.devs[feat]
else:
if standard:
reshaped_mean = [mean_value for mean_key, mean_value in self.means.items() if feat in mean_key]
reshaped_dev = [dev_value for dev_key, dev_value in self.devs.items() if feat in dev_key]
vals = (vals - reshaped_mean) / reshaped_dev
elif standard:
reshaped_mean = [mean_value for mean_key, mean_value in self.means.items() if feat in mean_key]
reshaped_dev = [dev_value for dev_key, dev_value in self.devs.items() if feat in dev_key]
vals = (vals - reshaped_mean) / reshaped_dev
node_data += (vals,)
x = torch.tensor(np.hstack(node_data), dtype=torch.float)
else:
Expand Down Expand Up @@ -983,11 +979,10 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl
vals = vals.reshape(-1, 1)
if standard:
vals = (vals - self.means[feat]) / self.devs[feat]
else:
if standard:
reshaped_mean = [mean_value for mean_key, mean_value in self.means.items() if feat in mean_key]
reshaped_dev = [dev_value for dev_key, dev_value in self.devs.items() if feat in dev_key]
vals = (vals - reshaped_mean) / reshaped_dev
elif standard:
reshaped_mean = [mean_value for mean_key, mean_value in self.means.items() if feat in mean_key]
reshaped_dev = [dev_value for dev_key, dev_value in self.devs.items() if feat in dev_key]
vals = (vals - reshaped_mean) / reshaped_dev
edge_data += (vals,)
edge_data = np.hstack(edge_data)
edge_data = np.vstack((edge_data, edge_data))
Expand All @@ -998,26 +993,25 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl
# target
if self.target is None:
y = None
else:
if targets.VALUES in grp and self.target in grp[targets.VALUES]:
y = torch.tensor([grp[f"{targets.VALUES}/{self.target}"][()]], dtype=torch.float).contiguous()

if self.task == targets.REGRESS and self.target_transform is True:
y = torch.sigmoid(torch.log(y))
elif self.task is not targets.REGRESS and self.target_transform is True:
raise ValueError(
f'Sigmoid transformation is not possible for {self.task} tasks. \
Please change `task` to "regress" or set `target_transform` to `False`.'
)
elif targets.VALUES in grp and self.target in grp[targets.VALUES]:
y = torch.tensor([grp[f"{targets.VALUES}/{self.target}"][()]], dtype=torch.float).contiguous()

if self.task == targets.REGRESS and self.target_transform is True:
y = torch.sigmoid(torch.log(y))
elif self.task is not targets.REGRESS and self.target_transform is True:
raise ValueError(
f'Sigmoid transformation is not possible for {self.task} tasks. \
Please change `task` to "regress" or set `target_transform` to `False`.'
)

else:
y = None
possible_targets = grp[targets.VALUES].keys()
if self.train_source is None:
raise ValueError(
f"Target {self.target} missing in entry {entry_name} in file {fname}, possible targets are {possible_targets}."
+ "\n Use the query class to add more target values to input data."
)
else:
y = None
possible_targets = grp[targets.VALUES].keys()
if self.train_source is None:
raise ValueError(
f"Target {self.target} missing in entry {entry_name} in file {fname}, possible targets are {possible_targets}."
+ "\n Use the query class to add more target values to input data."
)

# positions
pos = torch.tensor(grp[f"{Nfeat.NODE}/{Nfeat.POSITION}/"][()], dtype=torch.float).contiguous()
Expand Down Expand Up @@ -1052,7 +1046,7 @@ def load_one_graph(self, fname: str, entry_name: str) -> Data: # pylint: disabl

return data

def _check_features(self): # pylint: disable=too-many-branches
def _check_features(self):
"""Checks if the required features exist."""
f = h5py.File(self.hdf5_paths[0], "r")
mol_key = list(f.keys())[0]
Expand Down
4 changes: 2 additions & 2 deletions deeprank2/features/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
_log = logging.getLogger(__name__)


def add_features( # pylint: disable=unused-argument
pdb_path: str,
def add_features(
pdb_path: str, # noqa: ARG001 (unused argument)
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
):
Expand Down
4 changes: 2 additions & 2 deletions deeprank2/features/conservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from deeprank2.utils.graph import Graph


def add_features( # pylint: disable=unused-argument
pdb_path: str,
def add_features(
pdb_path: str, # noqa: ARG001 (unused argument)
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
):
Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
cutoff_14 = 4.2


def _get_nonbonded_energy( # pylint: disable=too-many-locals
def _get_nonbonded_energy(
atoms: list[Atom],
distances: NDArray[np.float64],
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
Expand Down Expand Up @@ -72,10 +72,10 @@ def _get_nonbonded_energy( # pylint: disable=too-many-locals
return E_elec, E_vdw


def add_features( # pylint: disable=unused-argument, too-many-locals
pdb_path: str,
def add_features(
pdb_path: str, # noqa: ARG001 (unused argument)
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
single_amino_acid_variant: SingleResidueVariant | None = None, # noqa: ARG001 (unused argument)
):
# assign each atoms (from all edges) a unique index
all_atoms = set()
Expand Down
8 changes: 4 additions & 4 deletions deeprank2/features/exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
_log = logging.getLogger(__name__)


def handle_sigint(sig, frame): # pylint: disable=unused-argument
def handle_sigint(sig, frame): # noqa: ARG001 (unused argument)
print("SIGINT received, terminating.")
sys.exit()


def handle_timeout(sig, frame):
def handle_timeout(sig, frame): # noqa: ARG001 (unused argument)
raise TimeoutError("Timed out!")


Expand All @@ -32,10 +32,10 @@ def space_if_none(value):
return value


def add_features( # pylint: disable=unused-argument
def add_features(
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
single_amino_acid_variant: SingleResidueVariant | None = None, # noqa: ARG001 (unused argument)
):
signal.signal(signal.SIGINT, handle_sigint)
signal.signal(signal.SIGALRM, handle_timeout)
Expand Down
6 changes: 2 additions & 4 deletions deeprank2/features/secondary_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def _get_secstructure(pdb_path: str) -> dict:
p = PDBParser(QUIET=True)
model = p.get_structure(Path(pdb_path).stem, pdb_path)[0]

# pylint: disable=raise-missing-from
try:
dssp = DSSP(model, pdb_path, dssp="mkdssp")
except Exception as e: # improperly formatted pdb files raise: `Exception: DSSP failed to produce an output`
Expand All @@ -121,10 +120,10 @@ def _get_secstructure(pdb_path: str) -> dict:
return sec_structure_dict


def add_features( # pylint: disable=unused-argument
def add_features(
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
single_amino_acid_variant: SingleResidueVariant | None = None, # noqa: ARG001 (unused argument)
):
sec_structure_features = _get_secstructure(pdb_path)

Expand All @@ -140,7 +139,6 @@ def add_features( # pylint: disable=unused-argument
chain_id = residue.chain.id
res_num = residue.number

# pylint: disable=raise-missing-from
try:
node.features[Nfeat.SECSTRUCT] = _classify_secstructure(sec_structure_features[chain_id][res_num]).onehot
except AttributeError as e:
Expand Down
16 changes: 4 additions & 12 deletions deeprank2/features/surfacearea.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from deeprank2.molstruct.residue import Residue, SingleResidueVariant
from deeprank2.utils.graph import Graph

# pylint: disable=c-extension-no-member

freesasa.setVerbosity(freesasa.nowarnings)
logging.getLogger(__name__)

Expand Down Expand Up @@ -108,30 +106,24 @@ def add_bsa(graph: Graph):
residue = node.id
chain_id = residue.chain.id
area_key = "residue"
selection = (
"residue, (resi %s) and (chain %s)" # pylint: disable=consider-using-f-string
% (residue.number_string, residue.chain.id),
)
selection = (f"residue, (resi {residue.number_string}) and (chain {residue.chain.id})",)

elif isinstance(node.id, Atom):
atom = node.id
chain_id = atom.residue.chain.id
area_key = "atom"
selection = (
"atom, (name %s) and (resi %s) and (chain %s)" # pylint: disable=consider-using-f-string
% (atom.name, atom.residue.number_string, atom.residue.chain.id),
)
selection = (f"atom, (name {atom.name}) and (resi {atom.residue.number_string}) and (chain {atom.residue.chain.id})",)

area_monomer = freesasa.selectArea(selection, sasa_chain_structures[chain_id], sasa_chain_results[chain_id])[area_key]
area_multimer = freesasa.selectArea(selection, sasa_complete_structure, sasa_complete_result)[area_key]

node.features[Nfeat.BSA] = area_monomer - area_multimer


def add_features( # pylint: disable=unused-argument
def add_features(
pdb_path: str,
graph: Graph,
single_amino_acid_variant: SingleResidueVariant | None = None,
single_amino_acid_variant: SingleResidueVariant | None = None, # noqa: ARG001 (unused argument)
):
"""Calculates the Buried Surface Area (BSA) and the Solvent Accessible Surface Area (SASA)."""
# BSA
Expand Down
Loading

0 comments on commit ec159ae

Please sign in to comment.