def assertSliceResult(self, name, features_dict, columns, features, expected): spec = slicer.SingleSliceSpec(columns=columns, features=features) msg = 'Test case %s: slice on columns %s, features %s' % (name, columns, features) six.assertCountEqual( self, expected, slicer.get_slices_for_features_dict(features_dict, [spec]), msg)
def process(self, element: types.Extracts) -> List[types.Extracts]: fpl = element.get(constants.FEATURES_PREDICTIONS_LABELS_KEY) if not fpl: raise RuntimeError( 'FPL missing, Please ensure Predict() was called.') if not isinstance(fpl, types.FeaturesPredictionsLabels): raise TypeError( 'Expected FPL to be instance of FeaturesPredictionsLabel. FPL was: ' '%s of type %s' % (str(fpl), type(fpl))) features = fpl.features slices = list( slicer.get_slices_for_features_dict(features, self._slice_spec)) # Make a a shallow copy, so we don't mutate the original. element_copy = copy.copy(element) element_copy[constants.SLICE_KEY_TYPES_KEY] = slices # Add a list of stringified slice keys to be materialized to output table. if self._materialize: element_copy[constants.SLICE_KEYS_KEY] = types.MaterializedColumn( name=constants.SLICE_KEYS_KEY, value=(list( slicer.stringify_slice_key(x).encode('utf-8') for x in slices))) return [element_copy]
def process(self, element): features = element.features slice_count = 0 for slice_key in slicer.get_slices_for_features_dict( features, self._slice_spec): slice_count += 1 yield (slice_key, element) self._num_slices_generated_per_instance.update(slice_count) self._post_slice_num_instances.inc(slice_count)
def process(self, element): fpl = element.extracts[constants.FEATURES_PREDICTIONS_LABELS_KEY] features = fpl.features slice_count = 0 for slice_key in slicer.get_slices_for_features_dict( features, self._slice_spec): slice_count += 1 yield (slice_key, fpl) self._num_slices_generated_per_instance.update(slice_count) self._post_slice_num_instances.inc(slice_count)
def testGetSlicesForFeaturesDictMultipleSingleSliceSpecs(self): features_dict = self._makeFeaturesDict({ 'gender': ['f'], 'age': [5], 'interest': ['cars'] }) spec_overall = slicer.SingleSliceSpec() spec_age = slicer.SingleSliceSpec(columns=['age']) spec_age4 = slicer.SingleSliceSpec(features=[('age', 4)]) spec_age5_gender = slicer.SingleSliceSpec( columns=['gender'], features=[('age', 5)]) slice_spec = [spec_overall, spec_age, spec_age4, spec_age5_gender] expected = [(), (('age', 5),), (('age', 5), ('gender', 'f'))] self.assertItemsEqual( expected, slicer.get_slices_for_features_dict(features_dict, slice_spec))
def process(self, element): fpl = element.extracts.get(constants.FEATURES_PREDICTIONS_LABELS_KEY) if not fpl: raise RuntimeError( 'FPL missing, Please ensure Predict() was called.') if not isinstance(fpl, load.FeaturesPredictionsLabels): raise TypeError( 'Expected FPL to be instance of FeaturesPredictionsLabel. FPL was: ' '%s of type %s' % (str(fpl), type(fpl))) features = fpl.features slices = list( slicer.get_slices_for_features_dict(features, self._slice_spec)) # Make a a shallow copy, so we don't mutate the original. element_copy = (element.create_copy_with_shallow_copy_of_extracts()) element_copy.extracts[constants.SLICE_KEYS] = slices # Add a list of stringified slice keys to be materialized to output table. element_copy.extracts[ constants.SLICE_KEYS_MATERIALIZED] = types.MaterializedColumn( name=constants.SLICE_KEYS_MATERIALIZED, value=(list(slicer.stringify_slice_key(x) for x in slices))) return [element_copy]