コード例 #1
0
   def read_datalines(self, NSTEPS=0, start_step=-1, select_ckeys=None, max_vector_dim=None, even_NSTEPS=True):
      """Read NSTEPS steps of file, starting from start_step, and store only
      the selected ckeys.
      INPUT:
        NSTEPS         -> number of steps to read (default: 0 -> reads all the file)
        start_step  = -1 -> continue from current step (default)
                       0 -> go to start step
                       N -> go to N-th step
        select_ckeys   -> an array with the column keys you want to read (see all_ckeys for a list)
        max_vector_dim -> when reading vectors read only this number of components (None = read all components)
        even_NSTEPS    -> round the number of steps to an even number (default: True)
      OUTPUT:
        data    ->  a dictionary with the selected-column steps
      """
      if self._GUI:
         progbar = FloatProgress(min=0, max=100)
         display(progbar)
      start_time = time()
      if (NSTEPS == 0):
         NSTEPS = self.MAX_NSTEPS
      self._set_ckey(select_ckeys, max_vector_dim)  # set the ckeys to read
      self._initialize_dic(NSTEPS)  # allocate dictionary
      self.gotostep(start_step)    # jump to the starting step
      
      # read NSTEPS of the file
      progbar_step = max(100000, int(0.005*NSTEPS))
      for step in range(NSTEPS):
         line = self.file.readline()
         if len(line) == 0:  # EOF
            print "Warning:  reached EOF."
            break
         values = np.array(line.split())
         for key, idx in self.ckey.iteritems():  # save the selected columns
            self.data[key][step,:] = np.array(map(float, values[idx]))
         if ( (step+1)%progbar_step == 0 ):
            if self._GUI:
               progbar.value = float(step+1)/NSTEPS*100.;
               progbar.description = "{:6.2f}%".format(progbar.value)
            else:
               print "    step = {:9d} - {:6.2f}% completed".format(step+1, float(step+1)/NSTEPS*100.)

      if self._GUI:
         progbar.close()
      # check number of steps read, keep an even number of steps
      if (step + 1 < self.NSTEPS):
         if (step == 0):
            print "WARNING:  no step read."
            return
         else:
            print "Warning:  less steps read."
            self.NSTEPS = step + 1
      if even_NSTEPS:
         if (NSTEPS%2 == 1):
            NSTEPS = NSTEPS - 1
      for key, idx in self.ckey.iteritems():  # free memory not used
         self.data[key] = self.data[key][:NSTEPS,:]
      print "  ( %d ) steps read." % (NSTEPS)
      self.NSTEPS = NSTEPS
      print "DONE.  Elapsed time: ", time()-start_time, "seconds"
      return self.data
コード例 #2
0
ファイル: classify.py プロジェクト: tgy/pendigits-hmm
def get_gaussian_labels_probabilities(digits, hidden_markov_models, n_observation_classes, n_hidden_states, n_iter, tol, display_progress, use_pickle, filename):

    labels_probabilities = []

    directory = settings.LABELS_PROBABILITIES_DIRECTORY + "centroids_" + str(n_observation_classes - 3)
    directory += "/hidden_states_" + str(n_hidden_states) + "/n_iter_" + str(n_iter) + "/tol_" + str(tol)
    path = directory + "/" + filename

    if use_pickle and os.path.isfile(path):

        labels_probabilities = pickle.load(open(path, 'rb'))

    else:

        f = FloatProgress(min=0, max=100)
        if display_progress:
            display(f)

        i = 0
        for dig in digits:
            probabilites = get_gaussian_label_probabilites(dig, hidden_markov_models)
            labels_probabilities.append(probabilites)
            f.value = (float(i) * 100.0) / float(len(digits))
            i += 1

        f.close()

        if use_pickle:
            if not os.path.exists(directory):
                os.makedirs(directory)
            with open(path,'wb') as f:
                pickle.dump(labels_probabilities,f)

    return labels_probabilities
コード例 #3
0
ファイル: plotter.py プロジェクト: tgy/pendigits-hmm
def plot_digit_samples(samples, display_progress = False):

    f = FloatProgress(min=0, max=100)
    if display_progress:
        display(f)

    plt.clf();
    _, axarr = plt.subplots(2, 5);

    for i in range(0, 2):
        for j in range(0, 5):

            n = 5*i + j
            n -= 1
            if n < 0:
                n = 9


            x_points = []
            y_points = []

            for curve in samples[n][0].curves:
                for point in curve:
                    x_points.append(point[0])
                    y_points.append(point[1])

            axarr[i, j].plot(x_points, y_points, linewidth = 2.0)
            #axarr[i, j].axis([settings.IMAGE_PLOT_X_MIN, settings.IMAGE_PLOT_X_MAX, settings.IMAGE_PLOT_Y_MIN, settings.IMAGE_PLOT_Y_MAX]);
            f.value += 10

    f.close()
    plt.show();
コード例 #4
0
class IPyBackend(ProgressBar):
    def __init__(self,
                 iterable=None,
                 length=None,
                 *,
                 label=None,
                 show_eta=True,
                 show_percent=None,
                 show_pos=False,
                 item_show_func=None,
                 info_sep=' '):
        from traitlets import TraitError
        try:
            from ipywidgets import FloatProgress
        except ImportError:
            from IPython.html.widgets.widget_float import FloatProgress

        try:
            self.backend = FloatProgress(value=0, min=0, step=1)
            # max and description are set via properties
        except TraitError:
            raise RuntimeError('IPython notebook needs to be running')

        super().__init__(iterable,
                         length,
                         label=label,
                         show_eta=show_eta,
                         show_percent=show_percent,
                         show_pos=show_pos,
                         item_show_func=item_show_func,
                         info_sep=info_sep)

        self.is_hidden = False

    def __enter__(self):
        from IPython.display import display
        display(self.backend)
        return super().__enter__()

    def render_finish(self):
        self.backend.close()

    def render_progress(self):
        info_bits = []
        if self.show_pos:
            info_bits.append(self.format_pos())
        if self.show_percent or (self.show_percent is None
                                 and not self.show_pos):
            info_bits.append(self.format_pct())
        if self.show_eta and self.eta_known and not self.finished:
            info_bits.append(self.format_eta())
        if self.item_show_func is not None:
            item_info = self.item_show_func(self.current_item)
            if item_info is not None:
                info_bits.append(item_info)

        self.backend.description = '{} {}'.format(
            self.label or '', self.info_sep.join(info_bits))
        self.backend.max = self.length
        self.backend.value = self.pos
コード例 #5
0
ファイル: plotter.py プロジェクト: tgy/pendigits-hmm
def plot_digit(digit, display_progress = False):

    fig=plt.figure()
    ax=fig.add_subplot(111)

    f = FloatProgress(min=0, max=100)
    if display_progress:
        display(f)

    n_points = 0
    for curve in digit.curves:
        n_points += len(curve)

    i = 0
    for curve in digit.curves:
        x_points = []
        y_points = []
        for point in curve:
            x_points.append(point[0])
            y_points.append(point[1])
            f.value = 100.0*(float(i) / float(n_points))
            i += 1

        plt.plot(x_points, y_points, linewidth = 2.0)
    f.close()

    plt.axis([settings.IMAGE_PLOT_X_MIN, settings.IMAGE_PLOT_X_MAX, settings.IMAGE_PLOT_Y_MIN, settings.IMAGE_PLOT_Y_MAX])
    plt.show()
コード例 #6
0
ファイル: orbital_util.py プロジェクト: tjduigna/exatomic
def _compute_current_density(bvs, gvx, gvy, gvz, cmatr, cmati, occvec, verbose=True):
    """Compute the current density in each cartesian direction."""
    nbas, npts = bvs.shape
    curx = np.zeros(npts, dtype=np.float64)
    cury = np.zeros(npts, dtype=np.float64)
    curz = np.zeros(npts, dtype=np.float64)
    cval = np.zeros(nbas, dtype=np.float64)
    if verbose:
        fp = FloatProgress(description='Computing:')
        display(fp)
    for mu in range(nbas):
        if verbose:
            fp.value = mu / nbas * 100
        crmu = cmatr[mu]
        cimu = cmati[mu]
        bvmu = bvs[mu]
        gvxmu = gvx[mu]
        gvymu = gvy[mu]
        gvzmu = gvz[mu]
        for nu in range(nbas):
            crnu = cmatr[nu]
            cinu = cmati[nu]
            bvnu = bvs[nu]
            gvxnu = gvx[nu]
            gvynu = gvy[nu]
            gvznu = gvz[nu]
            cval = evaluate('-0.5 * (occvec * (crmu * cinu - cimu * crnu))', out=cval)
            csum = cval.sum()
            evaluate('curx + csum * (bvmu * gvxnu - gvxmu * bvnu)', out=curx)
            evaluate('cury + csum * (bvmu * gvynu - gvymu * bvnu)', out=cury)
            evaluate('curz + csum * (bvmu * gvznu - gvzmu * bvnu)', out=curz)
    if verbose:
        fp.close()
    return curx, cury, curz
