Belle II Software development
GroupByRefiner Class Reference
Inheritance diagram for GroupByRefiner:
Refiner

Public Member Functions

 __init__ (self, wrapped_refiner, by=None, exclude_by=None)
 
 refine (self, harvesting_module, crops, groupby_part_name=None, groupby_value=None, *args, **kwds)
 
 __get__ (self, harvesting_module, cls=None)
 
 __call__ (self, harvesting_module, crops=None, *args, **kwds)
 

Public Attributes

 wrapped_refiner = wrapped_refiner
 cached value of the wrapped refiner
 
 by = by
 cached value of the group-by classifier
 
bool exclude_by = exclude_by if exclude_by is not None else self.default_exclude_by
 cached value of the exclude-by classifier
 
 refiner_function = refiner_function
 cached copy of the instance's refiner function
 

Static Public Attributes

bool default_exclude_by = True
 default value of the exclude-by classifier
 

Detailed Description

Refiner for grouping

Definition at line 939 of file refiners.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
wrapped_refiner,
by = None,
exclude_by = None )
Constructor for this refiner

Definition at line 945 of file refiners.py.

948 exclude_by=None):
949 """Constructor for this refiner"""
950 if by is None:
951 by = []
952 ## cached value of the wrapped refiner
953 self.wrapped_refiner = wrapped_refiner
954 ## cached value of the group-by classifier
955 self.by = by
956 ## cached value of the exclude-by classifier
957 self.exclude_by = exclude_by if exclude_by is not None else self.default_exclude_by
958

Member Function Documentation

◆ __call__()

__call__ ( self,
harvesting_module,
crops = None,
* args,
** kwds )
inherited
implementation of the function-call of the Refiner instance r = Refiner() r(harvester) # decoration r(harvester, crops, args, keywords) # refinement

Definition at line 55 of file refiners.py.

55 def __call__(self, harvesting_module, crops=None, *args, **kwds):
56 """implementation of the function-call of the Refiner instance
57 r = Refiner()
58 r(harvester) # decoration
59 r(harvester, crops, args, keywords) # refinement
60 """
61 if crops is None:
62 # Decoration mode
63 harvesting_module.refiners.append(self)
64 return harvesting_module
65 else:
66 # Refining mode
67 return self.refine(harvesting_module, crops, *args, **kwds)
68

◆ __get__()

__get__ ( self,
harvesting_module,
cls = None )
inherited
Getter of the Refiner instance

Definition at line 42 of file refiners.py.

42 def __get__(self, harvesting_module, cls=None):
43 """Getter of the Refiner instance"""
44 if harvesting_module is None:
45 # Class access
46 return self
47 else:
48 # Instance access
49 refine = self.refine
50
51 def bound_call(*args, **kwds):
52 return refine(harvesting_module, *args, **kwds)
53 return bound_call
54

◆ refine()

refine ( self,
harvesting_module,
crops,
groupby_part_name = None,
groupby_value = None,
* args,
** kwds )
Process this grouping

Reimplemented from Refiner.

Definition at line 959 of file refiners.py.

965 **kwds):
966 """Process this grouping"""
967
968 by = self.by
969
970 # A single name to do the group by
971 if isinstance(by, str) or by is None:
972 part_name = by
973 # Wrap it into a list an continue with the general case
974 by = [part_name, ]
975
976 for groupby_spec in by:
977 if groupby_spec is None:
978 # Using empty string as groupby_value to indicate that all values have been selected
979 value = None
980 self.wrapped_refiner(harvesting_module,
981 crops,
982 groupby_part_name=None,
983 groupby_value=value,
984 *args,
985 **kwds)
986 continue
987
988 elif isinstance(groupby_spec, str):
989 part_name = groupby_spec
990 groupby_parts = crops[part_name]
991 unique_values, index_of_values = np.unique(groupby_parts, return_inverse=True)
992 groupby_values = [f" = {value}]" for value in unique_values]
993
994 elif isinstance(groupby_spec, tuple):
995 part_name = groupby_spec[0]
996 cuts = groupby_spec[1]
997
998 groupby_parts = crops[part_name]
999
1000 # Take care of nans
1001 digitization_cuts = list(np.sort(cuts))
1002 if digitization_cuts[-1] != np.inf:
1003 digitization_cuts.append(np.inf)
1004 index_of_values = np.digitize(groupby_parts, digitization_cuts, right=True)
1005
1006 groupby_values = [f"below {digitization_cuts[0]}"]
1007 bin_bounds = list(zip(digitization_cuts[0:], digitization_cuts[1:]))
1008 for lower_bound, upper_bound in bin_bounds:
1009 if lower_bound == upper_bound:
1010 # degenerated bin case
1011 groupby_values.append(f"= {lower_bound}")
1012 elif upper_bound == np.inf:
1013 groupby_values.append(f"above {lower_bound}")
1014 else:
1015 groupby_values.append(f"between {lower_bound} and {upper_bound}")
1016 groupby_values.append("is nan")
1017 assert len(groupby_values) == len(digitization_cuts) + 1
1018
1019 else:
1020 raise ValueError(f"Unknown groupby specification {groupby_spec}")
1021
1022 # Exclude the groupby variable if desired
1023 selected_crops = select_crop_parts(crops, exclude=part_name if self.exclude_by else None)
1024 for index_of_value, groupby_value in enumerate(groupby_values):
1025 indices_for_value = index_of_values == index_of_value
1026 if not np.any(indices_for_value):
1027 continue
1028
1029 filtered_crops = filter_crops(selected_crops, indices_for_value)
1030
1031 self.wrapped_refiner(harvesting_module,
1032 filtered_crops,
1033 groupby_part_name=part_name,
1034 groupby_value=groupby_value,
1035 *args,
1036 **kwds)
1037
1038

Member Data Documentation

◆ by

by = by

cached value of the group-by classifier

Definition at line 955 of file refiners.py.

◆ default_exclude_by

bool default_exclude_by = True
static

default value of the exclude-by classifier

Definition at line 943 of file refiners.py.

◆ exclude_by

bool exclude_by = exclude_by if exclude_by is not None else self.default_exclude_by

cached value of the exclude-by classifier

Definition at line 957 of file refiners.py.

◆ refiner_function

refiner_function = refiner_function
inherited

cached copy of the instance's refiner function

Definition at line 40 of file refiners.py.

◆ wrapped_refiner

wrapped_refiner = wrapped_refiner

cached value of the wrapped refiner

Definition at line 953 of file refiners.py.


The documentation for this class was generated from the following file: