Skip to content

Commit

Permalink
Rename ReduceBinContent to MapBins. Rename its kwarg transform to seq.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed Nov 4, 2021
1 parent a0e10ff commit 1168c49
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/source/flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Flow
.. currentmodule:: lena.flow.split_into_bins
.. autosummary::

ReduceBinContent
MapBins
SplitIntoBins
TransformBins
cell_to_string
Expand Down
4 changes: 2 additions & 2 deletions lena/flow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,7 +44,7 @@
'RunIf',
# split into bins
'SplitIntoBins',
'ReduceBinContent',
'MapBins',
'TransformBins',
'get_example_bin',
]
26 changes: 13 additions & 13 deletions lena/flow/split_into_bins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
Expand Down
4 changes: 1 addition & 3 deletions lena/math/meshes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""mesh, md_map, flatten work with multidimensional data."""
from __future__ import print_function

from itertools import count, islice


Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion lena/structures/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
16 changes: 8 additions & 8 deletions tests/flow/test_split_into_bins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])]), {})]
Expand All @@ -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])
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1168c49

Please sign in to comment.