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

Public Member Functions

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

Public Attributes

 wrapped_refiner
 cached value of the wrapped refiner
 
 by
 cached value of the group-by classifier
 
 exclude_by
 cached value of the exclude-by classifier
 

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__()

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

Reimplemented from 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
953 self.wrapped_refiner = wrapped_refiner
954
955 self.by = by
956
957 self.exclude_by = exclude_by if exclude_by is not None else self.default_exclude_by
958

Member Function Documentation

◆ refine()

def 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

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

exclude_by

cached value of the exclude-by classifier

Definition at line 957 of file refiners.py.

◆ 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: