How to use the west.Segment function in west

To help you get started, we’ve selected a few west examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github westpa / westpa / lib / cmds / w_states.py View on Github external
westpa.rc.pstatus('No target states specified.')
            else:
                data_manager.save_target_states(target_states, n_iter)
                sim_manager.report_target_states(target_states)
                
            data_manager.update_iter_group_links(n_iter)
            
        else: # args.mode == 'append'
            if args.bstate_file or args.bstates:
                sys.stderr.write('refusing to append basis states; use --show followed by --replace instead\n')
                sys.exit(2)
            
            target_states = data_manager.get_target_states(n_iter)
            
            seg_index = data_manager.get_seg_index(n_iter)
            if (seg_index['status'] == Segment.SEG_STATUS_COMPLETE).any():
                print('Iteration {:d} has completed segments; applying new states to iteration {:d}'.format(n_iter,n_iter+1))
                n_iter += 1
            
            if args.tstate_file:
                target_states.extend(TargetState.states_from_file(args.tstate_file, system.pcoord_dtype))
            if args.tstates:
                tstates_strio = io.StringIO('\n'.join(args.tstates).replace(',', ' '))
                target_states.extend(TargetState.states_from_file(tstates_strio, system.pcoord_dtype))
                del tstates_strio
                
            if not target_states:
                westpa.rc.pstatus('No target states specified.')
            else:
                data_manager.save_target_states(target_states, n_iter)
                sim_manager.report_target_states(target_states)
github westpa / westpa / src / west / propagators / executable.py View on Github external
def update_args_env_segment(self, template_args, environ, segment):
        template_args['segment'] = segment
        
        environ[self.ENV_CURRENT_SEG_INITPOINT] = Segment.initpoint_type_names[segment.initpoint_type]
        
        if segment.initpoint_type == Segment.SEG_INITPOINT_CONTINUES:
            # Could use actual parent object here if the work manager cared to pass that much data
            # to us (we'd need at least the subset of parents for all segments sent in the call to propagate)
            # that may make a good west.cfg option for future crazy extensibility, but for now,
            # just populate the bare minimum
            parent = Segment(n_iter=segment.n_iter-1, seg_id=segment.parent_id)
            parent_template_args = dict(template_args)
            parent_template_args['segment'] = parent
            
            environ[self.ENV_PARENT_SEG_ID] = str(segment.parent_id if segment.parent_id is not None else -1)
            environ[self.ENV_PARENT_DATA_REF] = self.makepath(self.segment_ref_template, parent_template_args)
        elif segment.initpoint_type == Segment.SEG_INITPOINT_NEWTRAJ:
            # This segment is initiated from a basis state; WEST_PARENT_SEG_ID and WEST_PARENT_DATA_REF are
            # set to the basis state ID and data ref
            initial_state = self.initial_states[segment.initial_state_id]
