Beispiel #1
0
    def initialize(self, args):

        # read structure file
        struct_file = reader.open(args.struct_file, velocities=True)
        self.struct_filename = struct_file.filename
        self.coords = struct_file.readlines()

        self.npoints = self.coords.shape[0]

        # read number of copies
        ncfile = reader.open(args.ncfile)
        self.ncopiess = ncfile.readlines() 

        # read wfile if exists

        if args.wfile is None:
            self.weights = np.ones(self.npoints, dtype='float')
        else:
            if os.path.isfile(args.wfile):
                wfile = reader.open(args.wfile)
                self.weights = wfile.readlines()
            else:
                self.weights = np.ones(self.npoints, dtype='float')

        assert self.ncopiess.size == self.npoints, "the number of lines of .nc file should be equal to the number\
                                                   of configurations in the structure file. %i/%i" %(self.ncopiess.size, self.npoints)
        assert self.weights.size == self.npoints, "the number of lines of .w file should be equal to the number\
                                                   of configurations in the structure file. %i/%i" %(self.weights.size, self.npoints)
Beispiel #2
0
    def initialize(self, args):

        # read structure file
        with open(args.struct_file[0]) as f:

            self.coords = f.readlines()

        self.npoints = len(self.coords)

        # read number of copies
        ncfile = reader.open(args.ncfile)
        self.ncopiess = ncfile.readlines()

        # read wfile if exists

        if args.wfile is None:
            self.weights = np.ones(self.npoints, dtype="float")
        else:
            if os.path.isfile(args.wfile):
                wfile = reader.open(args.wfile)
                self.weights = wfile.readlines()
            else:
                self.weights = np.ones(self.npoints, dtype="float")

        assert self.ncopiess.size == self.npoints, (
            "the number of lines of .nc file should be equal to the number\
                                                   of configurations in the structure file. %i/%i"
            % (self.ncopiess.size, self.npoints)
        )
        assert self.weights.size == self.npoints, (
            "the number of lines of .w file should be equal to the number\
                                                   of configurations in the structure file. %i/%i"
            % (self.weights.size, self.npoints)
        )
    def initialize(self, args):

        # read structure file
        struct_file = reader.open(args.struct_file,
                                  velocities=False)  #, velocities=True)
        self.struct_filename = struct_file.filename
        self.coords = struct_file.readlines()

        self.npoints = self.coords.shape[0]

        # read number of copies
        ncfile = reader.open(args.ncfile)
        self.ncopiess = ncfile.readlines()

        # read wfile if exists

        if args.wfile is None:
            self.weights = np.ones(self.npoints, dtype='float')
        else:
            if os.path.isfile(args.wfile):
                wfile = reader.open(args.wfile)
                self.weights = wfile.readlines()
            else:
                self.weights = np.ones(self.npoints, dtype='float')

        assert self.ncopiess.size == self.npoints, "the number of lines of .nc file should be equal to the number\
                                                   of configurations in the structure file. %i/%i" % (
            self.ncopiess.size, self.npoints)
        assert self.weights.size == self.npoints, "the number of lines of .w file should be equal to the number\
                                                   of configurations in the structure file. %i/%i" % (
            self.weights.size, self.npoints)
Beispiel #4
0
    def initialize_local_scale(self):

        config = self.config
        args = self.args

        known_status = ['constant', 'kneighbor', 'user', 'kneighbor_mean']
        _mapped = {'const': 'constant', 'cst': 'constant', 'mean-kneighbor': 'mean_kneighbor'}

        if args.epsfile is None:
            status = config.get('LOCALSCALE','status')
            if status in _mapped:
                status = _mapped[status]
            if not status in known_status:
                raise ValueError("local scale status should be one of "+ ', '.join(known_status))
            if status in ['kneighbor', 'kneighbor_mean']:
                value = None
                self.k = config.getint('LOCALSCALE', 'k')
            if status == 'constant':
                value = config.getfloat('LOCALSCALE', 'epsilon')
                value = value*np.ones(self.npoints)
            if status == 'user':
                raise NameError("status 'user' specified in configuration file but epsfile not provided. Please consider option flag -e")
        else:
            espfile = reader.open(epsfile)
            status ='user'
            value = epsfile.readlines()

        self.status_epsilon = status
        self.epsilon = value
