def get_core(frm_in): """ Selects the core parts of a base.Container() instance, and returns them inside a new base.Container() instance. """ frm_out = base.Container() # --- frm_out.i = frm_in.i # --- extend pipeline_log frm_out.put_data('log', frm_in.get_data('log')) log_entry = {'get_core': True} frm_out.put_meta(log_entry) # --- filter particle numbers for key in sorted(frm_in.get_keys(base.loc_nr_particles)): if key.endswith('.s'): continue frm_out.put_data(base.loc_nr_particles + '/' + key, frm_in.get_data(base.loc_nr_particles + '/' + key)) # --- filter histogram data for key in sorted(frm_in.get_keys(base.loc_histograms)): if (key != 'radii'): pair = key.split(',') if (pair[0].endswith('.s') or pair[1].endswith('.s')): continue frm_out.put_data(base.loc_histograms + '/' + key, frm_in.get_data(base.loc_histograms + '/' + key)) # --- merge_virtual_particles(frm_out) # --- return frm_out
def __next__(self): """Generator that iterates through all the frames and yields frame by frame. """ if not self.initialized: self.init() # counters to make self.last and self.step work i = self.first # numbering relative to the original dataset raw_count = 0 # count the raw frames delivered by MDAnalysis for ts in self.universe.trajectory: if (i > self.last): break elif (raw_count % self.step == 0): coord = self.atoms.positions dimensions = self.universe.atoms.dimensions # --- separate coordinates by species and put them into the container frm = base.Container() for si in range(1, self.nEl + 1): species_label = self.elements[si - 1] species_coord = coord[np.where(self.speciesList == si)] # we need to add the coordinates type-converted to 64 bit floats frm.put_data(base.loc_coordinates + '/' + species_label, species_coord.astype(np.float64)) frm.put_data(base.loc_dimensions, dimensions) frm.i = i frm.put_meta(self.get_meta()) if self.verb: print("MDReader.next() : {}".format(frm.i)) yield frm # --- i = i + 1 raw_count += 1
def get_cross(frm_in): """ Selects the cross parts of a base.Container() instance, and returns them inside a new base.Container() instance. """ frm_out = base.Container() # --- frm_out.i = frm_in.i # --- extend pipeline_log frm_out.put_data('log', frm_in.get_data('log')) log_entry = {'get_cross': True} frm_out.put_meta(log_entry) # Note: particle numbers do not make sense for cross for key in sorted(frm_in.get_keys(base.loc_histograms)): if (key != 'radii'): pair = key.split(',') if (pair[0].endswith('.s') and pair[1].endswith('.s')) or \ (not pair[0].endswith('.s') and not pair[1].endswith('.s')): continue frm_out.put_data(base.loc_histograms + '/' + key, frm_in.get_data(base.loc_histograms + '/' + key)) # --- merge_virtual_particles(frm_out) # --- return frm_out
def __next__(self): while (self.count <= self.n_histogram_sets): hs = base.Container(number=self.count) dr = 0.01 radii = np.array([dr * (0.5 + x) for x in range(self.n_bins)]) hs.put_data(base.loc_histograms + '/radii', radii) # --- fill histogram set with dummy data spec_list = (list(string.ascii_uppercase))[:self.n_el] for i in range(1, self.n_virtual + 1): x = 'X' + str(i) spec_list.append(x) if self.shell: spec_list[:] = spec_list + [x + '.s' for x in spec_list] for i in range(len(spec_list)): for j in range(i, len(spec_list)): key = spec_list[i] + "," + spec_list[j] if self.random: histo = np.random.rand(self.n_bins) else: histo = np.ones(self.n_bins) hs.put_data(base.loc_histograms + '/' + key, histo) # --- if self.verb: print("DummyReader.next() :", self.count) hs.put_meta(self.get_meta()) yield hs self.count += 1 del hs
def __init__(self, n_frames=3, n_elems=3, n_atoms=1024, verbose=False): self.n_frames = n_frames self.n_elems = n_elems self.n_atoms = n_atoms self.verb = verbose self.frms = [] # --- self._depends.extend(super(base.Reader, self)._depends) self._conflicts.extend(super(base.Reader, self)._conflicts) # --- for i in range(n_frames): frm = base.Container(number=i) for j in range(n_elems): s_name = "El" + str(i) + str(j) s_coor = np.random.rand(n_atoms, 3) frm.put_data(base.loc_coordinates + '/' + s_name, s_coor) s_name = 'my/huge/table/in/some/subdirectory/data' s_table = np.random.rand(n_atoms, n_atoms) frm.put_data(s_name, s_table) s_name = 'integer' frm.put_data(s_name, 1) s_name = 'string' frm.put_data(s_name, 'Banana Joe') s_name = 'float' frm.put_data(s_name, 3.14) s_name = 'Python integer list' frm.put_data(s_name, [1, 2, 3, 4, 5, 6]) s_name = 'Python float list' frm.put_data(s_name, [1., 2., 3., 4., 5., 6.]) s_name = 'Python string list' frm.put_data(s_name, ['Hello', 'World']) s_name = 'Python mixed list' frm.put_data(s_name, ['Hello', 1]) self.frms.append(copy.deepcopy(frm))
def get_full(frm_in): """ Selects the core, shell, cross parts of a base.Container() instance, and returns them inside a new base.Container() instance in a combined way. """ ####################### # if there's no shell (shell_width<0) then the histogram is already the full histogram geom = frm_in.get_geometry() meta = frm_in.query_meta(geom) if meta['shell_width'] < 0: return frm_in ####################### frm_out = base.Container() # --- frm_out.i = frm_in.i # --- extend pipeline_log frm_out.put_data('log', frm_in.get_data('log')) log_entry = {'get_full': True} frm_out.put_meta(log_entry) # --- construct the full set from the divided sets frm_out_core = get_core(frm_in) frm_out_shell = get_shell(frm_in) frm_out_cross = get_cross(frm_in) # --- sum up particle numbers frm_out.put_data( base.loc_nr_particles, # include radii frm_out_core.get_data(base.loc_nr_particles)) X = frm_out.get_data(base.loc_nr_particles) Y = frm_out_shell.get_data(base.loc_nr_particles) dict_util.sum_values(X, Y) # semantics: X += Y # --- sum up histogram vectors frm_out.put_data( base.loc_histograms, # include radii frm_out_core.get_data(base.loc_histograms)) X = frm_out.get_data(base.loc_histograms) Y = frm_out_shell.get_data(base.loc_histograms) dict_util.sum_values(X, Y) # semantics: X += Y # --- sum up cross histograms explicitly due to key handling for (key, Y) in list( (frm_out_cross.get_data(base.loc_histograms)).items()): if (key == 'radii'): continue else: pair = key.split(',') for idx in [0, 1]: if pair[idx].endswith('.s'): pair[idx] = (pair[idx])[:-2] key_new = pair[0] + ',' + pair[1] X = frm_out.get_data(base.loc_histograms + '/' + key_new) X += Y # --- merge_virtual_particles(frm_out) # --- return frm_out
def get_frame(self, i): """Create coordinate data sets for a single frame.""" # Cadishi and Capriqorn use base.Container() instances to handle data frm = base.Container() # Generate list of (Nx3) NumPy coordinate arrays. # NOTE: This is the location where you would want to feed your own data in! # We use random data in this simple example. coord_set = util.generate_random_coordinate_set(self.n_objects) for idx, coords in enumerate(coord_set): # Each species carries a label. # NOTE: You need to take care about labels fitting to your data! label = "species_" + str(idx) # add coordinates to frame at the following standardized location location = base.loc_coordinates + '/' + label frm.put_data(location, coords.astype(np.float64)) # finally, each frame is uniquely numbered frm.i = i return frm
def __next__(self): """iterate through all the histogram sets and yield set by set""" if self.verb: print() for i in range(len(self.file_list)): hs = base.Container() # --- fill the hs.histograms dictionary filename = self.file_list[i] histograms = np.load(filename) (_n_bins, n_rows) = histograms.shape # the zeroth column of histograms contains the radial grid radii = histograms[:, 0] hs.put_data(base.loc_histograms + '/radii', radii) assert (n_rows == len(self.header)) # the following columns contain the distance histograms for idx in range(1, n_rows): key = self.header[idx] val = histograms[:, idx] hs.put_data(base.loc_histograms + '/' + key, val) # --- fill the hs.particles dictionary --- # The use of a numpy array to hold a single value may seem like # a bit of an overkill, but when averaging over several # HistogramSets the array will hold the particle numbers of all # the frames that have contributed to the averaged set. for j in range(len(self.nr_part_header)): key = self.nr_part_header[j] val = np.array([self.nr_part_table[i, j]], dtype=np.int32) hs.put_data(base.loc_nr_particles + '/' + key, val) # --- val = self.pipeline_log + [self.get_meta()] hs.put_data('log', val) # --- self.count += 1 hs.i = self.count # --- yield hs if self.verb: print("distHistoReader.next() : " + filename)
def get_shell(frm_in): """ Selects the shell parts of a base.Container() instance, and returns them inside a new base.Container() instance. """ frm_out = base.Container() # --- frm_out.i = frm_in.i # --- extend pipeline_log frm_out.put_data('log', frm_in.get_data('log')) log_entry = {'get_shell': True} frm_out.put_meta(log_entry) # --- filter particle numbers for key in sorted(frm_in.get_keys(base.loc_nr_particles)): if key.endswith('.s'): # strip off the trailing shell identifier frm_out.put_data( base.loc_nr_particles + '/' + key[:-2], frm_in.get_data(base.loc_nr_particles + '/' + key)) elif (key == 'frame'): frm_out.put_data( base.loc_nr_particles + '/' + key, frm_in.get_data(base.loc_nr_particles + '/' + key)) # --- filter histogram data for key in sorted(frm_in.get_keys(base.loc_histograms)): pair = key.split(',') if (pair[0].endswith('.s') and pair[1].endswith('.s')): # strip off the trailing shell identifiers key_new = (pair[0])[:-2] + ',' + (pair[1])[:-2] frm_out.put_data(base.loc_histograms + '/' + key_new, frm_in.get_data(base.loc_histograms + '/' + key)) elif (key == 'radii'): frm_out.put_data(base.loc_histograms + '/' + key, frm_in.get_data(base.loc_histograms + '/' + key)) # --- merge_virtual_particles(frm_out) # --- return frm_out
def __next__(self): frm_out = base.Container() for frm_tmp in next(self.src): # handle None correctly (used as a sign to abandon ship in parallel pipelines) # in combination with the remainder treatment below (which needs the last frm_in) if frm_tmp is not None: frm_in = copy.deepcopy(frm_tmp) self.geometry = frm_in.get_geometry() # --- multiref: scale histograms containing virtual particles virtual_param = frm_in.query_meta('VirtualParticles') if (virtual_param is not None and self.geometry == 'MultiReferenceStructure'): frm_in = scaleVirtualHistograms(frm_in) # --- take into account the histogram sample parameter when averaging if (self.count == 0): histo_par = util.search_pipeline('histograms', frm_in.get_meta()) if (histo_par is not None) and (len(histo_par) > 0): histo_sample = histo_par['histogram']['sum'] self.factor = self.factor / float(histo_sample) # --- sum distance histograms if not frm_out.contains_key(base.loc_histograms + '/radii'): frm_out.put_data( base.loc_histograms + '/radii', frm_in.get_data(base.loc_histograms + '/radii')) X = frm_out.get_data(base.loc_histograms) Y = frm_in.get_data(base.loc_histograms) dict_util.sum_values(X, Y) # --- sum shell H_xx(r) if (virtual_param is not None and self.geometry == 'MultiReferenceStructure'): if not frm_out.contains_key(base.loc_shell_Hxx): frm_out.put_data(base.loc_shell_Hxx, frm_in.get_data(base.loc_shell_Hxx)) X = frm_out.get_data(base.loc_shell_Hxx) Y = frm_in.get_data(base.loc_shell_Hxx) dict_util.sum_values(X, Y) # --- sum length histograms (only present with Sphere geometry) if frm_in.contains_key(base.loc_len_histograms): if not frm_out.contains_key(base.loc_len_histograms + '/radii'): frm_out.put_data( base.loc_len_histograms + '/radii', frm_in.get_data(base.loc_len_histograms + '/radii')) X = frm_out.get_data(base.loc_len_histograms) Y = frm_in.get_data(base.loc_len_histograms) dict_util.sum_values(X, Y) # --- append particle numbers if frm_in.contains_key(base.loc_nr_particles): frm_out.append_data(frm_in, base.loc_nr_particles) # --- append periodic box volumes if frm_in.contains_key(base.loc_volumes): frm_out.append_data(frm_in, base.loc_volumes) # --- self.count += 1 # deliver a frame averaged over n_avg frames if (not self.all) and (self.count % self.n_avg == 0): self.apply_rescaling(frm_in, frm_out, self.n_avg, virtual_param) if self.verb: print("Average.next() :", frm_in.i) yield frm_out del frm_out frm_out = base.Container() # rescale and deliver a single frame if averaging over all frames is desired # OR # treat a remainder properly remainder_avg = -1 if self.all: remainder_avg = self.count elif (self.count % self.n_avg > 0): remainder_avg = self.count % self.n_avg print("Average.next(): averaged over " + str(remainder_avg) + " remainder histograms") if (remainder_avg > 0): self.apply_rescaling(frm_in, frm_out, remainder_avg, virtual_param) if self.verb: print("Average.next() :", frm_in.i) yield frm_out # yiel a potentially pending None if frm_tmp is None: yield None