Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ibstate = numpy.digitize([random.random()], self.next_iter_bstate_cprobs)
basis_state = self.next_iter_bstates[ibstate[0]]
initial_state = self.data_manager.create_initial_states(1, n_iter=self.n_iter+1)[0]
initial_state.iter_created = self.n_iter
initial_state.basis_state_id = basis_state.state_id
initial_state.istate_status = InitialState.ISTATE_STATUS_PENDING
if self.do_gen_istates:
log.debug('generating new initial state from basis state {!r}'.format(basis_state))
initial_state.istate_type = InitialState.ISTATE_TYPE_GENERATED
futures.add(self.work_manager.submit(wm_ops.gen_istate, args=(basis_state, initial_state)))
else:
log.debug('using basis state {!r} directly'.format(basis_state))
initial_state.istate_type = InitialState.ISTATE_TYPE_BASIS
initial_state.pcoord = basis_state.pcoord.copy()
initial_state.istate_status = InitialState.ISTATE_STATUS_PREPARED
self.we_driver.avail_initial_states[initial_state.state_id] = initial_state
updated_states.append(initial_state)
self.data_manager.update_initial_states(updated_states, n_iter=self.n_iter+1)
return futures
system = westpa.rc.get_system_driver()
initial_states = sorted(initial_states,key=attrgetter('state_id'))
if not initial_states:
return
with self.lock:
n_iter = n_iter or self.current_iteration
ibstate_group = self.find_ibstate_group(n_iter)
state_ids = [state.state_id for state in initial_states]
index_entries = ibstate_group['istate_index'][state_ids]
pcoord_vals = numpy.empty((len(initial_states), system.pcoord_ndim), dtype=system.pcoord_dtype)
for i, initial_state in enumerate(initial_states):
index_entries[i]['iter_created'] = initial_state.iter_created
index_entries[i]['iter_used'] = initial_state.iter_used or InitialState.ISTATE_UNUSED
index_entries[i]['basis_state_id'] = initial_state.basis_state_id if initial_state.basis_state_id is not None else -1
index_entries[i]['istate_type'] = initial_state.istate_type or InitialState.ISTATE_TYPE_UNSET
index_entries[i]['istate_status'] = initial_state.istate_status or InitialState.ISTATE_STATUS_PENDING
pcoord_vals[i] = initial_state.pcoord
ibstate_group['istate_index'][state_ids] = index_entries
ibstate_group['istate_pcoord'][state_ids] = pcoord_vals
with self.lock:
n_iter = n_iter or self.current_iteration
ibstate_group = self.get_iter_group(n_iter)['ibstates']
istate_ids = {-int(segment.parent_id+1) for segment in segments if segment.parent_id < 0}
sorted_istate_ids = sorted(istate_ids)
if not sorted_istate_ids:
return []
istate_rows = ibstate_group['istate_index'][sorted_istate_ids][...]
istate_pcoords = ibstate_group['istate_pcoord'][sorted_istate_ids][...]
istates = []
for state_id, state, pcoord in zip(sorted_istate_ids, istate_rows, istate_pcoords):
istate = InitialState(state_id=state_id, basis_state_id=int(state['basis_state_id']),
iter_created=int(state['iter_created']), iter_used=int(state['iter_used']),
istate_type=int(state['istate_type']), pcoord=pcoord.copy())
istates.append(istate)
return istates
len_index = len(istate_index)
first_id = 0
else:
first_id = len(istate_index)
len_index = len(istate_index) + n_states
istate_index.resize((len_index,))
istate_pcoords = ibstate_group['istate_pcoord']
istate_pcoords.resize((len_index,system.pcoord_ndim))
index_entries = istate_index[first_id:len_index]
new_istates = []
for irow, row in enumerate(index_entries):
row['iter_created'] = n_iter
row['istate_status'] = InitialState.ISTATE_STATUS_PENDING
new_istates.append(InitialState(state_id=first_id+irow, basis_state_id=None,
iter_created=n_iter, istate_status=InitialState.ISTATE_STATUS_PENDING))
istate_index[first_id:len_index] = index_entries
return new_istates
chunksize = self.table_scan_chunksize
states = []
istart = 0
while istart < n_index_entries and len(states) < n_states:
istop = min(istart+chunksize, n_index_entries)
istate_chunk = istate_index[istart:istop]
pcoord_chunk = istate_pcoords[istart:istop]
#state_ids = numpy.arange(istart,istop,dtype=numpy.uint)
for ci in xrange(len(istate_chunk)):
row = istate_chunk[ci]
pcoord = pcoord_chunk[ci]
state_id = istart+ci
if row['iter_used'] == ISTATE_UNUSED and row['istate_status'] == ISTATE_STATUS_PREPARED:
istate = InitialState(state_id = state_id,
basis_state_id=long(row['basis_state_id']),
iter_created = int(row['iter_created']), iter_used=0,
istate_type = int(row['istate_type']),
pcoord=pcoord.copy(),
istate_status=ISTATE_STATUS_PREPARED)
states.append(istate)
del row, pcoord, state_id
istart += chunksize
del istate_chunk, pcoord_chunk #, state_ids, unused, ids_of_unused
log.debug('found {:d} unused states'.format(len(states)))
return states[:n_states]
chunksize = self.table_scan_chunksize
states = []
istart = 0
while istart < n_index_entries and len(states) < n_states:
istop = min(istart+chunksize, n_index_entries)
istate_chunk = istate_index[istart:istop]
pcoord_chunk = istate_pcoords[istart:istop]
#state_ids = numpy.arange(istart,istop,dtype=numpy.uint)
for ci in range(len(istate_chunk)):
row = istate_chunk[ci]
pcoord = pcoord_chunk[ci]
state_id = istart+ci
if row['iter_used'] == ISTATE_UNUSED and row['istate_status'] == ISTATE_STATUS_PREPARED:
istate = InitialState(state_id = state_id,
basis_state_id=int(row['basis_state_id']),
iter_created = int(row['iter_created']), iter_used=0,
istate_type = int(row['istate_type']),
pcoord=pcoord.copy(),
istate_status=ISTATE_STATUS_PREPARED)
states.append(istate)
del row, pcoord, state_id
istart += chunksize
del istate_chunk, pcoord_chunk #, state_ids, unused, ids_of_unused
log.debug('found {:d} unused states'.format(len(states)))
return states[:n_states]
def get_pcoord(self, state):
'''Get the progress coordinate of the given basis or initial state.'''
template_args, environ = {}, {}
if isinstance(state, BasisState):
execfn = self.exec_for_basis_state
self.update_args_env_basis_state(template_args, environ, state)
struct_ref = environ[self.ENV_BSTATE_DATA_REF]
elif isinstance(state, InitialState):
execfn = self.exec_for_initial_state
self.update_args_env_initial_state(template_args, environ, state)
struct_ref = environ[self.ENV_ISTATE_DATA_REF]
else:
raise TypeError('state must be a BasisState or InitialState')
child_info = self.exe_info.get('get_pcoord')
fd, rfname = tempfile.mkstemp()
os.close(fd)
addtl_env = {self.ENV_PCOORD_RETURN: rfname,
self.ENV_STRUCT_DATA_REF: struct_ref}
try:
rc, rusage = execfn(child_info, state, addtl_env)
if rc != 0:
istates = dm_new.create_initial_states(n_segments, n_iter=1)
segments = []
state_map_dtype = numpy.dtype([('old_n_iter', n_iter_dtype),
('old_seg_id', seg_id_dtype),
('new_istate_id', seg_id_dtype)])
state_map = numpy.empty((n_segments,),dtype=state_map_dtype)
state_map['old_n_iter'] = n_iter
for (iseg, (index_row, pcoord)) in enumerate(zip(old_index, old_final_pcoords)):
istate = istates[iseg]
istate.iter_created = 0
istate.iter_used = 1
istate.istate_type = InitialState.ISTATE_TYPE_RESTART
istate.istate_status = InitialState.ISTATE_STATUS_PREPARED
istate.pcoord = pcoord
segment = Segment(n_iter=1, seg_id=iseg, weight=index_row['weight'],
parent_id =-(istate.state_id+1),
wtg_parent_ids = [-(istate.state_id+1)],
status=Segment.SEG_STATUS_PREPARED)
segment.pcoord = numpy.zeros((pcoord_len, pcoord_ndim), dtype=pcoord.dtype)
segment.pcoord[0] = pcoord
segments.append(segment)
state_map[iseg]['old_seg_id'] = iseg
state_map[iseg]['new_istate_id'] = istate.state_id
dm_new.update_initial_states(istates, n_iter=0)
dm_new.prepare_iteration(n_iter=1, segments=segments)
# Update current iteration and close both files
def get_initial_states(self, n_iter=None):
states = []
with self.lock:
n_iter = n_iter or self.current_iteration
ibstate_group = self.find_ibstate_group(n_iter)
try:
istate_index = ibstate_group['istate_index'][...]
except KeyError:
return []
istate_pcoords = ibstate_group['pcoord'][...]
for state_id, (state, pcoord) in enumerate(izip(istate_index, istate_pcoords)):
states.append(InitialState(state_id=state_id, basis_state_id=long(state['basis_state_id']),
iter_created=int(state['iter_created']), iter_used=int(state['iter_used']),
istate_type=int(state['istate_type']), pcoord=pcoord.copy()))
return states
def get_pcoord(self, state):
if isinstance(state, BasisState):
template_args = {'basis_state': state}
bstate_data_ref = self.makepath(self.basis_state_ref_template, template_args)
print(bstate_data_ref)
coords = 10.0 * np.load(bstate_data_ref)
elif isinstance(state, InitialState):
template_args = {'initial_state': state}
istate_data_ref = self.makepath(self.initial_state_ref_template, template_args)
coords = 10.0 * np.load(istate_data_ref)
else:
raise TypeError('state must be BasisState or InitialState')
state.pcoord = self.dist(coords[0,:], coords[1,:])
print(state.pcoord)