Beispiel #5
0
    def initialize(self, comm, config, args):

        rank = comm.Get_rank()

        self.config = config
        self.args = args

        struct_file = reader.open(args.struct_file)
        self.struct_filename = struct_file.filename
        self.npoints = struct_file.nlines

        self.idxs_thread = p_index.get_idxs_thread(comm, self.npoints)

        if hasattr(struct_file, '_skip'): # multi-thread reading
            coords_thread = struct_file.readlines(self.idxs_thread)
            self.coords = np.vstack(comm.allgather(coords_thread))
        else: # serial reading
            if rank == 0:
                self.coords = struct_file.readlines()
            else:
                self.coords = None
            self.coords = comm.bcast(self.coords, root=0) 

        logging.info('input coordinates loaded')

        self.initialize_local_scale()
        self.initialize_weights()
        self.initialize_metric()

        self.neigs = 10
Beispiel #6
0
    def run(self):

        parser = self.create_arg_parser()
        args = parser.parse_args()

        evfile = reader.open(args.evfile)
        evs = evfile.readlines()

        npoints = evs.shape[0]

        ev1s = evs[:,1]
        ev2s = evs[:,2]

        # minimum and maximum 1st DC values used
        min_ev1s = np.amin(ev1s)
        print "DC1 min = %.5f" %min_ev1s
        max_ev1s = np.amax(ev1s)
        print "DC1 max = %.5f" %max_ev1s

        # create a histogram with the values of the first DCs
        nbins = int(np.sqrt(npoints)/2)
        bins = min_ev1s + np.array(range(nbins+1)) * (max_ev1s - min_ev1s)/nbins
        # slightly change the size of the last bin to include max_ev1s
        bins[-1] += 1e-6
        # inds contains the idx of the bin of each point
        inds = np.digitize(ev1s, bins) - 1
        hist = [[] for i in range(nbins)]
        for idx, ind in enumerate(inds):
            hist[ind].append(idx) # create histogram with the idx of each point

        ncopiess = np.zeros(npoints, dtype='i')

        for idx in xrange(args.npoints):

            # sample a random number according to a uniform distribution between min_ev1s and max_ev1s
            sample = np.random.rand() * (max_ev1s - min_ev1s) + min_ev1s
            ind = np.digitize([sample], bins) - 1
            ind = ind[0] # find the index of the bin where the sample is located
            while len(hist[ind]) == 0: # while there is no endpoints in the bin...
                sample = np.random.rand() * (max_ev1s - min_ev1s) + min_ev1s # ... pick another sample
                ind = np.digitize([sample], bins) - 1
                ind = ind[0] # find the index of the bin where the sample is located

            # choose the new starting point in order to have a uniform distribution of points along the 2nd DC within the bin
            ev2s_bin = [ev2s[idx] for idx in hist[ind]]
            min_ev2s = np.amin(ev2s_bin)
            max_ev2s = np.amax(ev2s_bin)

            sample = np.random.rand() * (max_ev2s - min_ev2s) + min_ev2s
            idx_sample = (np.abs(ev2s_bin - sample)).argmin()
            jdx = hist[ind][idx_sample] 
            ncopiess[jdx] += 1

        if args.ncfile is None:
            args.ncfile = os.path.splitext(args.evfile)[0] + '.nc'
        with open(args.ncfile, 'w') as ncfile:
            for ncopies in ncopiess:
                print >> ncfile, ncopies

        print "number of copies stored in %s" %args.ncfile
    def run(self):

        parser = self.create_arg_parser()
        args = parser.parse_args()

        evfile = reader.open(args.evfile)
        evs = evfile.readlines()

        npoints = evs.shape[0]

        ev1s = evs[:,1]
        ev2s = evs[:,2]

        # minimum and maximum 1st DC values used
        min_ev1s = np.amin(ev1s)
        print "minimum probability at %.5f" %min_ev1s
        max_ev1s = np.amax(ev1s)
        print "maximum probability at %.5f" %max_ev1s

        # create a histogram with the values of the first DCs
        nbins = int(np.sqrt(npoints)/2)
        bins = min_ev1s + np.array(range(nbins+1)) * (max_ev1s - min_ev1s)/nbins
        # slightly change the size of the last bin to include max_ev1s
        bins[-1] += 1e-6
        # inds contains the idx of the bin of each point
        inds = np.digitize(ev1s, bins) - 1
        hist = [[] for i in range(nbins)]
        for idx, ind in enumerate(inds):
            hist[ind].append(idx) # create histogram with the idx of each point

        ncopiess = np.zeros(npoints, dtype='i')

        for idx in xrange(args.npoints):

            # sample a random number according to a uniform distribution between min_ev1s and max_ev1s
            sample = np.random.rand() * (max_ev1s - min_ev1s) + min_ev1s
            ind = np.digitize([sample], bins) - 1
            ind = ind[0] # find the index of the bin where the sample is located
            while len(hist[ind]) == 0: # while there is no endpoints in the bin...
                sample = np.random.rand() * (max_ev1s - min_ev1s) + min_ev1s # ... pick another sample
                ind = np.digitize([sample], bins) - 1
                ind = ind[0] # find the index of the bin where the sample is located

            # choose the new starting point in order to have a uniform distribution of points along the 2nd DC within the bin
            ev2s_bin = [ev2s[idx] for idx in hist[ind]]
            min_ev2s = np.amin(ev2s_bin)
            max_ev2s = np.amax(ev2s_bin)

            sample = np.random.rand() * (max_ev2s - min_ev2s) + min_ev2s
            idx_sample = (np.abs(ev2s_bin - sample)).argmin()
            jdx = hist[ind][idx_sample] 
            ncopiess[jdx] += 1

        if args.ncfile is None:
            args.ncfile = os.path.splitext(args.evfile)[0] + '.nc'
        with open(args.ncfile, 'w') as ncfile:
            for ncopies in ncopiess:
                print >> ncfile, ncopies

        print "number of copies stored in %s" %args.ncfile