コード例 #7
0
    def butter_filter(self, lowcut, highcut=0, order=2, fs=20000.):

        nyq = 0.5 * fs

        if highcut != 0:
            low = lowcut / nyq
            high = highcut / nyq
            b, a = butter(order, [low, high], btype='band', analog=False)
        else:
            low = lowcut / nyq
            b, a = butter(order, low, btype='high', analog=False)

        if isinstance(self.metadata, list):
            progr = FloatProgress(min=0,
                                  max=len(self.metadata),
                                  description='Filtering...',
                                  bar_style='success')
            display(progr)
            cut_data_butter = [None] * len(self.raw_data)
            for i, k in enumerate(self.raw_data):
                cut_data_butter[i] = np.empty(k.shape)
                for j, z in enumerate(self.raw_data[i]):
                    cut_data_butter[i][j] = filtfilt(b, a, z)
                progr.value += 1
            self.butter_data = cut_data_butter
            progr.close()

        if isinstance(self.metadata, dict):
            cut_data_butter = [None] * len(self.raw_data)
            for i, k in enumerate(self.raw_data):
                cut_data_butter[i] = filtfilt(b, a, k)
            self.butter_data = np.asarray(cut_data_butter)
コード例 #8
0
ファイル: plotter.py プロジェクト: tgy/pendigits-hmm
def plot_digits_heatmap(digits, display_progress = False):

    f = FloatProgress(min=0, max=100)
    if display_progress:
        display(f)

    plt.clf();
    _, axarr = plt.subplots(2, 5);

    for i in range(0, 2):
        for j in range(0, 5):

            n = 5*i + j

            x_points = []
            y_points = []
            for digit in digits:
                if digit.label == n:
                    for curve in digit.curves:
                        for point in curve:
                            x_points.append(point[0])
                            y_points.append(point[1])

            heatmap, xedges, yedges = np.histogram2d(x_points, y_points, bins=50);

            extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]];

            axarr[i, j].imshow(np.rot90(heatmap), extent=extent);
            axarr[i, j].axis([settings.IMAGE_PLOT_X_MIN, settings.IMAGE_PLOT_X_MAX, settings.IMAGE_PLOT_Y_MIN, settings.IMAGE_PLOT_Y_MAX]);
            f.value += 10

    f.close()
    plt.show();
コード例 #9
0
    def cepstral_analysis(self,
                          aic_type='aic',
                          Kmin_corrfactor=1.0,
                          bayes_p=False,
                          density_grid=None):
        """Perform the Cepstral Analysis on all blocks."""

        if self.GUI:
            progbar = FloatProgress(min=0, max=100)
            progbar.description = "0 %"
            display(progbar)

        self.BLOCK_NFREQS = self.BLOCK_SIZE / 2 + 1
        if self.MULTI_COMPONENT:
            print ' N_COMPONENTS = {:10d}'.format(self.N_COMPONENTS)
            self.ck_THEORY_var, self.psd_THEORY_mean = multicomp_cepstral_parameters(
                self.BLOCK_NFREQS, self.N_COMPONENTS)
        self.bayes_p = bayes_p

        if (self.N_BLOCKS == 1):
            raise NotImplemented('One block.')

        for L in range(self.N_BLOCKS):
            if self.MULTI_COMPONENT:
                self.block[L].compute_psd(DT=self.TSKIP,
                                          DT_FS=self.DT_FS,
                                          average_components=True)
                self.block[L].dct = ta.CosFilter(self.block[L].logpsd, \
                    ck_theory_var=self.ck_THEORY_var, psd_theory_mean=self.psd_THEORY_mean, aic_type=aic_type, Kmin_corrfactor=Kmin_corrfactor, normalization=self.BLOCK_SIZE)
            else:
                self.block[L].compute_psd(DT=self.TSKIP, DT_FS=self.DT_FS)
                self.block[L].dct = ta.CosFilter(
                    self.block[L].logpsd,
                    aic_type=aic_type,
                    Kmin_corrfactor=Kmin_corrfactor,
                    normalization=self.BLOCK_SIZE)  # theory_var=None

            self.block[L].dct.scan_filter_tau()
            if self.bayes_p:
                self.block[L].dct.compute_p_aic(method='ba')
                if density_grid is not None:
                    self.density_grid = density_grid
                    self.block[L].dct.compute_logtau_density(
                        method='ba',
                        only_stats=False,
                        density_grid=density_grid)
                else:
                    self.block[L].dct.compute_logtau_density(method='ba',
                                                             only_stats=True)
            if self.GUI:
                progbar.value = float(L + 1) / self.N_BLOCKS * 100.
                progbar.description = "%5.2f %%" % progbar.value

        if self.GUI:
            progbar.close()

        self.freqs = self.block[0].freqs
        return
コード例 #10
0
ファイル: pcf.py プロジェクト: wgong/exatomic
def radial_pcf_out_of_core(hdftwo, hdfout, u, pairs, **kwargs):
    """
    Out of core radial pair correlation calculation.

    Atomic two body data is expected to have been computed (see
    :func:`~exatomic.core.two.compute_atom_two_out_of_core`)
    An example is given below. Note the importance of the definition
    of pairs and the presence of additional arguments.

    .. code:: Python

        radial_pcf_out_of_core("in.hdf", "out.hdf", uni, {"O_H": ([0], "H")},
                               length="Angstrom", dr=0.01)

    Args:
        hdftwo (str): HDF filepath containing atomic two body data
        hdfout (str): HDF filepath to which radial PCF data will be written (see Note)
        u (:class:`~exatomic.core.universe.Universe`): Universe
        pairs (dict): Dictionary of string name keys, values of ``a``, ``b`` arguments (see Note)
        kwargs: Additional keyword arguments to be passed (see Note)

    Note:
        Results will be stored in the hdfout HDF file. Keys are of the form
        ``radial_pcf_key``. The keys of ``pairs`` are used to store the output
        while the values are used to perform the pair correlation itself.
    """
    f = u.atom['frame'].unique()
    n = len(f)
    fp = FloatProgress(description="Computing:")
    display(fp)
    fdx = f[0]
    twokey = "frame_" + str(fdx) + "/atom_two"
    atom = u.atom[u.atom['frame'] == fdx].copy()
    uu = Universe(atom=atom,
                  frame=u.frame.loc[[fdx]],
                  atom_two=pd.read_hdf(hdftwo, twokey))
    pcfs = {}
    for key, ab in pairs.items():
        pcfs[key] = radial_pair_correlation(uu, ab[0], ab[1],
                                            **kwargs).reset_index()
    fp.value = 1 / n * 100
    for i, fdx in enumerate(f[1:]):
        twokey = "frame_" + str(fdx) + "/atom_two"
        atom = u.atom[u.atom['frame'] == fdx].copy()
        uu = Universe(atom=atom,
                      frame=u.frame.loc[[fdx]],
                      atom_two=pd.read_hdf(hdftwo, twokey))
        for key, ab in pairs.items():
            pcfs[key] += radial_pair_correlation(uu, ab[0], ab[1],
                                                 **kwargs).reset_index()
        fp.value = (i + 1) / n * 100
    store = pd.HDFStore(hdfout)
    for key in pairs.keys():
        pcfs[key] /= n
        store.put("radial_pcf_" + key, pcfs[key])
    store.close()
    fp.close()
コード例 #11
0
    def read_timesteps(self, selection, start_step=-1, select_ckeys=None, fast_check=True):
        """
        Read selected keys of file, within the provided range.
        Examples:
            read_timesteps(10, start_step=0, select_ckeys=['id,xu,yu,vu']) -->>   Read first 10 timesteps, only the specified columns
            read_timesteps(10, select_ckeys=['id,xu,yu,vu']) -->>   Read the next 10 timesteps, only the specified columns (DELTA_TIMESTEP is assumed)
            read_timesteps((10,30))      -->>  Read from TIMESTEP 10 to 30
            read_timesteps((10,30,2))    -->>  Read every 2 steps from TIMESTEP 10 to 30
        """
        if self._GUI:
            progbar = FloatProgress(min=0, max=100)
            display(progbar)
        start_time = time()
        self._set_ckey(select_ckeys)   # set the ckeys to read      --> ckey
        self._set_timesteps(selection, start_step)   # set the timesteps to read  --> timestep
        self._initialize_dic()   # allocate dictionary        --> data

        # extract the steps from the file
        progbar_step = max(1000, int(0.005 * self.nsteps))
        atomid_col = self.all_ckeys['id'][0]
        for istep, step in enumerate(self.timestep):
            self._gototimestep(step, fast_check)   # jump to the desired step,
            self.data[istep]['TIMESTEP'] = step
            for nat in range(self.NATOMS):   # read data (may be unsorted)
                line = self.file.readline()
                if len(line) == 0:   # EOF
                    raise EOFError('Warning:  reached EOF.')
                values = np.array(line.split())
                for key, idx in self.ckey.items():   # save the selected columns
                    atomid = int(values[atomid_col]) - 1   # current atom index (in LAMMPS it starts from 1)
                    if (key == 'element'):   # this should be improved
                        self.data[istep][key][atomid, :] = np.array(list(map(str, values[idx])))
                    else:
                        self.data[istep][key][atomid, :] = np.array(list(map(float, values[idx])))
            if ((istep + 1) % progbar_step == 0):
                if self._GUI:
                    progbar.value = float(istep + 1) / self.nsteps * 100.
                    progbar.description = '%g %%' % progbar.value
                else:
                    log.write_log('    step = {:9d} - {:6.2f}% completed'.format(istep + 1,
                                                                                 float(istep + 1) / self.nsteps * 100.))
        if self._GUI:
            progbar.close()
        # check number of steps read, keep an even number of steps
        if (istep + 1 < self.nsteps):   # (should never happen)
            if (istep == 0):
                log.write_log('WARNING:  no step read.')
                return
            else:
                log.write_log('Warning:  less steps read.')
                self.nsteps = istep + 1
        if not self._quiet:
            log.write_log('  ( %d ) steps read.' % (self.nsteps))
            log.write_log('DONE.  Elapsed time: ', time() - start_time, 'seconds')
        self._compute_current_step = False   # next time do not compute the current_step
        return self.data
コード例 #12
0
        def direct_read(self, path, start, stop, modified):
            # Generate an array with the length of the clean indices and broadcast the data directly into array.
            #-> The slicing is very time consuming for many datapoints (>100000)
            if isinstance(self.dict, File):
                electrode_info = np.asarray(self.dict['mapping']['channel',
                                                                 'electrode'])
                mask = electrode_info['electrode'] != -1
                clean_rel_inds = electrode_info['channel'][mask]

                traces = np.empty((len(clean_rel_inds), stop - start))
                self.metadata['DAC'] = np.empty([stop - start])
                self.dict['sig'].read_direct(traces[:, :],
                                             source_sel=np.s_[clean_rel_inds,
                                                              start:stop])
                self.dict['sig'].read_direct(self.metadata['DAC'][:],
                                             source_sel=np.s_[1024,
                                                              start:stop])
                print self.metadata['DAC'].shape
                self.metadata['time'] = np.arange(0, (stop - start) / 20000.,
                                                  1 / 20000.)

            if isinstance(self.dict, list):
                electrode_info = [
                    np.asarray(i['mapping']['channel', 'electrode'])
                    for i in self.dict
                ]
                mask = [i['electrode'] != -1 for i in electrode_info]
                clean_rel_inds = [
                    i[0]['channel'][i[1]] for i in zip(electrode_info, mask)
                ]

                traces = [
                    np.empty((len(i), stop - start)) for i in clean_rel_inds
                ]

                progr = FloatProgress(min=0,
                                      max=len(self.dict),
                                      description='Importing...',
                                      bar_style='success')
                display(progr)

                for i, v in enumerate(zip(traces, clean_rel_inds)):
                    self.dict[i]['sig'].read_direct(
                        v[0], source_sel=np.s_[v[1], start:stop])
                    self.metadata[i]['DAC'] = np.empty([stop - start])
                    self.dict[i]['sig'].read_direct(
                        self.metadata[i]['DAC'][:],
                        source_sel=np.s_[1024, start:stop])
                    self.metadata[i]['time'] = np.arange(
                        0, (stop - start) / 20000., 1 / 20000.)
                    progr.value += 1
                progr.close()

            return traces
コード例 #13
0
def compute_atom_two_out_of_core(hdfname, uni, a, **kwargs):
    """
    Perform an out of core periodic two body calculation for a simple cubic
    unit cell with dimension a.

    All data will be saved to and HDF5 file with the given filename. Key
    structure is per frame, i.e. ``frame_fdx/atom_two``.

    Args:
        hdfname (str): HDF file name
        uni (:class:`~exatomic.core.universe.Universe`): Universe
        a (float): Simple cubic unit cell dimension
        kwargs: Keyword arguments for bond computation (i.e. covalent radii)

    See Also:
        :func:`~exatomic.core.two._compute_bonds`
    """
    store = pd.HDFStore(hdfname, mode="a")
    unit_atom = uni.atom[['symbol', 'x', 'y', 'z', 'frame']].copy()
    unit_atom['symbol'] = unit_atom['symbol'].astype(str)
    unit_atom['frame'] = unit_atom['frame'].astype(int)
    unit_atom.update(uni.unit_atom)
    grps = unit_atom.groupby("frame")
    n = len(grps)
    fp = FloatProgress(description="AtomTwo to HDF:")
    display(fp)
    for i, (fdx, atom) in enumerate(grps):
        v = pdist_ortho(atom['x'].values, atom['y'].values, atom['z'].values,
                        a, a, a, atom.index.values, a)
        tdf = pd.DataFrame.from_dict({
            'frame':
            np.array([fdx] * len(v[0]), dtype=int),
            'dx':
            v[0],
            'dy':
            v[1],
            'dz':
            v[2],
            'dr':
            v[3],
            'atom0':
            v[4],
            'atom1':
            v[5],
            'projection':
            v[6]
        })
        _compute_bonds(uni.atom[uni.atom['frame'] == fdx], tdf, **kwargs)
        store.put("frame_" + str(fdx) + "/atom_two", tdf)
        fp.value = i / n * 100
    store.close()
    fp.close()
コード例 #14
0
def torus_dat(kp, kq, refine=300, segm=40, tR=1.6, tr=0.6):
    spt, spp, spq, spr, spR = sp.symbols("t p q r R", real=True)

    c = sp.Matrix([(spR+spr*sp.cos(2*sp.pi*spq*spt))*sp.cos(2*sp.pi*spp*spt),\
         (spR+spr*sp.cos(2*sp.pi*spq*spt))*sp.sin(2*sp.pi*spp*spt),\
          spr*sp.sin(2*sp.pi*spq*spt)])

    dc = sp.Matrix([sp.diff(x,spt) for x in c]) # derivative
    ldc = sp.sqrt(sum( [ x**2 for x in dc ] )).simplify() # speed
    udc = dc/ldc

    ## 2nd order
    kc = sp.Matrix([sp.diff(x,spt) for x in udc]) # curvature vector
    ks = sp.sqrt(sum( [ x**2 for x in kc])) # curvature scalar
    ukc = kc/ks # unit curvature vector

    ## bi-normal
    bnc = udc.cross(ukc) # cross of unit tangent and unit curvature.

    ## the parametrization of the boundary of the width w tubular neighbourhood
    spw, spu = sp.symbols("w, u", real=True) ## width of torus knot, and meridional parameter

    tSurf = c + spw*sp.cos(2*sp.pi*(spu+kp*kq*spt))*ukc + spw*sp.sin(2*sp.pi*(spu+kp*kq*spt))*bnc

    ## (b) ufuncify
    from sympy.utilities.autowrap import ufuncify
    knotSuf = [ufuncify([spt, spp, spq, spr, spR, spw, spu], tSurf[i]) for i in range(3)]
    knotSnp = sp.lambdify((spt, spp, spq, spr, spR, spw, spu), tSurf, "numpy" )

    kt = (np.pi*tr) / (4*kp) # knot radial thickness 2*pi*tr is circumf, and kp strands pass through so this
    ## should be around 2*pi*tr  would be 2*kp*kt for the knot to fill the surface, i.e kt = pi*tr / 4*kp
    ## make bigger or smaller depending on how much empty space one wants to see.

    seg = kp*refine ## segments along length of pq torus knot. kp*120 gives a fairly smooth image.

    def surf(i,j): ## lambdify
        return np.array(knotSnp(float(i)/seg, kp, kq, tr, tR, kt, float(j)/segm)).ravel()


    fp = FloatProgress(min=0, max=100, description="Knot data");     
    display(fp); ## progrss indicator

    xyz = np.ndarray( (seg+1, segm+1, 3) )
    for i,j in it.product( range(seg+1), range(segm+1) ):
        ## put the affine reparametrization here. 
        xyz[i,j] = surf(i,j)
        fp.value = int(100*i/(seg+1))
        
    fp.close()
    return(xyz)
コード例 #15
0
class IPyBackend(ProgressBar):
	def __init__(self, iterable=None, length=None, *, label=None,
			show_eta=True, show_percent=None, show_pos=False,
			item_show_func=None, info_sep=' '):
		from IPython import get_ipython
		
		try:
			from ipywidgets import FloatProgress
		except ImportError:
			from IPython.html.widgets.widget_float import FloatProgress
		
		ipython = get_ipython()
		if not ipython or ipython.__class__.__name__ != 'ZMQInteractiveShell':
			raise RuntimeError('IPython notebook needs to be running')
		
		self.backend = FloatProgress(value=0, min=0, step=1)
		# max and description are set via properties
		
		super().__init__(iterable, length, label=label,
			show_eta=show_eta, show_percent=show_percent, show_pos=show_pos,
			item_show_func=item_show_func, info_sep=info_sep)
		
		self.is_hidden = False
	
	
	def __enter__(self):
		from IPython.display import display
		display(self.backend)
		return super().__enter__()
	
	def render_finish(self):
		self.backend.close()
	
	def render_progress(self):
		info_bits = []
		if self.show_pos:
			info_bits.append(self.format_pos())
		if self.show_percent or (self.show_percent is None and not self.show_pos):
			info_bits.append(self.format_pct())
		if self.show_eta and self.eta_known and not self.finished:
			info_bits.append(self.format_eta())
		if self.item_show_func is not None:
			item_info = self.item_show_func(self.current_item)
			if item_info is not None:
				info_bits.append(item_info)
		
		self.backend.description = '{} {}'.format(self.label or '', self.info_sep.join(info_bits))
		self.backend.max = self.length
		self.backend.value = self.pos
コード例 #16
0
ファイル: plots.py プロジェクト: ddemidov/pydem
def make_video(fname, show=True, figsize=(12,8), vmax=None, cmap='inferno', s=9, maxframes=None):
    with h5py.File('{0}.h5'.format(fname), 'r') as f:
        time = f['time'][:]
        n = time.shape[0]

        if maxframes is None or maxframes > n:
            stride = 1
            frames = n
        else:
            stride = round(n / maxframes)
            frames = n // stride

        if vmax is None:
            vmax = norm(f['step-0/vel'][:],axis=1).max()

        bar = FloatProgress(min=0, max=n-1, description='frames: {0}'.format(n))
        display(bar)

        def make_frame(step):
            coo = f['step-{0}/coo'.format(step * stride)][:]
            vel = f['step-{0}/vel'.format(step * stride)][:]
            L,H = amax(coo, axis=0)

            gcf().clear()
            dots = scatter(coo[:,0], coo[:,1], s=s, c=norm(vel,axis=1),
                    vmax=vmax, linewidth=0, cmap=cmap)

            m = L * 0.01
            xlim([-m, L+m])
            ylim([-m, H+m])
            colorbar()
            gca().set_aspect('equal')

            bar.value = step * stride
            bar.description = '{0}/{1}'.format(step * stride, n)
            return dots

        fig = figure(figsize=figsize);
        anim = animation.FuncAnimation(fig, make_frame, frames=frames, interval=30)
        anim.save('{0}.mp4'.format(fname), bitrate=3200, extra_args=['-vcodec', 'libx264'])

        close()
        bar.close()

    if show:
        return show_video(fname)
コード例 #17
0
ファイル: plotter.py プロジェクト: tgy/pendigits-hmm
def plot_digit_observations(digit, centroids, n_observation_classes, display_progress = False):

    pen_down_label = n_observation_classes - settings.PEN_DOWN_LABEL_DELTA
    pen_up_label = n_observation_classes - settings.PEN_UP_LABEL_DELTA
    stop_label = n_observation_classes - settings.STOP_LABEL_DELTA

    fig=plt.figure()
    ax=fig.add_subplot(111)

    f = FloatProgress(min=0, max=100)
    if display_progress:
        display(f)

    curves = []
    current_curve = []
    for observation in digit.observations:
        if observation < pen_down_label:
            point = centroids[observation]
            current_curve.append(point)
        elif observation == pen_up_label:
            if len(current_curve) > 0:
                curves.append(current_curve)
            current_curve = []

    n_points = 0
    for curve in curves:
        n_points += len(curve)

    i = 0
    for curve in curves:
        x_points = []
        y_points = []
        for point in curve:
            x_points.append(point[0])
            y_points.append(point[1])
            f.value = 100.0*(float(i) / float(n_points))
            i += 1

        plt.plot(x_points, y_points, linewidth = 2.0)
    f.close()

    plt.axis([settings.IMAGE_PLOT_X_MIN, settings.IMAGE_PLOT_X_MAX, settings.IMAGE_PLOT_Y_MIN, settings.IMAGE_PLOT_Y_MAX])
    plt.show()
コード例 #18
0
    def cepstral_analysis_kappa(self,other, aic_type='aic', Kmin_corrfactor=1.0, bayes_p=False, density_grid=None): #need also "other", a class with the charge current!
        """Perform the Cepstral Analysis on all blocks."""

        if self.GUI:
            progbar = FloatProgress(min=0, max=100)
            progbar.description = "0 %"
            display(progbar)

        self.BLOCK_NFREQS = self.BLOCK_SIZE/2 + 1
        if self.MULTI_COMPONENT:
            print ' N_COMPONENTS = {:10d}'.format(self.N_COMPONENTS)
            self.ck_THEORY_var, self.psd_THEORY_mean = tc.md.cepstral.multicomp_cepstral_parameters(self.BLOCK_NFREQS, self.N_COMPONENTS-1) #different number of degrees of freedom!
        self.bayes_p = bayes_p
        
        if (self.N_BLOCKS == 1):
            raise NotImplementedError('One block.')
        
        for L in range(self.N_BLOCKS):
            if self.MULTI_COMPONENT:
                self.block[L].compute_kappa(other=other.block[L],DT=self.TSKIP, DT_FS=self.DT_FS, average_components=True) #different method call!
                self.block[L].dct = tc.md.CosFilter(self.block[L].logpsd, \
                    ck_theory_var=self.ck_THEORY_var, psd_theory_mean=self.psd_THEORY_mean, aic_type=aic_type, Kmin_corrfactor=Kmin_corrfactor)#, normalization=self.BLOCK_SIZE) #removed (personal comunication with Loris)
            else:
                self.block[L].compute_kappa(other=other.block[L],DT=self.TSKIP, DT_FS=self.DT_FS) #different method call!
                self.block[L].dct = tc.md.CosFilter(self.block[L].logpsd, aic_type=aic_type, Kmin_corrfactor=Kmin_corrfactor)#, normalization=self.BLOCK_SIZE) # theory_var=None

            self.block[L].dct.scan_filter_tau()
            if self.bayes_p:
                self.block[L].dct.compute_p_aic(method='ba')
                if density_grid is not None:
                    self.density_grid = density_grid
                    self.block[L].dct.compute_logtau_density(method='ba', only_stats=False, density_grid=density_grid)
                else:
                    self.block[L].dct.compute_logtau_density(method='ba', only_stats=True)
            if self.GUI:
                progbar.value = float(L+1)/self.N_BLOCKS*100.;
                progbar.description = "%5.2f %%" % progbar.value
        
        if self.GUI:
            progbar.close()

        self.freqs = self.block[0].freqs
        return
コード例 #19
0
ファイル: orbital_util.py プロジェクト: wgong/exatomic
def _compute_current_density(bvs,
                             gvx,
                             gvy,
                             gvz,
                             cmatr,
                             cmati,
                             occvec,
                             verbose=True):
    """Compute the current density in each cartesian direction."""
    nbas, npts = bvs.shape
    curx = np.zeros(npts, dtype=np.float64)
    cury = np.zeros(npts, dtype=np.float64)
    curz = np.zeros(npts, dtype=np.float64)
    cval = np.zeros(nbas, dtype=np.float64)
    if verbose:
        fp = FloatProgress(description='Computing:')
        display(fp)
    for mu in range(nbas):
        if verbose:
            fp.value = mu / nbas * 100
        crmu = cmatr[mu]
        cimu = cmati[mu]
        bvmu = bvs[mu]
        gvxmu = gvx[mu]
        gvymu = gvy[mu]
        gvzmu = gvz[mu]
        for nu in range(nbas):
            crnu = cmatr[nu]
            cinu = cmati[nu]
            bvnu = bvs[nu]
            gvxnu = gvx[nu]
            gvynu = gvy[nu]
            gvznu = gvz[nu]
            cval = evaluate('-0.5 * (occvec * (crmu * cinu - cimu * crnu))',
                            out=cval)
            csum = cval.sum()
            evaluate('curx + csum * (bvmu * gvxnu - gvxmu * bvnu)', out=curx)
            evaluate('cury + csum * (bvmu * gvynu - gvymu * bvnu)', out=cury)
            evaluate('curz + csum * (bvmu * gvznu - gvzmu * bvnu)', out=curz)
    if verbose:
        fp.close()
    return curx, cury, curz
