Skip to content

Commit

Permalink
Change ut.check_non_negative_number to ut.check_number_range
Browse files Browse the repository at this point in the history
  • Loading branch information
breimanntools committed Sep 25, 2023
1 parent 5b623d0 commit 452f9aa
Show file tree
Hide file tree
Showing 78 changed files with 661 additions and 620 deletions.
6 changes: 4 additions & 2 deletions aaanalysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from aaanalysis.aaclust import AAclust
from aaanalysis.cpp import CPP, CPPPlot, SequenceFeature, SplitRange
from aaanalysis.dpulearn import dPULearn
from aaanalysis.plotting import plot_settings, plot_set_legend, plot_gcfs, plot_get_cmap, plot_get_cdict
from aaanalysis.plotting import (plot_settings, plot_set_legend, plot_gcfs,
plot_get_cmap, plot_get_cdict)

__all__ = ["load_dataset", "load_scales", "AAclust",
"CPP", "CPPPlot", "SequenceFeature", "SplitRange",
"dPULearn",
"plot_settings", "plot_set_legend", "plot_gcfs", "plot_get_cmap", "plot_get_cdict"]
"plot_get_cmap", "plot_get_cdict",
"plot_settings", "plot_set_legend", "plot_gcfs"]
Binary file modified aaanalysis/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/__pycache__/utils.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/_utils/__pycache__/_utils_check.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/_utils/__pycache__/utils_cpp.cpython-39.pyc
Binary file not shown.
32 changes: 15 additions & 17 deletions aaanalysis/_utils/_utils_check.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
"""
Utility check functions
Basic utility check functions for type checking
"""
from sklearn.utils import check_array, check_consistent_length
import pandas as pd


# Type checking functions
def check_non_negative_number(name=None, val=None, min_val=0, max_val=None, accept_none=False,
just_int=None):
"""Check if value of given name variable is non-negative integer"""
def check_number_range(name=None, val=None, min_val=0, max_val=None, accept_none=False, just_int=None):
"""Check if value of given name is within defined range"""
if just_int is None:
raise ValueError("'just_int' must be specified")
check_types = [int] if just_int else [float, int]
str_check = "non-negative int" if just_int else "non-negative float or int"
add_str = f"n>={min_val}" if max_val is None else f"{min_val}<=n<={max_val}"
if accept_none:
add_str += " or None"
error = f"'{name}' ({val}) should be {str_check} n, with " + add_str
if accept_none and val is None:
return None
if type(val) not in check_types:
raise ValueError(error)
if val < min_val:
raise ValueError(error)
if max_val is not None and val > max_val:
valid_types = (int,) if just_int else (float, int)
type_description = "int" if just_int else "float or int"

# Verify the value's type and range
if not isinstance(val, valid_types) or val < min_val or (max_val is not None and val > max_val):
range_desc = f"n>={min_val}" if max_val is None else f"{min_val}<=n<={max_val}"
error = f"'{name}' ({val}) should be {type_description} in the range: {range_desc}"
if accept_none:
error += " or None"
raise ValueError(error)