Beispiel #8
0
    def initialize_weights(self):

        config = self.config
        args = self.args

        if args.wfile is None:
            self.weights = np.ones(self.npoints, dtype='float')
        else:
            if os.path.isfile(args.wfile):
                wfile = reader.open(args.wfile)
                weights = wfile.readlines()
                self.weights = self.npoints*weights/np.sum(weights)
            else:
                logging.warning('.w file does not exist, set weights to 1.0.')
                self.weights = np.ones(self.npoints, dtype='float')
Beispiel #9
0
    def initialize(self, comm, config, args):

        rank = comm.Get_rank()
        size = comm.Get_size()

        self.config = config
        self.args = args

        filename = args.struct_file[0] 
        self.struct_filename = filename
        self.npoints,self.natoms = coord_reader.get_nframes_natoms(filename)

        if coord_reader.supports_parallel_reading(filename): 
            # read coordinates in parallel
            self.idxs_thread, self.npoints_per_thread, self.offsets_per_thread = p_index.get_idxs_thread(comm, self.npoints)
            coords_thread = coord_reader.get_coordinates(filename, idxs=self.idxs_thread)
            coords_ravel = coords_thread.ravel()
            ravel_lengths, ravel_offsets = p_index.get_ravel_offsets(self.npoints_per_thread,self.natoms)
            coordstemp = np.zeros(self.npoints*3*self.natoms, dtype='float')
            start = MPI.Wtime()
            comm.Allgatherv(coords_ravel, (coordstemp, ravel_lengths, ravel_offsets, MPI.DOUBLE))
            self.coords = coordstemp.reshape((self.npoints,3,self.natoms))
        else: 
            # serial reading
            if rank == 0:
                self.coords = coord_reader.get_coordinates(filename)
            else:
                self.coords = np.zeros((self.npoints,3,self.natoms),dtype=np.double)
            comm.Bcast(self.coords, root=0) 

        logging.info('input coordinates loaded')

        # load file of values
        valfile = reader.open(args.valfile)
        self.values = valfile.readlines()

        format = os.path.splitext(args.valfile)[1]
        if format == '.ev':
            self.fitdcs = True
        else:
            self.fitdcs = False
            if len(self.values.shape) > 2:
                raise ValueError('file of values should contain a single column')

        self.initialize_metric()
        self.function = config.get(self.args.section,'function')

        self.status_sigma = config.get(self.args.section,'status')
        if self.status_sigma == 'constant':
            self.sigma = config.getfloat(self.args.section,'sigma')
            self.ksigma = None
        elif self.status_sigma == 'kneighbor':
            self.sigma = None
            self.ksigma = config.getint(self.args.section,'ksigma')

        if args.embed_file is not None:
            #embed_file = reader.open(args.embed_file)
            #self.embed_filename = embed_file.filename
            #self.npoints_embed = embed_file.nlines
            #self.idxs_thread_embed = p_index.get_idxs_thread_v(comm, self.npoints_embed)
            #if hasattr(embed_file, '_skip'): # multi-thread reading
            #    coords_thread_embed = embed_file.readlines(self.idxs_thread_embed)
            #    self.coords_embed = np.vstack(comm.allgather(coords_thread_embed))
            #else: # serial reading
            #    if rank == 0:
            #        self.coords_embed = embed_file.readlines()
            #    else:
            #        self.coords_embed = None
            #    self.coords_embed = comm.bcast(self.coords_embed, root=0)
            filename_embed = args.embed_file
            self.embed_filename = filename_embed
            self.npoints_embed,self.natoms_embed = coord_reader.get_nframes_natoms(filename_embed)
            if coord_reader.supports_parallel_reading(filename_embed): 
                # read coordinates in parallel
                self.idxs_thread_embed, self.npoints_per_thread_embed, self.offsets_per_thread_embed = p_index.get_idxs_thread(comm, self.npoints_embed)
                coords_thread_embed = coord_reader.get_coordinates(filename_embed, idxs=self.idxs_thread_embed)
                coords_ravel_embed = coords_thread_embed.ravel()
                ravel_lengths, ravel_offsets = p_index.get_ravel_offsets(self.npoints_per_thread_embed,self.natoms_embed)
                coordstemp_embed = np.zeros(self.npoints_embed*3*self.natoms_embed, dtype='float')
                comm.Allgatherv(coords_ravel, (coordstemp_embed, ravel_lengths, ravel_offsets, MPI.DOUBLE))
                self.coords_embed = coordstemp_embed.reshape((self.npoints_embed,3,self.natoms_embed))
            else: 
                # serial reading
                if rank == 0:
                    self.coords_embed = coord_reader.get_coordinates(filename_embed)
                else:
                    self.coords_embed = np.zeros((self.npoints_embed,3,self.natoms_embed),dtype=np.double)
                comm.Bcast(self.coords_embed, root=0) 
