From 1168c492977f329a041b1c1a744d6f728a7cdd49 Mon Sep 17 00:00:00 2001 From: Yaroslav Nikitenko Date: Thu, 4 Nov 2021 22:46:02 +0300 Subject: [PATCH] Rename ReduceBinContent to MapBins. Rename its kwarg transform to seq. --- docs/source/flow.rst | 2 +- lena/flow/__init__.py | 4 ++-- lena/flow/split_into_bins.py | 26 +++++++++++++------------- lena/math/meshes.py | 4 +--- lena/structures/histogram.py | 2 +- tests/flow/test_split_into_bins.py | 16 ++++++++-------- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/docs/source/flow.rst b/docs/source/flow.rst index 4bf397c..dc4edc7 100644 --- a/docs/source/flow.rst +++ b/docs/source/flow.rst @@ -53,7 +53,7 @@ Flow .. currentmodule:: lena.flow.split_into_bins .. autosummary:: - ReduceBinContent + MapBins SplitIntoBins TransformBins cell_to_string diff --git a/lena/flow/__init__.py b/lena/flow/__init__.py index ba8f3ee..ef214d5 100644 --- a/lena/flow/__init__.py +++ b/lena/flow/__init__.py @@ -10,7 +10,7 @@ from .progress import Progress from .selectors import Not, Selector from .split_into_bins import ( - SplitIntoBins, ReduceBinContent, TransformBins, + SplitIntoBins, MapBins, TransformBins, get_example_bin, ) from .zip import Zip @@ -44,7 +44,7 @@ 'RunIf', # split into bins 'SplitIntoBins', - 'ReduceBinContent', + 'MapBins', 'TransformBins', 'get_example_bin', ] diff --git a/lena/flow/split_into_bins.py b/lena/flow/split_into_bins.py index fbcdaad..237ac22 100644 --- a/lena/flow/split_into_bins.py +++ b/lena/flow/split_into_bins.py @@ -190,7 +190,7 @@ def run(self, flow): yield (hist, ana_context) -class ReduceBinContent(object): +class MapBins(object): """Transform bin content of histograms. This class is used when histogram bins contain complex structures. @@ -199,20 +199,20 @@ class ReduceBinContent(object): we shall create 3 histograms corresponding to vector's components. """ - def __init__(self, select, transform, drop_bins_context=True): + def __init__(self, select, seq, drop_bins_context=True): """*Select* determines which types should be transformed. The types must be given in a ``list`` (not a tuple) or as a general :class:`.Selector`. Example: ``select=[lena.math.vector3, list]``. - *transform* is a *Sequence* or element applied to bin contents. - If *transform* is not a :class:`.Sequence` + *seq* is a *Sequence* or element applied to bin contents. + If *seq* is not a :class:`.Sequence` or an element with *run* method, it is converted to a :class:`.Sequence`. - Example: ``transform=Split([X(), Y(), Z()])`` + Example: ``seq=Split([X(), Y(), Z()])`` (provided that you have X, Y, Z variables). - :class:`.ReduceBinContent` creates histograms + :class:`.MapBins` creates histograms that may be plotted, and their bins contain only data without context. By default, context of all bins except one is not used. @@ -232,16 +232,16 @@ def __init__(self, select, transform, drop_bins_context=True): ) self._selector = select - if not lena.core.is_run_el(transform): + if not lena.core.is_run_el(seq): try: - transform = lena.core.Sequence(transform) + seq = lena.core.Sequence(seq) except lena.core.LenaTypeError: raise lena.core.LenaTypeError( - "transform must be a Sequence or convertible to that, " + "seq must be a Sequence or convertible to that, " "or an element with run method; " - "{} provided".format(transform) + "{} provided".format(seq) ) - self._transform = transform + self._seq = seq self._drop_bins_context = bool(drop_bins_context) def run(self, flow): @@ -272,7 +272,7 @@ def run(self, flow): # bins should be transformed. # Several iterations can happen, in principle. generators = _MdSeqMap( - lambda cell: copy.deepcopy(self._transform).run([cell]), + lambda cell: copy.deepcopy(self._seq).run([cell]), hist.bins ) for new_bins in generators: @@ -345,7 +345,7 @@ def __init__(self, seq, arg_var, edges): The final histogram may contain vectors, histograms and any other data the analysis produced. To plot them, one can extract vector components with e.g. - :class:`.ReduceBinContent`. + :class:`.MapBins`. If bin contents are histograms, they can be yielded one by one with :class:`.TransformBins`. diff --git a/lena/math/meshes.py b/lena/math/meshes.py index 2297402..f07d11f 100644 --- a/lena/math/meshes.py +++ b/lena/math/meshes.py @@ -1,6 +1,4 @@ """mesh, md_map, flatten work with multidimensional data.""" -from __future__ import print_function - from itertools import count, islice @@ -84,7 +82,7 @@ def md_map(f, array): arr_0 = array[0] # Tuples can be (data, context) pairs, - # they should not be expanded in ReduceBinContent. + # they should not be expanded in MapBins # if isinstance(arr_0, (list, tuple)): if isinstance(arr_0, list): return [md_map(f, ar) for ar in array] diff --git a/lena/structures/histogram.py b/lena/structures/histogram.py index c8db0d7..0922e4e 100644 --- a/lena/structures/histogram.py +++ b/lena/structures/histogram.py @@ -143,7 +143,7 @@ def __eq__(self, other): """Two histograms are equal, if and only if they have equal bins and equal edges. - If *other* is not a :class:`.histogram`, return `False`. + If *other* is not a :class:`.histogram`, return ``False``. Note that floating numbers should be compared approximately (using :func:`math.isclose`). diff --git a/tests/flow/test_split_into_bins.py b/tests/flow/test_split_into_bins.py index edbc6e8..55804db 100644 --- a/tests/flow/test_split_into_bins.py +++ b/tests/flow/test_split_into_bins.py @@ -8,7 +8,7 @@ from lena.core import Split, FillCompute, Sequence from lena.variables import Variable from lena.structures import histogram -from lena.flow import SplitIntoBins, TransformBins, ReduceBinContent +from lena.flow import SplitIntoBins, TransformBins, MapBins from lena.flow.split_into_bins import _iter_bins_with_edges from tests.examples.fill_compute import Count, Sum @@ -108,15 +108,15 @@ def test_transform_bins(): def test_reduce_bin_content(): ## test init with pytest.raises(lena.core.LenaTypeError): - ReduceBinContent(1, ()) + MapBins(1, ()) # this works - ReduceBinContent(lambda _: True, ()) + MapBins(lambda _: True, ()) with pytest.raises(lena.core.LenaTypeError): - ReduceBinContent(lambda _: True, 1) + MapBins(lambda _: True, 1) # not selected flow passes unchanged data = [1, (2, {}), (histogram([0, 1], [1]), {})] - r = ReduceBinContent(lena.math.vector3, lambda v: v.x) + r = MapBins(lena.math.vector3, lambda v: v.x) assert list(r.run(data)) == data # empty context data = [(histogram([0, 1], [lena.math.vector3([0.5, 0, 1])]), {})] @@ -143,7 +143,7 @@ def test_reduce_bin_content(): }, } X = Variable("x", lambda v: v.x) - r = ReduceBinContent(lena.math.vector3, X, drop_bins_context=False) + r = MapBins(lena.math.vector3, X, drop_bins_context=False) data = copy.deepcopy(data_template) results = list(r.run(data))[0] assert results[0] == histogram([0, 1], bins=[0.5]) @@ -217,9 +217,9 @@ def test_split_into_bins(): # first element is Sum, second is Count assert list(res_bins) == [[0, 1, 2, 3], [2, 1, 1, 1]] - # ReduceBinContent works + # MapBins works ## It was a transform kwarg, but it seems not needed. - t = ReduceBinContent(int, lambda x: x+1) + t = MapBins(int, lambda x: x+1) edges = [0, 1, 2, 3, 4] arg_var = Variable("x", lambda x: x) s = Sequence(SplitIntoBins(seq, arg_var, edges), t)