def check_float(name=None, val=None, accept_none=False, just_float=True):
def check_number_val(name=None, val=None, accept_none=False, just_int=False):
"""Check if value is float"""
check_types = [int] if just_int else [float, int]
if accept_none and val is None:
return None
if type(val) not in [float, int]:
error = f"'{name}' ({val}) should be float"
if not just_float:
if not just_int:
error += " or int."
else:
error += "."
Expand Down
18 changes: 9 additions & 9 deletions aaanalysis/_utils/utils_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def check_ylim(df=None, ylim=None, val_col=None, retrieve_plot=False, scaling_fa
""""""
if ylim is not None:
ut_check.check_tuple(name="ylim", val=ylim, n=2)
ut_check.check_float(name="ylim:min", val=ylim[0], just_float=False)
ut_check.check_float(name="ylim:max", val=ylim[1], just_float=False)
ut_check.check_number_val(name="ylim:min", val=ylim[0], just_int=False)
ut_check.check_number_val(name="ylim:max", val=ylim[1], just_int=False)
max_val = round(max(df[val_col]), 3)
max_y = ylim[1]
if max_val >= max_y:
Expand Down Expand Up @@ -117,10 +117,10 @@ def check_args_len(tmd_len=None, jmd_n_len=None, jmd_c_len=None, ext_len=None,
"""Check length parameters and if they are matching with sequences if provided"""
# Check lengths
tmd_seq_given = tmd_seq is not None or accept_tmd_none # If tmd_seq is given, tmd_len can be None
ut_check.check_non_negative_number(name="tmd_len", val=tmd_len, accept_none=tmd_seq_given, min_val=1, just_int=True)
ut_check.check_non_negative_number(name="jmd_n_len", val=jmd_n_len, accept_none=True, min_val=1, just_int=True)
ut_check.check_non_negative_number(name="jmd_c_len", val=jmd_c_len, accept_none=True, min_val=1, just_int=True)
ut_check.check_non_negative_number(name="ext_len", val=ext_len, accept_none=True, just_int=True)
ut_check.check_number_range(name="tmd_len", val=tmd_len, accept_none=tmd_seq_given, min_val=1, just_int=True)
ut_check.check_number_range(name="jmd_n_len", val=jmd_n_len, accept_none=True, min_val=1, just_int=True)
ut_check.check_number_range(name="jmd_c_len", val=jmd_c_len, accept_none=True, min_val=1, just_int=True)
ut_check.check_number_range(name="ext_len", val=ext_len, accept_none=True, just_int=True)
# Check if lengths and sequences match (any sequence is excepted, strings, lists, arrays)
tmd_len = _check_seq(tmd_seq, tmd_len, "tmd_seq", "tmd_len")
jmd_n_len = _check_seq(jmd_n_seq, jmd_n_len, "jmd_n_seq", "jmd_n_len")
Expand Down Expand Up @@ -220,7 +220,7 @@ def check_split(split=None):
i_th, n_split = [int(x) for x in split.split("(")[1].replace(")", "").split(",")]
# Check if values non-negative integers
for name, val in zip(["i_th", "n_split"], [i_th, n_split]):
ut_check.check_non_negative_number(name=name, val=val, just_int=True)
ut_check.check_number_range(name=name, val=val, just_int=True)
# Check if i-th and n_split are valid
if i_th > n_split:
raise ValueError
Expand All @@ -233,7 +233,7 @@ def check_split(split=None):
start = int(start[0])
# Check if values non-negative integers
for name, val in zip(["start", "step1", "step2"], [start, step1, step2]):
ut_check.check_non_negative_number(name=name, val=val, just_int=True)
ut_check.check_number_range(name=name, val=val, just_int=True)
# Check if terminus valid
terminus = split.split("i+")[0].split("(")[1].replace(",", "")
if terminus not in ["N", "C"]:
Expand All @@ -248,7 +248,7 @@ def check_split(split=None):
list_pos = [int(x) for x in list_pos]
for val in list_pos:
name = "pos" + str(val)
ut_check.check_non_negative_number(name=name, val=val, just_int=True)
ut_check.check_number_range(name=name, val=val, just_int=True)
# Check if terminus valid
if terminus not in ["N", "C"]:
raise ValueError
Expand Down
Binary file modified aaanalysis/cpp/__pycache__/_cpp.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/cpp/__pycache__/_feature_pos.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/cpp/__pycache__/cpp.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/cpp/__pycache__/cpp_plot.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/cpp/__pycache__/feature.cpython-39.pyc
Binary file not shown.
8 changes: 4 additions & 4 deletions aaanalysis/cpp/_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ def draw_shap_legend(x=None, y=10, offset_text=1, fontsize=13):
class CPPPlots:
"""Class for plotting functions for CPP analysis"""
def __init__(self, tmd_len=20, jmd_n_len=10, jmd_c_len=10, start=1):
ut.check_non_negative_number(name="tmd_len", val=tmd_len, min_val=1, just_int=True)
ut.check_non_negative_number(name="jmd_n_len", val=jmd_n_len, min_val=0, just_int=True)
ut.check_non_negative_number(name="jmd_c_len", val=jmd_c_len, min_val=0, just_int=True)
ut.check_non_negative_number(name="start", val=start, just_int=True)
ut.check_number_range(name="tmd_len", val=tmd_len, min_val=1, just_int=True)
ut.check_number_range(name="jmd_n_len", val=jmd_n_len, min_val=0, just_int=True)
ut.check_number_range(name="jmd_c_len", val=jmd_c_len, min_val=0, just_int=True)
ut.check_number_range(name="start", val=start, just_int=True)
self.jmd_n_len = jmd_n_len
self.tmd_len = tmd_len
self.jmd_c_len = jmd_c_len
Expand Down
2 changes: 1 addition & 1 deletion aaanalysis/cpp/_feature_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_dict_part_pos(tmd_len=20, jmd_n_len=10, jmd_c_len=10, ext_len=0, start=1
-------
dict_part_pos: dictionary with parts to positions of parts
"""
ut.check_non_negative_number(name="start", val=start, min_val=1, just_int=True)
ut.check_number_range(name="start", val=start, min_val=1, just_int=True)
ut.check_args_len(tmd_len=tmd_len, jmd_n_len=jmd_n_len, jmd_c_len=jmd_c_len, ext_len=ext_len)
pa = Parts()
jmd_n = list(range(0, jmd_n_len))
Expand Down
10 changes: 5 additions & 5 deletions aaanalysis/cpp/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ def run(self, labels=None, parametric=False, n_filter=100,
# Check input
ut.check_labels(labels=labels, df=self.df_parts, name_df="df_parts")
ut.check_args_len(tmd_len=tmd_len, jmd_n_len=jmd_n_len, jmd_c_len=jmd_c_len, ext_len=ext_len)
ut.check_non_negative_number(name="n_filter", val=n_filter, min_val=1, just_int=True)
ut.check_non_negative_number(name="n_pre_filter", val=n_pre_filter, min_val=1, accept_none=True, just_int=True)
ut.check_non_negative_number(name="pct_pre_filter", val=pct_pre_filter, min_val=5, max_val=100, just_int=True)
ut.check_non_negative_number(name="max_std_test", val=max_std_test, min_val=0.0, max_val=1.0, just_int=False)
ut.check_non_negative_number(name="max_overlap", val=max_overlap, min_val=0.0, max_val=1.0, just_int=False)
ut.check_number_range(name="n_filter", val=n_filter, min_val=1, just_int=True)
ut.check_number_range(name="n_pre_filter", val=n_pre_filter, min_val=1, accept_none=True, just_int=True)
ut.check_number_range(name="pct_pre_filter", val=pct_pre_filter, min_val=5, max_val=100, just_int=True)
ut.check_number_range(name="max_std_test", val=max_std_test, min_val=0.0, max_val=1.0, just_int=False)
ut.check_number_range(name="max_overlap", val=max_overlap, min_val=0.0, max_val=1.0, just_int=False)
ut.check_bool(name="verbose", val=self._verbose)
# Settings and creation of objects
args = dict(split_kws=self.split_kws, df_scales=self.df_scales)
Expand Down
39 changes: 19 additions & 20 deletions aaanalysis/cpp/cpp_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ def check_normalize(normalize=True):
# Check for plotting methods
def check_args_size(seq_size=None, tmd_jmd_fontsize=None):
"""Check if sequence size parameters match"""
ut.check_non_negative_number(name="seq_size", val=seq_size, min_val=0, accept_none=True, just_int=False)
ut.check_non_negative_number(name="tmd_jmd_fontsize", val=tmd_jmd_fontsize, min_val=0, accept_none=True, just_int=False)
ut.check_number_range(name="seq_size", val=seq_size, min_val=0, accept_none=True, just_int=False)
ut.check_number_range(name="tmd_jmd_fontsize", val=tmd_jmd_fontsize, min_val=0, accept_none=True, just_int=False)
args_size = dict(seq_size=seq_size, tmd_jmd_fontsize=tmd_jmd_fontsize)
return args_size


def check_args_xtick(xtick_size=None, xtick_width=None, xtick_length=None):
"""Check if x tick parameters non-negative float"""
args = dict(accept_none=True, just_int=False, min_val=0)
ut.check_non_negative_number(name="xtick_size", val=xtick_size, **args)
ut.check_non_negative_number(name="xtick_width", val=xtick_width, **args)
ut.check_non_negative_number(name="xtick_length", val=xtick_length, **args)
ut.check_number_range(name="xtick_size", val=xtick_size, **args)
ut.check_number_range(name="xtick_width", val=xtick_width, **args)
ut.check_number_range(name="xtick_length", val=xtick_length, **args)
args_xtick = dict(xtick_size=xtick_size, xtick_width=xtick_width, xtick_length=xtick_length)
return args_xtick


def check_args_ytick(ytick_size=None, ytick_width=None, ytick_length=None):
"""Check if y tick parameters non-negative float"""
args = dict(accept_none=True, just_int=False, min_val=1)
ut.check_non_negative_number(name="ytick_size", val=ytick_size, **args)
ut.check_non_negative_number(name="ytick_width", val=ytick_width, **args)
ut.check_non_negative_number(name="ytick_length", val=ytick_length, **args)
ut.check_number_range(name="ytick_size", val=ytick_size, **args)
ut.check_number_range(name="ytick_width", val=ytick_width, **args)
ut.check_number_range(name="ytick_length", val=ytick_length, **args)
args_ytick = dict(ytick_size=ytick_size, ytick_width=ytick_width, ytick_length=ytick_length)
return args_ytick

Expand All @@ -86,8 +86,8 @@ def check_seq_color(tmd_seq_color=None, jmd_seq_color=None):
def check_figsize(figsize=None):
""""""
ut.check_tuple(name="figsize", val=figsize, n=2)
ut.check_non_negative_number(name="figsize:width", val=figsize[0], min_val=1, just_int=False)
ut.check_non_negative_number(name="figsize:height", val=figsize[1], min_val=1, just_int=False)
ut.check_number_range(name="figsize:width", val=figsize[0], min_val=1, just_int=False)
ut.check_number_range(name="figsize:height", val=figsize[1], min_val=1, just_int=False)


def check_dict_color(dict_color=None, df_cat=None):
Expand Down Expand Up @@ -124,8 +124,8 @@ def check_parameters(func=None, name_called_func=None, e=None):
# Check heatmap plotting
def check_vmin_vmax(vmin=None, vmax=None):
"""Check if number of cmap colors is valid with given value range"""
ut.check_float(name="vmin", val=vmin, accept_none=True, just_float=False)
ut.check_float(name="vmax", val=vmax, accept_none=True, just_float=False)
ut.check_number_val(name="vmin", val=vmin, accept_none=True, just_int=False)
ut.check_number_val(name="vmax", val=vmax, accept_none=True, just_int=False)
if vmin is not None and vmax is not None and vmin >= vmax:
raise ValueError(f"'vmin' ({vmin}) < 'vmax' ({vmax}) not fulfilled.")

Expand All @@ -144,10 +144,9 @@ def check_ylabel_fontweight(ylabel_fontweight=None, accept_none=True):
if accept_none and ylabel_fontweight is None:
return
name = "ylabel_fontweight"
args = dict(name=name, val=ylabel_fontweight)
list_weights = ['light', 'medium', 'bold']
if type(ylabel_fontweight) in [float, int]:
ut.check_non_negative_number(**args, min_val=0, max_val=1000, just_int=False)
ut.check_number_range(name=name, val=ylabel_fontweight, min_val=0, max_val=1000, just_int=False)
elif isinstance(ylabel_fontweight, str):
if ylabel_fontweight not in list_weights:
error = f"'{name}' ({ylabel_fontweight}) should be one of following: {list_weights}"
Expand Down Expand Up @@ -360,9 +359,9 @@ def profile(self, df_feat=None, y="category", val_col="mean_dif", val_type="coun
# Checking input
# Args checked by Matplotlib: title, legend_kws
# Args checked by internal plotting functions: ylim
ut.check_non_negative_number(name="bar_width", val=bar_width, min_val=0, just_int=False)
ut.check_non_negative_number(name="start", val=start, min_val=0, just_int=True)
ut.check_non_negative_number(name="tmd_area_alpha", val=highlight_alpha, min_val=0, max_val=1, just_int=False)
ut.check_number_range(name="bar_width", val=bar_width, min_val=0, just_int=False)
ut.check_number_range(name="start", val=start, min_val=0, just_int=True)
ut.check_number_range(name="tmd_area_alpha", val=highlight_alpha, min_val=0, max_val=1, just_int=False)
ut.check_bool(name="add_jmd_tmd", val=add_jmd_tmd)
ut.check_bool(name="highlight_tmd_area", val=highlight_tmd_area)
ut.check_bool(name="grid", val=grid)
Expand Down Expand Up @@ -578,9 +577,9 @@ def heatmap(self, df_feat=None, y="subcategory", val_col="mean_dif", val_type="m

# Checking input
# Args checked by Matplotlib: title, cmap, cbar_kws, legend_kws
ut.check_non_negative_number(name="start", val=start, min_val=0, just_int=True)
ut.check_non_negative_number(name="ytick_size", val=ytick_size, accept_none=True, just_int=False, min_val=1)
ut.check_non_negative_number(name="cmap_n_colors", val=cmap_n_colors, min_val=1, accept_none=True, just_int=True)
ut.check_number_range(name="start", val=start, min_val=0, just_int=True)
ut.check_number_range(name="ytick_size", val=ytick_size, accept_none=True, just_int=False, min_val=1)
ut.check_number_range(name="cmap_n_colors", val=cmap_n_colors, min_val=1, accept_none=True, just_int=True)
ut.check_bool(name="add_jmd_tmd", val=add_jmd_tmd)
ut.check_bool(name="add_legend_cat", val=add_legend_cat)
ut.check_dict(name="legend_kws", val=legend_kws, accept_none=True)
Expand Down
8 changes: 4 additions & 4 deletions aaanalysis/cpp/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def check_split_int_args(kwargs_int=None):
"""Check type of given arguments"""
for arg in kwargs_int:
arg_val = kwargs_int[arg]
ut.check_non_negative_number(name=arg, val=arg_val, just_int=False)
ut.check_number_range(name=arg, val=arg_val, just_int=False)


def check_split_list_args(kwargs_list=None, accept_none=True):
Expand Down Expand Up @@ -385,7 +385,7 @@ def feat_matrix(features=None, df_parts=None, df_scales=None, accept_gaps=False,
feat_matrix: array-like or sparse matrix, shape (n_samples, n_features)
Feature values of samples.
"""
ut.check_non_negative_number(name="j_jobs", val=n_jobs, accept_none=True, min_val=1, just_int=True)
ut.check_number_range(name="j_jobs", val=n_jobs, accept_none=True, min_val=1, just_int=True)
if df_scales is None:
df_scales = aaanalysis.data_loader.load_scales_.load_scales()
ut.check_df_scales(df_scales=df_scales)
Expand Down Expand Up @@ -607,10 +607,10 @@ def add_position(df_feat=None, features=None, start=1, tmd_len=20, jmd_n_len=10,
"""
# TODO add sequence, generalize check functions for tmd_len ...
features = ut.check_features(features=features)
ut.check_non_negative_number(name="tmd_len", val=tmd_len, just_int=True, min_val=1)
ut.check_number_range(name="tmd_len", val=tmd_len, just_int=True, min_val=1)
args = dict(jmd_n_len=jmd_n_len, jmd_c_len=jmd_c_len, ext_len=ext_len, start=start)
for name in args:
ut.check_non_negative_number(name=name, val=args[name], just_int=True, min_val=0)
ut.check_number_range(name=name, val=args[name], just_int=True, min_val=0)
sfp = SequenceFeaturePositions()
dict_part_pos = sfp.get_dict_part_pos(tmd_len=tmd_len, **args)
feat_positions = sfp.get_positions(dict_part_pos=dict_part_pos, features=features)
Expand Down
Binary file modified aaanalysis/data_loader/__pycache__/load_dataset_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/data_loader/__pycache__/load_scales_.cpython-39.pyc
Binary file not shown.
8 changes: 4 additions & 4 deletions aaanalysis/data_loader/load_dataset_.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def check_name_of_dataset(name="INFO", folder_in=None):

def check_min_max_val(min_len=None, max_len=None):
"""Check if min_val and max_val are valid and match"""
ut.check_non_negative_number(name="min_len", val=min_len, min_val=1, accept_none=True, just_int=True)
ut.check_non_negative_number(name="max_len", val=max_len, min_val=1, accept_none=True, just_int=True)
ut.check_number_range(name="min_len", val=min_len, min_val=1, accept_none=True, just_int=True)
ut.check_number_range(name="max_len", val=max_len, min_val=1, accept_none=True, just_int=True)
if min_len is None or max_len is None:
return
if isinstance(min_len, int) and isinstance(max_len, int) and min_len > max_len:
Expand All @@ -55,7 +55,7 @@ def check_aa_window_size(aa_window_size=None):
"""Check if aa_window size is a positive odd integer"""
if aa_window_size is None:
return
ut.check_non_negative_number(name="aa_window_size", val=aa_window_size, min_val=1, just_int=True)
ut.check_number_range(name="aa_window_size", val=aa_window_size, min_val=1, just_int=True)
if aa_window_size % 2 == 0:
raise ValueError(f"'aa_window_size' ({aa_window_size}) must be an odd number.")

Expand Down Expand Up @@ -183,7 +183,7 @@ def load_dataset(name: str = "INFO",
"""

check_name_of_dataset(name=name, folder_in=FOLDER_BENCHMARKS)
ut.check_non_negative_number(name="n", val=n, min_val=1, accept_none=True, just_int=True)
ut.check_number_range(name="n", val=n, min_val=1, accept_none=True, just_int=True)
check_non_canonical_aa(non_canonical_aa=non_canonical_aa)
check_min_max_val(min_len=min_len, max_len=max_len)
check_aa_window_size(aa_window_size=aa_window_size)
Expand Down
Loading

0 comments on commit 452f9aa

Please sign in to comment.