コード例 #20
0
ファイル: progress.py プロジェクト: dpressel/baseline
class ProgressBarJupyter(Progress):
    """Simple Jupyter progress bar

    Writes a progress bar to an ipython widget

    """
    def __init__(self, total):
        super(ProgressBarJupyter, self).__init__()
        from ipywidgets import FloatProgress
        from IPython.display import display
        self.progress = FloatProgress(min=0, max=total)
        display(self.progress)

    def update(self, step=1):
        self.progress.value += step

    def done(self):
        """Close the widget
        """
        self.progress.close()
コード例 #21
0
class ProgressBarJupyter(Progress):
    """Simple Jupyter progress bar
    
    Writes a progress bar to an ipython widget
    
    """
    def __init__(self, total):
        super(ProgressBarJupyter, self).__init__()
        from ipywidgets import FloatProgress
        from IPython.display import display
        self.progress = FloatProgress(min=0, max=total)
        display(self.progress)

    def update(self, step=1):
        self.progress.value += step

    def done(self):
        """Close the widget
        """
        self.progress.close()
コード例 #22
0
ファイル: angles.py プロジェクト: chrinide/exatomic
def compute_angles_out_of_core(hdfname, uni, bond=True):
    """
    Given an HDF of atom two body properties, compute angles.

    Atomic two body data is expected to have been computed (see
    :func:`~exatomic.core.two.compute_atom_two_out_of_core`)

    Args:
        hdfname (str): Path to HDF file containing two body data
        uni (:class:`~exatomic.core.universe.Universe`): Universe
        bond (bool): Restrict to bond angles (default True)

    Warning:
        If bond is set to False, this process may take a very long time.
    """
    store = pd.HDFStore(hdfname, mode="a")
    f = u.atom['frame'].unique()
    n = len(f)
    fp = FloatProgress(description="Computing:")
    display(fp)
    for i, fdx in enumerate(f):
        tdf = store.get("frame_" + str(fdx) + "/atom_two")
        indexes = []
        radians = []
        for atom0, group in tdf[tdf['bond'] == True].groupby("atom0"):
            dx = group['dx'].values.astype(float)
            dy = group['dy'].values.astype(float)
            dz = group['dz'].values.astype(float)
            dr = group['dr'].values.astype(float)
            atom1 = group['atom1'].values.astype(int)
            rad, adx = angles(dx, dy, dz, dr, atom0, atom1)
            indexes.append(adx)
            radians.append(rad)
        indexes = np.concatenate(indexes)
        radians = np.concatenate(radians)
        adf = pd.DataFrame(indexes, columns=("atom0", "atom1", "atom2"))
        adf['angle'] = radians
        store.put("frame_" + str(fdx) + "/atom_angle", adf)
        fp.value = i / n * 100
    store.close()
    fp.close()
コード例 #23
0
   def read_datalines(filedata, ckey, even_NSTEPS=True, GUI=False):
      if GUI:
         progbar = FloatProgress(min=0, max=100)
         display(progbar)
      NSTEPS = len(filedata) - 1
      data = initialize_dic(NSTEPS, ckey)

      progbar_step = max(100000, int(0.005*NSTEPS))
      for step, line in enumerate(filedata[1:]):
         if len(line) == 0:  # EOF
            print "Warning:  reached EOF."
            break
         values = np.array(line.split())
         for key, idx in ckey.iteritems():  # save the selected columns
            data[key][step,:] = np.array(map(float, values[idx]))
         if ( (step+1)%progbar_step == 0 ):
            if GUI:
               progbar.value = float(step+1)/NSTEPS*100.;
               progbar.description = "{:6.2f}%".format(progbar.value)
            else:
               print "    step = {:9d} - {:6.2f}% completed".format(step+1, float(step+1)/NSTEPS*100.)
      if GUI:
         progbar.close()
      # check number of steps read, keep an even number of steps
      if (step + 1 < NSTEPS):
         if (step == 0):
            print "WARNING:  no step read."
            return
         else:
            print "Warning:  less steps read."
            NSTEPS = step + 1
      if even_NSTEPS:
         if (NSTEPS%2 == 1):
            NSTEPS = NSTEPS - 1
      for key, idx in ckey.iteritems():  # free memory not used
         data[key] = data[key][:NSTEPS,:]
      print "  ( %d ) steps read." % (NSTEPS)
      return data
コード例 #24
0
class AbuProgress(object):
    """单进程(主进程)进度显示控制类"""

    # 过滤DeprecationWarning: Widget._keys_default is deprecated in traitlets 4.1: use @default decorator instead.
    @warnings_filter
    def __init__(self, total, a_progress, label=None):
        """
        外部使用eg:
            progess = AbuProgress(stock_df.shape[0], 0, 'merging {}'.format(m))
            for i, symbol in enumerate(stock_df['symbol']):
                progess.show(i + 1)
        :param total: 总任务数量
        :param a_progress: 初始进度
        :param label: 进度显示label
        """
        self._total = total
        self._progress = a_progress
        self._label = label
        self.f = sys.stdout
        self.progress_widget = None

    def __enter__(self):
        """创建子进程做进度显示"""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.f.write('\r')
        if self.progress_widget is not None:
            self.progress_widget.close()

    @property
    def progress(self):
        """property获取self._progress"""
        return self._progress

    @progress.setter
    def progress(self, a_progress):
        """rogress.setter设置progress"""
        if a_progress > self._total:
            self._progress = self._total
        elif a_progress < 0:
            self._progress = 0
        else:
            self._progress = a_progress

    def show(self, a_progress=None, ext='', p_format="{}:{}:{}%"):
        """
        进行进度控制显示主方法
        :param ext: 可以添加额外的显示文字,str,默认空字符串
        :param a_progress: 默认None, 即使用类内部计算的迭代次数进行进度显示
        :param p_format: 进度显示格式,默认{}: {}%,即'self._label:round(self._progress / self._total * 100, 2))%'
        """
        self.progress = a_progress if a_progress is not None else self.progress + 1
        ps = round(self._progress / self._total * 100, 2)

        if self._label is not None:
            # 如果初始化label没有就只显示ui进度
            self.f.write('\r')
            self.f.write(p_format.format(self._label, ext, ps))

        if ABuEnv.g_is_ipython:
            if self.progress_widget is None:
                self.progress_widget = FloatProgress(value=0, min=0, max=100)
                display(self.progress_widget)
            self.progress_widget.value = ps

        # 这样会出现余数结束的情况,还是尽量使用上下文管理器控制结束
        if self._progress == self._total:
            self.f.write('\r')
            if self.progress_widget is not None:
                self.progress_widget.close()
