diff --git a/.gitignore b/.gitignore index ad93a21..f3e8254 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ src/hydrodiy/gis/tests/*.cpg src/hydrodiy/gis/tests/*.dbf src/hydrodiy/gis/tests/*.shp src/hydrodiy/gis/tests/*.shx +src/hydrodiy/plot/tests/images/ +src/hydrodiy/stat/tests/__pycache__/ src/hydrodiy/io/tests/script_test* src/hydrodiy/gis/tests/grid_test.bil src/hydrodiy/data/c_hydrodiy_data.c diff --git a/src/hydrodiy/__init__.py b/src/hydrodiy/__init__.py index 4bb1bc2..4e4ac12 100644 --- a/src/hydrodiy/__init__.py +++ b/src/hydrodiy/__init__.py @@ -8,16 +8,17 @@ if sys.version_info > (3, 0): PYVERSION = 3 + def has_c_module(name, raise_error=True): name = f"c_hydrodiy_{name}" out = importlib.util.find_spec(name) - if not out is None: + if out is not None: return True else: if raise_error: - raise ImportError(f"C module {name} is "+\ - "not available, please run python setup.py build") + raise ImportError(f"C module {name} is " + + "not available, please " + + "run python setup.py build") else: return False - diff --git a/src/hydrodiy/io/hyruns.py b/src/hydrodiy/io/hyruns.py index c66924a..ecf7f75 100644 --- a/src/hydrodiy/io/hyruns.py +++ b/src/hydrodiy/io/hyruns.py @@ -300,6 +300,13 @@ def from_cartesian_product(self, **kwargs): + f" an int or a string, got {type(v)}." raise TypeError(errmsg) + # convert numpy to list + try: + v2 = v2.tolist() + except AttributeError: + pass + + # Store options sk = str(k) self.options[sk] = v2 diff --git a/src/hydrodiy/io/iutils.py b/src/hydrodiy/io/iutils.py index 9ecb390..6b74d32 100644 --- a/src/hydrodiy/io/iutils.py +++ b/src/hydrodiy/io/iutils.py @@ -126,14 +126,14 @@ def __init__(self, logger, self.tab_length = tab_length def get_separator(self, nsep, sep): - return sep*nsep + return sep * nsep def add_tab(self, msg, ntab): if ntab == 0: return msg - tab_space = " "*self.tab_length*ntab - return tab_space+msg + tab_space = " " * self.tab_length * ntab + return tab_space + msg def error(self, msg, ntab=0, *args, **kwargs): msg = self.add_tab(msg, ntab) diff --git a/src/hydrodiy/io/tests/test_hyio_hyruns.py b/src/hydrodiy/io/tests/test_hyio_hyruns.py index 43333ab..c125960 100644 --- a/src/hydrodiy/io/tests/test_hyio_hyruns.py +++ b/src/hydrodiy/io/tests/test_hyio_hyruns.py @@ -311,7 +311,8 @@ def test_option_dict(): def test_option_file(): opm = hyruns.OptionManager(bidule="test") - opm.from_cartesian_product(v1=["a", "b"], v2=[1, 2, 3]) + opm.from_cartesian_product(v1=pd.Series(["a", "b"]), + v2=np.arange(1, 4)) f = TESTS_DIR / "opm.json" if f.exists(): diff --git a/src/hydrodiy/plot/putils.py b/src/hydrodiy/plot/putils.py index 0f6d086..f2da89c 100644 --- a/src/hydrodiy/plot/putils.py +++ b/src/hydrodiy/plot/putils.py @@ -18,6 +18,7 @@ import numpy as np import pandas as pd +from hydrodiy.data.dutils import get_value_from_kwargs from hydrodiy.stat import sutils HAS_CYCLER = False @@ -571,6 +572,7 @@ def scattercat(ax, x, y, z, ncats=5, cuts=None, cmap="PiYG", fmt="0.2f", eps=1e-5, show_extremes_in_legend=True, + show_counts_in_legend=False, *args, **kwargs): """ Draw a scatter plot using different colors or markersize depending on categories defined by z. Be careful when z has a lot of zeros, @@ -611,7 +613,9 @@ def scattercat(ax, x, y, z, ncats=5, cuts=None, cmap="PiYG", Number format to be used in labels show_extremes_in_legend : bool Show lowest and highest bounds in legend. If False, report in - legend. + legend. Can be abbreviated as 'sel'. + show_counts_in_legend : bool + Show count of categoriges in legend. Can be abbreviated as 'scl'. eps : float Tolerance on min and max value if using cuts. args, kwargs @@ -633,18 +637,24 @@ def scattercat(ax, x, y, z, ncats=5, cuts=None, cmap="PiYG", Series containing the category number for each item """ # Check inputs - if "m" in kwargs: - markers = kwargs["m"] - kwargs.pop("m") + markers = get_value_from_kwargs(kwargs, "markers", "m", + markers) - if "ms" in kwargs: - markersizes = kwargs["ms"] - kwargs.pop("ms") + markersizes = get_value_from_kwargs(kwargs, "markersizes", + "ms", markersizes) - if "ec" in kwargs: - edgecolors = kwargs["ec"] - kwargs.pop("ec") + edgecolors = get_value_from_kwargs(kwargs, "edgecolors", + "ec", edgecolors) + show_extremes_in_legend = get_value_from_kwargs(kwargs, + "show_extremes_in_legend", + "sel", + show_extremes_in_legend) + + show_counts_in_legend = get_value_from_kwargs(kwargs, + "show_counts_in_legend", + "scl", + show_counts_in_legend) if not len(x) == len(y): errmess = "Expected x and y of same length, got "\ + f"len(x)={len(x)}, len(y)={len(y)}" @@ -771,6 +781,9 @@ def notlist(x): continue label = labels[icat] + if show_counts_in_legend: + label = f"{label} ({idx.sum()})" + marker = markers[icat] markersize = markersizes[icat] col = colors[icat] diff --git a/src/hydrodiy/plot/tests/test_hyplot_putils.py b/src/hydrodiy/plot/tests/test_hyplot_putils.py index d8c3347..8caf917 100644 --- a/src/hydrodiy/plot/tests/test_hyplot_putils.py +++ b/src/hydrodiy/plot/tests/test_hyplot_putils.py @@ -321,6 +321,17 @@ def test_scattercat(): fig.savefig(fp) +def test_scattercat_counts(): + x, y, z = np.random.uniform(0, 1, size=(100, 3)).T + fig, ax = plt.subplots() + plotted, cats = putils.scattercat(ax, x, y, z, 5, \ + markersizes=np.linspace(30, 70, 5), \ + alphas=0.6, scl=True) + ax.legend(loc=2, title="categories") + fp = FIMG / "scattercat_counts.png" + fig.savefig(fp) + + def test_scattercat_nocmap(): """ Test categorical scatter plot with no cmap""" x, y, z = np.random.uniform(0, 1, size=(100, 3)).T diff --git a/src/hydrodiy/plot/violinplot.py b/src/hydrodiy/plot/violinplot.py index b349907..e8d5379 100644 --- a/src/hydrodiy/plot/violinplot.py +++ b/src/hydrodiy/plot/violinplot.py @@ -4,7 +4,6 @@ import numpy as np import pandas as pd from scipy.stats import gaussian_kde -from numpy.polynomial import Chebyshev import matplotlib.pyplot as plt from matplotlib.patches import Polygon