def test_return_parameters_from_template(self): atom_pairs = [[0, 1], [10, 14]] atom_pair_op = op.MSMBFeaturizerCV("atom_pairs", AtomPairsFeaturizer, topology=self.topology, pair_indices=atom_pairs) # little trick. We just pretend the atom_pairs_op is a function we want # to use it cannot be stored though, but for from_template it is enough params = NetCDFPlus.get_value_parameters( atom_pair_op(self.traj_topology[0])) assert params['var_type'] == 'numpy.float32' assert params['simtk_unit'] is None assert params['dimensions'] == tuple([2])
def test_return_parameters_from_template(self): atom_pairs = [[0, 1], [10, 14]] atom_pair_op = op.MSMBFeaturizerCV( "atom_pairs", AtomPairsFeaturizer, topology=self.topology, pair_indices=atom_pairs) # little trick. We just predent the atom_pairs_op is a function we want # to use it cannot be stored though, but for from_template it is enough params = NetCDFPlus.get_value_parameters( atom_pair_op(self.traj_topology[0])) assert params['var_type'] == 'numpy.float32' assert params['simtk_unit'] is None assert params['dimensions'] == tuple([2])
def add_cv(self, cv, template, allow_incomplete=None, chunksize=None): """ Parameters ---------- cv : :obj:`openpathsampling.CollectiveVariable` template : :obj:`openpathsampling.engines.BaseSnapshot` chunksize : int allow_incomplete : bool Returns ------- :obj:`openpathsampling.netcdfplus.ObjectStore` int """ if cv in self.cv_list: return self.cv_list[cv] if allow_incomplete is None: allow_incomplete = cv.diskcache_allow_incomplete if chunksize is None: chunksize = cv.diskcache_chunksize if template is None: template = cv.diskcache_template time_reversible = cv.cv_time_reversible if not time_reversible: # in the rare case of not time_reversible we use store partial allow_incomplete = True if not allow_incomplete: # in complete mode we force chunk size one to match it to snapshots chunksize = self.default_store_chunk_size # determine value type and shape params = NetCDFPlus.get_value_parameters(cv(template)) shape = params['dimensions'] if shape is None: chunksizes = None else: chunksizes = tuple(params['dimensions']) cv_idx = self.storage.cvs.index[cv.__uuid__] store = SnapshotValueStore(time_reversible=time_reversible, allow_incomplete=allow_incomplete, chunksize=chunksize) store_name = SnapshotWrapperStore._get_cv_name(cv_idx) self.storage.create_store(store_name, store, False) if store.allow_incomplete: # we are not using the .initialize function here since we # only have one variable and only here know its shape self.storage.create_dimension(store.prefix, 0) if shape is not None: shape = tuple(list(shape)) chunksizes = tuple([chunksize] + list(chunksizes)) else: shape = tuple() chunksizes = tuple([chunksize]) # create the variable store.create_variable( 'value', var_type=params['var_type'], dimensions=shape, chunksizes=chunksizes, simtk_unit=params['simtk_unit'], ) store.create_variable('index', 'index') else: chunksize = self.default_store_chunk_size # todo: Remove once netcdf4/python bug in Unidata/netcdf4-python#566 has # been resolved. It seems that so far, after data has been written to a # dimension you have to use chunksize 1 for any new variables in that # dimension Even if the other variables all share the same chunksize chunksize = 1 if shape is not None: shape = tuple(['snapshots'] + list(shape)) chunksizes = tuple([chunksize] + list(chunksizes)) else: shape = tuple(['snapshots']) chunksizes = tuple([chunksize]) # create the variable store.storage.create_variable( store_name + '_value', var_type=params['var_type'], dimensions=shape, chunksizes=chunksizes, simtk_unit=params['simtk_unit'], ) setattr(store, 'value', self.storage.vars[store_name + '_value']) store.initialize() store_idx = int(len(self.storage.dimensions['cvcache'])) self.cv_list[cv] = (store, store_idx) self.storage.vars['cvcache'][store_idx] = store # use the cache and function of the CV to fill the store when it is made if not allow_incomplete: indices = self.vars['uuid'][:] for pos, idx in enumerate(indices): proxy = LoaderProxy(self.storage.snapshots, idx) value = cv._cache_dict._get(proxy) if value is None: # not in cache so compute it if possible if cv._eval_dict: value = cv._eval_dict([proxy])[0] else: value = None if value is not None: store.vars['value'][pos] = value store.cache[pos] = value cv.set_cache_store(store) return store, store_idx
def add_cv(self, cv, template, allow_incomplete=None, chunksize=None): """ Parameters ---------- cv : :obj:`openpathsampling.CollectiveVariable` template : :obj:`openpathsampling.engines.BaseSnapshot` chunksize : int allow_incomplete : bool Returns ------- :obj:`openpathsampling.netcdfplus.ObjectStore` int """ if cv in self.cv_list: return self.cv_list[cv] if allow_incomplete is None: allow_incomplete = cv.diskcache_allow_incomplete if chunksize is None: chunksize = cv.diskcache_chunksize if template is None: template = cv.diskcache_template time_reversible = cv.cv_time_reversible if not time_reversible: # in the rare case of not time_reversible we use store partial allow_incomplete = True if not allow_incomplete: # in complete mode we force chunk size one to match it to snapshots chunksize = self.default_store_chunk_size # determine value type and shape params = NetCDFPlus.get_value_parameters(cv(template)) shape = params['dimensions'] if shape is None: chunksizes = None else: chunksizes = tuple(params['dimensions']) cv_idx = self.storage.cvs.index[cv.__uuid__] store = SnapshotValueStore( time_reversible=time_reversible, allow_incomplete=allow_incomplete, chunksize=chunksize ) store_name = SnapshotWrapperStore._get_cv_name(cv_idx) self.storage.create_store(store_name, store, False) if store.allow_incomplete: # we are not using the .initialize function here since we # only have one variable and only here know its shape self.storage.create_dimension(store.prefix, 0) if shape is not None: shape = tuple(list(shape)) chunksizes = tuple([chunksize] + list(chunksizes)) else: shape = tuple() chunksizes = tuple([chunksize]) # create the variable store.create_variable( 'value', var_type=params['var_type'], dimensions=shape, chunksizes=chunksizes, simtk_unit=params['simtk_unit'], ) store.create_variable('index', 'index') else: chunksize = self.default_store_chunk_size # todo: Remove once netcdf4/python bug in Unidata/netcdf4-python#566 has # been resolved. It seems that so far, after data has been written to a # dimension you have to use chunksize 1 for any new variables in that # dimension Even if the other variables all share the same chunksize chunksize = 1 if shape is not None: shape = tuple(['snapshots'] + list(shape)) chunksizes = tuple([chunksize] + list(chunksizes)) else: shape = tuple(['snapshots']) chunksizes = tuple([chunksize]) # create the variable store.storage.create_variable( store_name + '_value', var_type=params['var_type'], dimensions=shape, chunksizes=chunksizes, simtk_unit=params['simtk_unit'], ) setattr(store, 'value', self.storage.vars[store_name + '_value']) store.initialize() store_idx = int(len(self.storage.dimensions['cvcache'])) self.cv_list[cv] = (store, store_idx) self.storage.vars['cvcache'][store_idx] = store # use the cache and function of the CV to fill the store when it is made if not allow_incomplete: indices = self.vars['uuid'][:] for pos, idx in enumerate(indices): proxy = LoaderProxy(self.storage.snapshots, idx) value = cv._cache_dict._get(proxy) if value is None: # not in cache so compute it if possible if cv._eval_dict: value = cv._eval_dict([proxy])[0] else: value = None if value is not None: store.vars['value'][pos] = value store.cache[pos] = value cv.set_cache_store(store) return store, store_idx