Beispiel #10
0
def run():

    parser = create_arg_parser()
    args = parser.parse_args()

    #read input files

    x = []

    for input_file in args.input_file_x: 
        file = reader.open(input_file)
        vals = file.readlines()
        # using readlines() of lsdmap reader, each row of x is a list
        # corresponding to the coordinates of each config, thus
        # needs to concatenate.
        vals = np.concatenate(vals, axis=0)
        x.append(vals)

    x = np.hstack(np.array(x))

    y = []
    for input_file in args.input_file_y:
        file = reader.open(input_file)
        vals = file.readlines()
        # using readlines() of lsdmap reader, each row of y is a list
        # corresponding to the coordinates of each config, thus
        # needs to concatenate.
        vals = np.concatenate(vals, axis=0)
        y.append(vals)

    y = np.hstack(np.array(y)) 


    if not args.nbins:
        npoints = x.shape[0]
        nbins = int(np.sqrt(npoints))
    else:
        nbins = args.nbins

    if args.wfile is not None:
        weight = []
        for wfile in args.wfile:
            file = reader.open(wfile)
            vals = file.readlines()
            weight.append(vals)
        weight = np.hstack(np.array(weight))
    else:
        weight = None


    plot_style = args.plot_style
    free_energy_plot = args.free_energy_plot
    idx_smoothing = args.idx_smoothing 

    x, y, z = hist2d.make(x, y, nbins, nbins, weight=weight, plot_style=plot_style, free_energy_plot=free_energy_plot, idx_smoothing=idx_smoothing)

    if plot_style == 'scatter':
        cp = plt.scatter(x, y, s=10, c=z, marker='o', linewidth=0., vmax=3.5)
    elif plot_style == 'contour':
        cp = plt.contourf(x, y, z)
        plt.contour(x, y, z, cp.levels, colors='k', hold='on')

    plt.xlabel(args.xlabel, size=16)
    plt.ylabel(args.ylabel, size=16)

    cb = plt.colorbar(cp)
    pad = 10
    if free_energy_plot is True:
        cb.set_label(r'Free Energy (kcal/mol)',labelpad=pad)
    else:
        cb.set_label(r'Density', labelpad=pad)

    plt.show()
Beispiel #11
0
    def initialize(self, comm, config, args):

        rank = comm.Get_rank()
        size = comm.Get_size()

        self.config = config
        self.args = args

        filename = args.struct_file[0]
        self.struct_filename = filename
        self.npoints, self.natoms = coord_reader.get_nframes_natoms(filename)

        if coord_reader.supports_parallel_reading(filename):
            # read coordinates in parallel
            self.idxs_thread, self.npoints_per_thread, self.offsets_per_thread = p_index.get_idxs_thread(
                comm, self.npoints)
            coords_thread = coord_reader.get_coordinates(filename,
                                                         idxs=self.idxs_thread)
            coords_ravel = coords_thread.ravel()
            ravel_lengths, ravel_offsets = p_index.get_ravel_offsets(
                self.npoints_per_thread, self.natoms)
            coordstemp = np.zeros(self.npoints * 3 * self.natoms,
                                  dtype='float')
            start = MPI.Wtime()
            comm.Allgatherv(
                coords_ravel,
                (coordstemp, ravel_lengths, ravel_offsets, MPI.DOUBLE))
            self.coords = coordstemp.reshape((self.npoints, 3, self.natoms))
        else:
            # serial reading
            if rank == 0:
                self.coords = coord_reader.get_coordinates(filename)
            else:
                self.coords = np.zeros((self.npoints, 3, self.natoms),
                                       dtype=np.double)
            comm.Bcast(self.coords, root=0)

        logging.info('input coordinates loaded')

        # load file of values
        valfile = reader.open(args.valfile)
        self.values = valfile.readlines()

        format = os.path.splitext(args.valfile)[1]
        if format == '.ev':
            self.fitdcs = True
        else:
            self.fitdcs = False
            if len(self.values.shape) > 2:
                raise ValueError(
                    'file of values should contain a single column')

        self.initialize_metric()
        self.function = config.get(self.args.section, 'function')

        self.status_sigma = config.get(self.args.section, 'status')
        if self.status_sigma == 'constant':
            self.sigma = config.getfloat(self.args.section, 'sigma')
            self.ksigma = None
        elif self.status_sigma == 'kneighbor':
            self.sigma = None
            self.ksigma = config.getint(self.args.section, 'ksigma')

        if args.embed_file is not None:
            #embed_file = reader.open(args.embed_file)
            #self.embed_filename = embed_file.filename
            #self.npoints_embed = embed_file.nlines
            #self.idxs_thread_embed = p_index.get_idxs_thread_v(comm, self.npoints_embed)
            #if hasattr(embed_file, '_skip'): # multi-thread reading
            #    coords_thread_embed = embed_file.readlines(self.idxs_thread_embed)
            #    self.coords_embed = np.vstack(comm.allgather(coords_thread_embed))
            #else: # serial reading
            #    if rank == 0:
            #        self.coords_embed = embed_file.readlines()
            #    else:
            #        self.coords_embed = None
            #    self.coords_embed = comm.bcast(self.coords_embed, root=0)
            filename_embed = args.embed_file
            self.embed_filename = filename_embed
            self.npoints_embed, self.natoms_embed = coord_reader.get_nframes_natoms(
                filename_embed)
            if coord_reader.supports_parallel_reading(filename_embed):
                # read coordinates in parallel
                self.idxs_thread_embed, self.npoints_per_thread_embed, self.offsets_per_thread_embed = p_index.get_idxs_thread(
                    comm, self.npoints_embed)
                coords_thread_embed = coord_reader.get_coordinates(
                    filename_embed, idxs=self.idxs_thread_embed)
                coords_ravel_embed = coords_thread_embed.ravel()
                ravel_lengths, ravel_offsets = p_index.get_ravel_offsets(
                    self.npoints_per_thread_embed, self.natoms_embed)
                coordstemp_embed = np.zeros(self.npoints_embed * 3 *
                                            self.natoms_embed,
                                            dtype='float')
                comm.Allgatherv(coords_ravel, (coordstemp_embed, ravel_lengths,
                                               ravel_offsets, MPI.DOUBLE))
                self.coords_embed = coordstemp_embed.reshape(
                    (self.npoints_embed, 3, self.natoms_embed))
            else:
                # serial reading
                if rank == 0:
                    self.coords_embed = coord_reader.get_coordinates(
                        filename_embed)
                else:
                    self.coords_embed = np.zeros(
                        (self.npoints_embed, 3, self.natoms_embed),
                        dtype=np.double)
                comm.Bcast(self.coords_embed, root=0)