def test_features_df_serialization(): superpixels = generate_random_voronoi((100,200), 200) rag = Rag( superpixels ) # For simplicity, just make values identical to superpixels values = superpixels.astype(np.float32) feature_names = ['standard_edge_mean', 'standard_edge_minimum', 'standard_edge_maximum'] features_df = rag.compute_features(values, feature_names) tmpdir = tempfile.mkdtemp() try: with h5py.File( tmpdir + '/' + 'test_dataframe.h5', 'w' ) as f: group = f.create_group('test_dataframe') dataframe_to_hdf5( group, features_df ) with h5py.File( tmpdir + '/' + 'test_dataframe.h5', 'r' ) as f: group = f['test_dataframe'] readback_features_df = dataframe_from_hdf5( group ) assert (readback_features_df.columns.values == features_df.columns.values).all() assert (readback_features_df.values.shape == features_df.values.shape) assert (readback_features_df.values == features_df.values).all() finally: shutil.rmtree(tmpdir)
def _serialize(self, group, name, slot): if slot.level == 0: # Is the cache up-to-date? # if not, we'll just return (don't recompute the classifier just to save it) slot_index = slot.operator.index(slot) inner_op = self.cache.getLane(slot_index) if inner_op._dirty: return dataframe = inner_op.Output.value # Can be None if the user didn't actually compute features yet. if dataframe is None: return df_group = group.create_group(name) dataframe_to_hdf5(df_group, dataframe) else: subgroup = group.create_group(name) for i, subslot in enumerate(slot): subname = self.subname.format(i) self._serialize(subgroup, subname, slot[i])
def _serialize(self, group, name, slot): if slot.level == 0: # Is the cache up-to-date? # if not, we'll just return (don't recompute the classifier just to save it) slot_index = slot.operator.index(slot) inner_op = self.cache.getLane( slot_index ) if inner_op._dirty: return dataframe = inner_op.Output.value # Can be None if the user didn't actually compute features yet. if dataframe is None: return df_group = group.create_group( name ) dataframe_to_hdf5( df_group, dataframe ) else: subgroup = group.create_group(name) for i, subslot in enumerate(slot): subname = self.subname.format(i) self._serialize(subgroup, subname, slot[i])