github westpa / westpa / lib / west_tools / w_dumpsegs.py View on Github external
def go(self):
        segments = self.data_reader.get_segments(self.n_iter)
        
        max_seg_id_len = len(str(max(segment.seg_id for segment in segments)))
        max_status_name_len = max(list(map(len,iter(Segment.status_names.values()))))
        max_endpoint_type_len = max(list(map(len,iter(Segment.endpoint_type_names.values()))))
        max_n_parents_len = len(str(max(len(segment.wtg_parent_ids) for segment in segments)))
        
        report_line = ( '{segment.n_iter:d}  {segment.seg_id:{max_seg_id_len}d}  {segment.weight:20.14g}' 
                        +'  {status_name:{max_status_name_len}s} ({segment.status})'
                        +'  {segment.walltime:<12.6g} {segment.cputime:<12.6g}'
                        +'  {endpoint_type_name:{max_endpoint_type_len}s} ({segment.endpoint_type})'
                        +'  {n_parents:{max_n_parents_len}d} {segment.parent_id:{max_seg_id_len}d} {parents_str}' 
                        +'\n')
        pcoord_lines = ('  pcoord[0]  = {init_pcoord}\n  pcoord[-1] = {final_pcoord}'
                        +'\n')
        for (_seg_id, segment) in enumerate(segments):    
            parents_str = '['+', '.join(map(str,sorted(segment.wtg_parent_ids)))+']'
            init_pcoord_str = '[' + ', '.join('{pcval:<12.6g}'.format(pcval=float(pce)) for pce in segment.pcoord[0]) + ']'
            final_pcoord_str = '[' + ', '.join('{pcval:<12.6g}'.format(pcval=float(pce)) for pce in segment.pcoord[-1]) + ']'
            self.output_file.write(report_line.format(segment=segment, 
                                                 status_name = segment.status_names[segment.status],
github westpa / westpa / src / west / propagators / executable.py View on Github external
def update_args_env_segment(self, template_args, environ, segment):
        template_args['segment'] = segment
        
        environ[self.ENV_CURRENT_SEG_INITPOINT] = Segment.initpoint_type_names[segment.initpoint_type]
        
        if segment.initpoint_type == Segment.SEG_INITPOINT_CONTINUES:
            # Could use actual parent object here if the work manager cared to pass that much data
            # to us (we'd need at least the subset of parents for all segments sent in the call to propagate)
            # that may make a good west.cfg option for future crazy extensibility, but for now,
            # just populate the bare minimum
            parent = Segment(n_iter=segment.n_iter-1, seg_id=segment.parent_id)
            parent_template_args = dict(template_args)
            parent_template_args['segment'] = parent
            
            environ[self.ENV_PARENT_SEG_ID] = str(segment.parent_id if segment.parent_id is not None else -1)
            environ[self.ENV_PARENT_DATA_REF] = self.makepath(self.segment_ref_template, parent_template_args)
        elif segment.initpoint_type == Segment.SEG_INITPOINT_NEWTRAJ:
            # This segment is initiated from a basis state; WEST_PARENT_SEG_ID and WEST_PARENT_DATA_REF are
            # set to the basis state ID and data ref
            initial_state = self.initial_states[segment.initial_state_id]
            basis_state = self.basis_states[initial_state.basis_state_id]
            
            if self.ENV_BSTATE_ID not in environ:
                self.update_args_env_basis_state(template_args, environ, basis_state)
            if self.ENV_ISTATE_ID not in environ:
                self.update_args_env_initial_state(template_args, environ, initial_state)
github westpa / westpa / lib / cmds / w_fork.py View on Github external
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
dm_new.current_iteration = 1
dm_new.close_backing()
dm_old.close_backing()

# Write state map
istate_map_file = open(args.istate_map, 'wt')
github westpa / westpa / lib / cmds / w_fork.py View on Github external
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
dm_new.current_iteration = 1
dm_new.close_backing()
dm_old.close_backing()
github westpa / westpa / src / west / sim_manager.py View on Github external
# Get basis states used in this iteration
        self.current_iter_bstates = self.data_manager.get_basis_states(self.n_iter)
        
        # Get the segments for this iteration and separate into complete and incomplete
        if self.segments is None:
            segments = self.segments = {segment.seg_id: segment for segment in self.data_manager.get_segments()}
            log.debug('loaded {:d} segments'.format(len(segments)))
        else:
            segments = self.segments
            log.debug('using {:d} pre-existing segments'.format(len(segments)))
        
        completed_segments = self.completed_segments = {}
        incomplete_segments = self.incomplete_segments = {}
        for segment in segments.values():
            if segment.status == Segment.SEG_STATUS_COMPLETE:
                completed_segments[segment.seg_id] = segment
            else:
                incomplete_segments[segment.seg_id] = segment
        log.debug('{:d} segments are complete; {:d} are incomplete'.format(len(completed_segments), len(incomplete_segments)))
        
        if len(incomplete_segments) == len(segments):
            # Starting a new iteration
            self.rc.pstatus('Beginning iteration {:d}'.format(self.n_iter))
        elif incomplete_segments:
            self.rc.pstatus('Continuing iteration {:d}'.format(self.n_iter))
        self.rc.pstatus('{:d} segments remain in iteration {:d} ({:d} total)'.format(len(incomplete_segments), self.n_iter,
                                                                                     len(segments)))
        
        # Get the initial states active for this iteration (so that the propagator has them if necessary)
        self.current_iter_istates = {state.state_id: state for state in 
                                     self.data_manager.get_segment_initial_states(list(segments.values()))}
github westpa / westpa / lib / cmds / w_steady_state.py View on Github external
n_iter = dm.current_iteration - 1
    
    # Adjust iteration start/stop to ensure that they are in storage
    args.start_iter = max(1,args.start_iter)
    maxiter = sim_manager.data_manager.current_iteration - 2
    if args.stop_iter:
        args.stop_iter = min(maxiter,args.stop_iter)
    else:
        args.stop_iter = maxiter

    # We will reweight the current iteration (even if it is partially complete)
    next_segments = sim_manager.data_manager.get_segments(n_iter+1)
    
    ss = steady_state(sim_manager, args)
    region_set = ss.region_set
    region_set.assign_to_bins(next_segments, key=Segment.initial_pcoord)
    all_bins = region_set.get_all_bins()
    orig_weights = [bin.weight for bin in all_bins]
    
    # Calculate new weights
    new_weights = ss.get_new_weights()
    
    # Calculate pre-reweighting weights
    assert len(new_weights) == len(orig_weights)
    
    
    if not args.no_reweight:
        if sys.stdout.isatty() and not west.rc.quiet_mode:
            sys.stdout.write('\n')
            sys.stdout.write('Reweighting segments in storage\n')
        
        for bin,new_weight in zip(all_bins, new_weights):
github westpa / westpa / src / west / sim_manager.py View on Github external
if not suppress_we:
            self.we_driver.populate_initial(initial_states, weights, system)
            segments = list(self.we_driver.next_iter_segments)
            binning = self.we_driver.next_iter_binning
        else:
            segments = list(self.we_driver.current_iter_segments)
            binning = self.we_driver.final_binning

        bin_occupancies = numpy.fromiter(map(len,binning), dtype=numpy.uint, count=self.we_driver.bin_mapper.nbins)
        target_occupancies = numpy.require(self.we_driver.bin_target_counts, dtype=numpy.uint)
    
        # Make sure we have 
        for segment in segments:
            segment.n_iter = 1
            segment.status = Segment.SEG_STATUS_PREPARED
            assert segment.parent_id < 0
            assert initial_states[segment.initial_state_id].iter_used == 1        
                    
        data_manager.prepare_iteration(1, segments)
        data_manager.update_initial_states(initial_states, n_iter=1)
                    
        if self.rc.verbose_mode:
            pstatus('\nSegments generated:')
            for segment in segments:
                pstatus('{!r}'.format(segment))
        
        
        pstatus('''
        Total bins:            {total_bins:d}
        Initial replicas:      {init_replicas:d} in {occ_bins:d} bins, total weight = {weight:g}
        Total target replicas: {total_replicas:d}