def merge(self): if not self._is_already_merged: if len(self._unique_states_matrix_ids) > 1: if not self._is_row_struct: convert_to_1d_arrays = map(lambda xx: xx._as_1d_array, self) len_of_1d_arrays = map(len, convert_to_1d_arrays) sector_positions = np.cumsum([0] + len_of_1d_arrays) sector_positions_slices = map(lambda xx: slice(*xx), list(tools.ngram(sector_positions, 2))) join_all_states_arrays = np.concatenate(tuple(convert_to_1d_arrays), axis=0) u, i = np.unique(join_all_states_arrays, return_inverse=True) self._idx_trans_vectors = map(lambda xx: IndexTransformVector(i[xx]), sector_positions_slices) self._new_states_matrix = StatesMatrix(data=u, eval_cls=self._eval_cls) # TODO: self._is_row_struct == True else: self._is_already_merged = True self._is_already_updated = True pass else: # Case of len(self._unique_states_matrix_ids) <= 1, # it means states_mats has only one kind of states matrix # in this case, we don't need to do anything about merge or update self._is_already_merged = True self._is_already_updated = True return self
def merge(self): if not self._executed_merge: if len(self._unique_states_array_ids) > 1: states_array_lens = map(lambda xx:len(xx._states_array),self) sector_position = map(lambda xx:slice(*xx),list(tools.ngram(np.cumsum([0] + states_array_lens),2))) join_all_states_arrays = np.concatenate(tuple(map(lambda xx:xx._states_array,self)),axis=0) u,i = np.unique(join_all_states_arrays,return_inverse=True) ptrs_transforms = map(lambda xx:i[xx],sector_position) self._new_states_array = StatesArray(data=u, eval_cls=self._eval_cls) self._new_states_dict = StatesDictionary(states_array=self._new_states_array) self._ptrs_transforms = ptrs_transforms else: self._new_states_array = self[0]._states_array self._new_states_dict = StatesDictionary(states_array=self._new_states_array) self._executed_merge = True return self
def _1d_ngram(self, n): assert self._is_1d ngram_results = list(tools.ngram(self.flatten(), n)) return type(self)(data=ngram_results, **self._reconstruct_kwargs)