コード例 #25
0
ファイル: neighbors.py プロジェクト: wgong/exatomic
def periodic_nearest_neighbors_by_atom(uni, source, a, sizes, **kwargs):
    """
    Determine nearest neighbor molecules to a given source (or sources) and
    return the data as a dataframe.

    Warning:
        For universes with more than about 250 atoms, consider using the
        slower but more memory efficient
        :func:`~exatomic.algorithms.neighbors.periodic_nearest_neighbors_by_atom_large`.

    For a simple cubic periodic system with unit cell dimension ``a``,
    clusters can be generated as follows. In the example below, additional
    keyword arguments have been included as they are almost always required
    in order to correctly identify molecular units semi-empirically.

    .. code-block:: python

        periodic_nearest_neighbors_by_atom(u, [0], 40.0, [0, 5, 10, 50],
                                           dmax=40.0, C=1.6, O=1.6)

    Argument descriptions can be found below. The additional keyword arguments,
    ``dmax``, ``C``, ``O``, are passed directly to the two body computation used
    to determine (semi-empirically) molecular units. Note that although molecules
    are computed, neighboring molecular units are determine by an atom to atom
    criteria.

    Args:
        uni (:class:`~exatomic.core.universe.Universe`): Universe
        source (int, str, list): Integer label or string symbol of source atom
        a (float): Cubic unit cell dimension
        sizes (list): List of slices to create
        kwargs: Additional keyword arguments to be passed to atom two body calculation

    Returns:
        dct (dict): Dictionary of sliced universes and nearest neighbor table

    See Also:
        Sliced universe construction can be facilitated by
        :func:`~exatomic.algorithms.neighbors.construct`.
    """
    def sorter(group, source_atom_idxs):
        s = group[['atom0', 'atom1']].stack()
        return s[~s.isin(source_atom_idxs)].reset_index()

    if "label" not in uni.atom.columns:
        uni.atom['label'] = uni.atom.get_atom_labels()
    dct = defaultdict(list)
    grps = uni.atom.groupby("frame")
    ntot = len(grps)
    fp = FloatProgress(description="Slicing:")
    display(fp)
    for i, (fdx, atom) in enumerate(grps):
        if len(atom) > 0:
            uu = _create_super_universe(Universe(atom=atom.copy()), a)
            uu.compute_atom_two(**kwargs)
            uu.compute_molecule()
            if isinstance(source, (int, np.int32, np.int64)):
                source_atom_idxs = uu.atom[(uu.atom.index.isin([source])) &
                                           (uu.atom['prj'] == 13)].index.values
            elif isinstance(source, (list, tuple)):
                source_atom_idxs = uu.atom[uu.atom['label'].isin(source) &
                                           (uu.atom['prj'] == 13)].index.values
            else:
                source_atom_idxs = uu.atom[(uu.atom['symbol'] == source) &
                                           (uu.atom['prj'] == 13)].index.values
            source_molecule_idxs = uu.atom.loc[source_atom_idxs,
                                               'molecule'].unique().astype(int)
            uu.atom_two['frame'] = uu.atom_two['atom0'].map(uu.atom['frame'])
            nearest_atoms = uu.atom_two[
                (uu.atom_two['atom0'].isin(source_atom_idxs)) |
                (uu.atom_two['atom1'].isin(source_atom_idxs))].sort_values(
                    "dr")[['frame', 'atom0', 'atom1']]
            nearest = nearest_atoms.groupby("frame").apply(
                sorter, source_atom_idxs=source_atom_idxs)
            del nearest['level_1']
            nearest.index.names = ['frame', 'idx']
            nearest.columns = ['two', 'atom']
            nearest['molecule'] = nearest['atom'].map(uu.atom['molecule'])
            nearest = nearest[~nearest['molecule'].isin(source_molecule_idxs)]
            nearest = nearest.drop_duplicates('molecule', keep='first')
            nearest.reset_index(inplace=True)
            nearest['frame'] = nearest['frame'].astype(int)
            nearest['molecule'] = nearest['molecule'].astype(int)
            dct['nearest'].append(nearest)
            for nn in sizes:
                atm = []
                for j, fdx in enumerate(nearest['frame'].unique()):
                    mdxs = nearest.loc[nearest['frame'] == fdx,
                                       'molecule'].tolist()[:nn]
                    mdxs.append(source_molecule_idxs[j])
                    atm.append(uu.atom[uu.atom['molecule'].isin(mdxs)][[
                        'symbol', 'x', 'y', 'z', 'frame'
                    ]].copy())
                dct[nn].append(pd.concat(atm, ignore_index=True))
        fp.value = i / ntot * 100
    dct['nearest'] = pd.concat(dct['nearest'], ignore_index=True)
    for nn in sizes:
        dct[nn] = Universe(atom=pd.concat(dct[nn], ignore_index=True))
    fp.close()
    return dct
コード例 #26
0
ファイル: ABuProgress.py プロジェクト: 3774257/abu
class AbuProgress(object):
    """单进程(主进程)进度显示控制类"""

    # 过滤DeprecationWarning: Widget._keys_default is deprecated in traitlets 4.1: use @default decorator instead.
    @warnings_filter
    def __init__(self, total, a_progress, label=None):
        """
        外部使用eg:
            progess = AbuProgress(stock_df.shape[0], 0, 'merging {}'.format(m))
            for i, symbol in enumerate(stock_df['symbol']):
                progess.show(i + 1)
        :param total: 总任务数量
        :param a_progress: 初始进度
        :param label: 进度显示label
        """
        self._total = total
        self._progress = a_progress
        self._label = label
        self.f = sys.stdout
        self.progress_widget = None

    def __enter__(self):
        """创建子进程做进度显示"""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.f.write('\r')
        if self.progress_widget is not None:
            self.progress_widget.close()

    @property
    def progress(self):
        """property获取self._progress"""
        return self._progress

    @progress.setter
    def progress(self, a_progress):
        """rogress.setter设置progress"""
        if a_progress > self._total:
            self._progress = self._total
        elif a_progress < 0:
            self._progress = 0
        else:
            self._progress = a_progress

    def show(self, a_progress=None, ext='', p_format="{}:{}:{}%"):
        """
        进行进度控制显示主方法
        :param ext: 可以添加额外的显示文字,str,默认空字符串
        :param a_progress: 默认None, 即使用类内部计算的迭代次数进行进度显示
        :param p_format: 进度显示格式,默认{}: {}%,即'self._label:round(self._progress / self._total * 100, 2))%'
        """
        self.progress = a_progress if a_progress is not None else self.progress + 1
        ps = round(self._progress / self._total * 100, 2)

        if self._label is not None:
            # 如果初始化label没有就只显示ui进度
            self.f.write('\r')
            self.f.write(p_format.format(self._label, ext, ps))

        if ABuEnv.g_is_ipython:
            if self.progress_widget is None:
                self.progress_widget = FloatProgress(value=0, min=0, max=100)
                display(self.progress_widget)
            self.progress_widget.value = ps

        # 这样会出现余数结束的情况,还是尽量使用上下文管理器控制结束
        if self._progress == self._total:
            self.f.write('\r')
            if self.progress_widget is not None:
                self.progress_widget.close()
コード例 #27
0
ファイル: neighbors.py プロジェクト: wgong/exatomic
def periodic_nearest_neighbors_by_atom_large(uni, source, a, sizes, **kwargs):
    """
    Determine nearest neighbor molecules to a given source (or sources) and
    return the data as a dataframe.

    Tip:
        This function performs the same operation as
        :func:`~exatomic.algorithms.neighbors.periodic_nearest_neighbors_by_atom`,
        but is meant for universes containing more than about 250 atoms
        per frame (the referenced function will be faster for smaller universes).

    For a simple cubic periodic system with unit cell dimension ``a``,
    clusters can be generated as follows. In the example below, additional
    keyword arguments have been included as they are almost always required
    in order to correctly identify molecular units semi-empirically.

    .. code-block:: python

        periodic_nearest_neighbors_by_atom_ooc(u, [0], 40.0, [0, 5, 10, 50],
                                           dmax=40.0, C=1.6, O=1.6)

    Argument descriptions can be found below. The additional keyword arguments,
    ``dmax``, ``C``, ``O``, are passed directly to the two body computation used
    to determine (semi-empirically) molecular units. Note that although molecules
    are computed, neighboring molecular units are determine by an atom to atom
    criteria.

    Args:
        uni (:class:`~exatomic.core.universe.Universe`): Universe
        source (int, str, list): Integer label or string symbol of source atom
        a (float): Cubic unit cell dimension
        sizes (iterable): List of slices to create
        kwargs: Additional keyword arguments to be passed to atom two body calculation

    Returns:
        dct (dict): Dictionary of sliced universes and nearest neighbor table

    See Also:
        Sliced universe construction can be facilitated by
        :func:`~exatomic.algorithms.neighbors.construct`.
    """
    if "label" not in uni.atom.columns:
        uni.atom['label'] = uni.atom.get_atom_labels()
    if not isinstance(sizes, list):
        raise TypeError("Argument sizes must be iterable of ints.")
    dct = defaultdict(list)
    grps = uni.atom.groupby("frame")
    ntot = len(grps)
    fp = FloatProgress(description="Slicing:")
    display(fp)
    for i, (fdx, atom) in enumerate(grps):
        if len(atom) > 0:
            uu = Universe(atom=atom.copy())
            uu.frame = uni.frame.loc[[fdx], :]
            uu.compute_atom_two(**kwargs)
            uu.compute_molecule()
            if isinstance(source, (int, np.int32, np.int64)):
                source_atom_idxs = [source]
            elif isinstance(source, np.ndarray):
                source_atom_idxs = source.tolist()
            elif isinstance(source, (list, tuple)):
                source_atom_idxs = uu.atom[uu.atom['label'].isin(source)
                                           | uu.atom['symbol'].
                                           isin(source)].index.astype(
                                               int).tolist()
            else:
                source_atom_idxs = uu.atom[uu.atom['symbol'] ==
                                           source].index.astype(int).tolist()
            source_molecule_idxs = uu.atom.loc[
                source_atom_idxs, 'molecule'].unique().astype(int).tolist()
            # Identify the nearest molecules
            nearest_atoms = uu.atom_two[
                uu.atom_two['atom0'].isin(source_atom_idxs)
                | uu.atom_two['atom1'].isin(source_atom_idxs)].sort_values(
                    'dr')[['atom0', 'atom1']].copy()
            nearest_atoms['molecule0'] = nearest_atoms['atom0'].map(
                uu.atom['molecule'])
            nearest_atoms['molecule1'] = nearest_atoms['atom1'].map(
                uu.atom['molecule'])
            nearest_molecules = nearest_atoms[['molecule0',
                                               'molecule1']].stack()
            nearest_molecules = nearest_molecules[
                ~nearest_molecules.isin(source_molecule_idxs)].drop_duplicates(
                    keep='first')
            # Build the appropriate universes
            for nn in sizes:
                atom1 = uu.atom.loc[uu.atom['molecule'].
                                    isin(nearest_molecules.iloc[:nn].tolist() +
                                         source_molecule_idxs),
                                    ['symbol', 'x', 'y', 'z']]
                adxs, x, y, z, prj = _worker(atom1.index.values.astype(int),
                                             atom1['x'].values.astype(float),
                                             atom1['y'].values.astype(float),
                                             atom1['z'].values.astype(float),
                                             a)
                patom = pd.DataFrame.from_dict({
                    'atom': adxs,
                    'x': x,
                    'y': y,
                    'z': z,
                    'prj': prj
                })
                patom['frame'] = patom['atom'].map(uu.atom['frame'])
                patom['symbol'] = patom['atom'].map(uu.atom['symbol'])
                sliced_u = Universe(atom=patom)
                sliced_u.compute_atom_two(dmax=a)
                sliced_u.compute_molecule()
                source_adxs1 = sliced_u.atom[
                    (sliced_u.atom['prj'] == 13)
                    & sliced_u.atom['atom'].isin(source_atom_idxs)].index
                source_mdxs1 = sliced_u.atom.loc[source_adxs1,
                                                 'molecule'].unique().tolist()
                nearest_atoms1 = sliced_u.atom_two[
                    sliced_u.atom_two['atom0'].isin(source_adxs1) | sliced_u.
                    atom_two['atom1'].isin(source_adxs1)].sort_values('dr')[[
                        'atom0', 'atom1'
                    ]].copy()
                nearest_atoms1['molecule0'] = nearest_atoms1['atom0'].map(
                    sliced_u.atom['molecule'])
                nearest_atoms1['molecule1'] = nearest_atoms1['atom1'].map(
                    sliced_u.atom['molecule'])
                nearest_molecules1 = nearest_atoms1[['molecule0',
                                                     'molecule1']].stack()
                nearest_molecules1 = nearest_molecules1[
                    ~nearest_molecules1.isin(source_mdxs1)].drop_duplicates(
                        keep='first')
                # Its fine to overwrite atom1 above since the uu.atom slice is not necessarily clustered
                atom1 = sliced_u.atom.loc[sliced_u.atom['molecule'].isin(
                    nearest_molecules1.iloc[:nn].tolist() +
                    source_mdxs1)].copy()
                dct[nn].append(atom1)
            index = nearest_molecules.index.get_level_values(0)
            nearest_molecules = nearest_molecules.to_frame()
            nearest_molecules.columns = ['molecule']
            nearest_molecules['frame'] = fdx
            nearest_molecules['atom0'] = nearest_atoms.loc[index,
                                                           'atom0'].values
            nearest_molecules['atom1'] = nearest_atoms.loc[index,
                                                           'atom1'].values
            dct['nearest'].append(nearest_molecules)
        fp.value = i / ntot * 100
    dct['nearest'] = pd.concat(dct['nearest'], ignore_index=True)
    for nn in sizes:
        dct[nn] = Universe(atom=pd.concat(dct[nn], ignore_index=True))
    fp.close()
    return dct
コード例 #28
0
    def read_datalines(self,
                       NSTEPS=0,
                       start_step=-1,
                       select_ckeys=None,
                       max_vector_dim=None,
                       even_NSTEPS=True):
        """
        Read NSTEPS steps of file, starting from start_step, and store only the selected ckeys.

        INPUT:
          NSTEPS         -> number of steps to read (default: 0 -> reads all the file)
          start_step  = -1 -> continue from current step (default)
                         0 -> go to start step
                         N -> go to N-th step
          select_ckeys   -> an array with the column keys you want to read (see all_ckeys for a list)
          max_vector_dim -> when reading vectors read only this number of components (None = read all components)
          even_NSTEPS    -> round the number of steps to an even number (default: True)

        OUTPUT:
          data    ->  a dictionary with the selected-column steps
        """
        if self._GUI:
            progbar = FloatProgress(min=0, max=100)
            display(progbar)
        start_time = time()
        if (NSTEPS == 0):
            NSTEPS = self.MAX_NSTEPS
        self._set_ckey(select_ckeys, max_vector_dim)  # set the ckeys to read
        self._initialize_dic(NSTEPS)  # allocate dictionary
        self.gotostep(start_step)  # jump to the starting step

        # read NSTEPS of the file
        progbar_step = max(100000, int(0.005 * NSTEPS))
        for step in range(NSTEPS):
            line = self.file.readline()
            if (len(line) == 0):  # EOF
                log.write_log('Warning:  reached EOF.')
                break
            if self.endrun_keyword in line:  # end-of-run keyword
                log.write_log('  endrun_keyword found.')
                step -= 1
                break
            values = np.array(line.split())
            if (values.size != self.NALLCKEYS):
                log.write_log(
                    'Warning:  line with wrong number of columns found. Stopping here...'
                )
                log.write_log(line)
                break
            for key, idx in self.ckey.items():  # save the selected columns
                self.data[key][step, :] = np.array(
                    list(map(float, values[idx])))
            if ((step + 1) % progbar_step == 0):
                if self._GUI:
                    progbar.value = float(step + 1) / NSTEPS * 100.
                    progbar.description = '{:6.2f}%'.format(progbar.value)
                else:
                    log.write_log(
                        '    step = {:9d} - {:6.2f}% completed'.format(
                            step + 1,
                            float(step + 1) / NSTEPS * 100.))

        if self._GUI:
            progbar.close()
        # check number of steps read, keep an even number of steps
        if (step + 1 < NSTEPS):
            if (step == 0):
                log.write_log('WARNING:  no step read.')
                return
            else:
                if (NSTEPS != self.MAX_NSTEPS):  # if NSTEPS was specified
                    log.write_log('Warning:  less steps read.')
                NSTEPS = step + 1  # the correct number of read steps
        # even the number of steps
        if even_NSTEPS:
            if (NSTEPS % 2 == 1):
                NSTEPS = NSTEPS - 1
                log.write_log(
                    '  Retaining an even number of steps (even_NSTEPS=True).')
        for key, idx in self.ckey.items():  # free the memory not used
            self.data[key] = self.data[key][:NSTEPS, :]
        log.write_log('  ( %d ) steps read.' % (NSTEPS))
        self.NSTEPS = NSTEPS
        log.write_log('DONE.  Elapsed time: ', time() - start_time, 'seconds')
        return self.data
コード例 #29
0
ファイル: neighbors.py プロジェクト: tjduigna/exatomic
def periodic_nearest_neighbors_by_atom(uni, source, a, sizes, **kwargs):
    """
    Determine nearest neighbor molecules to a given source (or sources) and
    return the data as a dataframe.

    For a simple cubic periodic system with unit cell dimension ``a``,
    clusters can be generated as follows. In the example below, additional
    keyword arguments have been included as they are almost always required
    in order to correctly identify molecular units semi-empirically.

    .. code-block:: python

        periodic_nearest_neighbors_by_atom(u, [0], 40.0, [0, 5, 10, 50],
                                           dmax=40.0, C=1.6, O=1.6)

    Argument descriptions can be found below. The additional keyword arguments,
    ``dmax``, ``C``, ``O``, are passed directly to the two body computation used
    to determine (semi-empirically) molecular units. Note that although molecules
    are computed, neighboring molecular units are determine by an atom to atom
    criteria.

    Args:
        uni (:class:`~exatomic.core.universe.Universe`): Universe
        source (int, str, list): Integer label or string symbol of source atom
        a (float): Cubic unit cell dimension
        sizes (list): List of slices to create
        kwargs: Additional keyword arguments to be passed to atom two body calculation

    Returns:
        dct (dict): Dictionary of sliced universes and nearest neighbor table

    See Also:
        Sliced universe construction can be facilitated by
        :func:`~exatomic.algorithms.neighbors.construct`.
    """
    def sorter(group, source_atom_idxs):
        s = group[['atom0', 'atom1']].stack()
        return s[~s.isin(source_atom_idxs)].reset_index()

    if "label" not in uni.atom.columns:
        uni.atom['label'] = uni.atom.get_atom_labels()
    dct = defaultdict(list)
    grps = uni.atom.groupby("frame")
    ntot = len(grps)
    fp = FloatProgress(description="Slicing:")
    display(fp)
    for i, (fdx, atom) in enumerate(grps):
        if len(atom) > 0:
            uu = _create_super_universe(Universe(atom=atom.copy()), a)
            uu.compute_atom_two(**kwargs)
            uu.compute_molecule()
            if isinstance(source, (int, np.int32, np.int64)):
                source_atom_idxs = uu.atom[(uu.atom.index.isin([source])) &
                                           (uu.atom['prj'] == 13)].index.values
            elif isinstance(source, (list, tuple)):
                source_atom_idxs = uu.atom[uu.atom['label'].isin(source) &
                                           (uu.atom['prj'] == 13)].index.values
            else:
                source_atom_idxs = uu.atom[(uu.atom['symbol'] == source) &
                                           (uu.atom['prj'] == 13)].index.values
            source_molecule_idxs = uu.atom.loc[source_atom_idxs, 'molecule'].unique().astype(int)
            uu.atom_two['frame'] = uu.atom_two['atom0'].map(uu.atom['frame'])
            nearest_atoms = uu.atom_two[(uu.atom_two['atom0'].isin(source_atom_idxs)) |
                                        (uu.atom_two['atom1'].isin(source_atom_idxs))].sort_values("dr")[['frame', 'atom0', 'atom1']]
            nearest = nearest_atoms.groupby("frame").apply(sorter, source_atom_idxs=source_atom_idxs)
            del nearest['level_1']
            nearest.index.names = ['frame', 'idx']
            nearest.columns = ['two', 'atom']
            nearest['molecule'] = nearest['atom'].map(uu.atom['molecule'])
            nearest = nearest[~nearest['molecule'].isin(source_molecule_idxs)]
            nearest = nearest.drop_duplicates('molecule', keep='first')
            nearest.reset_index(inplace=True)
            nearest['frame'] = nearest['frame'].astype(int)
            nearest['molecule'] = nearest['molecule'].astype(int)
            dct['nearest'].append(nearest)
            for nn in sizes:
                atm = []
                for j, fdx in enumerate(nearest['frame'].unique()):
                    mdxs = nearest.loc[nearest['frame'] == fdx, 'molecule'].tolist()[:nn]
                    mdxs.append(source_molecule_idxs[j])
                    atm.append(uu.atom[uu.atom['molecule'].isin(mdxs)][['symbol', 'x', 'y', 'z', 'frame']].copy())
                dct[nn].append(pd.concat(atm, ignore_index=True))
        fp.value = i/ntot*100
    dct['nearest'] = pd.concat(dct['nearest'], ignore_index=True)
    for nn in sizes:
        dct[nn] = Universe(atom=pd.concat(dct[nn], ignore_index=True))
    fp.close()
    return dct
コード例 #30
0
def flowLines(F, Ipb, LAT, flow):
    f = F[0]  ## sympy function, matrix valued.
    k = len(F[1])  ## number of parameters to f. F[1] is the variable names.
    ## F[2] is the integration bounds.

    Df = sp.zeros(3, k)
    for i in range(k):
        Df[:, i] = sp.diff(f, F[1][i])
    Cf = sp.sqrt((Df.transpose() * Df).det()).simplify()
    ## Cf is the length/area/volume distortion of f.

    x, y, z = sp.symbols('x y z')
    P = sp.Matrix([x, y, z])
    ## we have to push I through Df to get the current vector field.
    I = Df * Ipb
    den = sp.Matrix(f - P)
    ITG = I.cross(den)
    den = den.dot(den)**(3 / 2)
    ITG = (ITG / den)

    ## let's make an entire routine that does Euler's method callable?
    ## this is possibly something to experiment with, the level of abstraction.
    ## we could also just make the integration command callable.
    ITGc = [ufuncify(F[1] + [x, y, z], ITG[i, 0]) for i in range(3)]

    ## compile the curve as well, as we need to check distances
    C = [ufuncify(F[1], F[0][i, 0]) for i in range(3)]

    ## loop running through the lattice [X,Y,Z]=LAT computing flowlines at those
    ##  coordinates.
    retval = []
    fp = FloatProgress(min=0, max=100, description="Flowlines")
    display(fp)
    ## progrss indicator

    count = 0
    totc = LAT[0].shape[0] * LAT[0].shape[1] * LAT[0].shape[2]
    for i, j, k in it.product(range(LAT[0].shape[0]), range(LAT[0].shape[1]),
                              range(LAT[0].shape[2])):
        # TODO: let's throw in a basic check to see if this starting coordinate
        #  is too close to the curve.
        errFlag = False
        flowline = [[LAT[0][i, j, k]], [LAT[1][i, j, k]], [LAT[2][i, j, k]]]
        for s in range(flow[1]):
            dp = [
                INT.nquad(ITGc[l],
                          F[2],
                          args=(flowline[0][-1], flowline[1][-1],
                                flowline[2][-1]),
                          opts=[{
                              'epsrel': 0
                          }, {
                              'epsabs': 1e-3
                          }])[0] for l in range(3)
            ]
            ## here is a primitive check to see if our lattice point is too close to
            ## the curve.  Should be replaced with something more sophisticated... later.
            ldp = np.sqrt(sum([crd**2 for crd in dp]))

            if (ldp > 1e+8):
                errFlag = True
                break
            ## assemble our flowline
            for l in range(3):
                flowline[l].append(flowline[l][-1] + flow[0] * dp[l] / ldp)
        count += 1
        fp.value = int(100 * count / totc)

        ## let's not build the flow line if ldp is too large.
        if errFlag == False:
            retval.append(flowline)
    fp.close()

    return retval, C
コード例 #31
0
        def array_slice(self, path, start, stop, modified):
            #Convert the (whole!) h5 dataset to an numpy array and slice immediately.
            #Check if input is only one h5 file or multiple
            if isinstance(self.dict, File):
                electrode_info = np.asarray(self.dict['mapping']['channel',
                                                                 'electrode'])
                mask = electrode_info['electrode'] != -1
                clean_rel_inds = electrode_info['channel'][mask]

                if modified == True:
                    stop = 'end'
                    self.metadata['modified'] = True
                else:
                    self.metadata['modified'] = False

                if stop == 'end':
                    if modified == False:
                        traces = np.asarray(self.dict['sig'])
                        self.metadata['DAC'] = traces[1024, start:]
                        traces = traces[clean_rel_inds,
                                        start:]  #crop the traces
                        self.metadata['time'] = np.arange(
                            0, (self.dict['sig'].shape[1] - start) / 20000.,
                            1 / 20000.)
                    else:
                        traces = np.asarray(self.dict['sig'])[:, start:]
                        self.metadata['time'] = self.dict['time'][:]
                else:
                    traces = np.asarray(self.dict['sig'])
                    self.metadata['DAC'] = traces[1024, start:stop]
                    traces = traces[clean_rel_inds, start:stop]
                    self.metadata['time'] = np.arange(0,
                                                      (stop - start) / 20000.,
                                                      1 / 20000.)

            if isinstance(self.dict, list):
                electrode_info = [
                    np.asarray(i['mapping']['channel', 'electrode'])
                    for i in self.dict
                ]
                mask = [i['electrode'] != -1 for i in electrode_info]
                clean_rel_inds = [
                    i[0]['channel'][i[1]] for i in zip(electrode_info, mask)
                ]

                traces = []

                progr = FloatProgress(min=0,
                                      max=len(self.dict),
                                      description='Importing...',
                                      bar_style='success')
                display(progr)

                if modified == True:
                    stop = 'end'
                    for i in range(len(clean_rel_inds)):
                        self.metadata[i]['modified'] = True
                else:
                    for i in range(len(clean_rel_inds)):
                        self.metadata[i]['modified'] = False

                if stop == 'end':
                    for i, v in enumerate(clean_rel_inds):

                        if modified == False:
                            raw_trace = np.asarray(self.dict[i]['sig'])
                            self.metadata[i]['DAC'] = raw_trace[1024, start:]
                            traces.append(raw_trace[v, start:])
                            self.metadata[i]['time'] = np.arange(
                                0, (self.dict[i]['sig'].shape[1] - start) /
                                20000., 1 / 20000.)
                        else:
                            traces.append(
                                np.asarray(self.dict[i]['sig'])[:, start:])
                            self.metadata[i]['time'] = self.dict[i]['time'][:]
                        progr.value += 1
                else:
                    for i, v in enumerate(clean_rel_inds):
                        raw_trace = np.asarray(self.dict[i]['sig'])
                        self.metadata[i]['DAC'] = raw_trace[1024, start:]
                        traces.append(raw_trace[v, start:stop])
                        self.metadata[i]['time'] = np.arange(
                            0, (stop - start) / 20000., 1 / 20000.)
                        progr.value += 1

                progr.close()

            return traces