Ejemplo n.º 1
0
    def _load(self, idx):
        sample_set = SampleSet(
            self.vars['samples'][idx],
            movepath=LoaderProxy(self.storage.pathmovechanges, int(self.variables['movepath'][idx]))
        )

        return sample_set
Ejemplo n.º 2
0
    def _add_empty_to_cache(self, idx, trajectory_idx, replica_idx, bias,
                           ensemble_idx, parent_idx, details_idx, mover_idx):
        storage = self.storage
        obj = Sample(
            trajectory=storage.trajectories[int(trajectory_idx)],
            replica=int(replica_idx),
            bias=float(bias),
            ensemble=storage.ensembles[int(ensemble_idx)],
            mover=storage.pathmovers[int(mover_idx)],
            parent=LoaderProxy(storage.samples, int(parent_idx)),
            details=LoaderProxy(storage.details, int(details_idx))
        )

        self.index[obj] = idx
        self.cache[idx] = obj

        return obj
Ejemplo n.º 3
0
    def _add_empty_to_cache(self, idx, sample_idxs, pmc_idx):
        if idx not in self.cache:
            obj = SampleSet(
                samples=[self.storage.samples[sample_idx.tolist()] for sample_idx in sample_idxs],
                movepath=LoaderProxy(self.storage.pathmovechanges, int(pmc_idx))
            )

            self.index[obj] = idx
            self.cache[idx] = obj
Ejemplo n.º 4
0
    def _save(self, trajectory, idx):
        self.vars['snapshots'][idx] = trajectory
        snapshot_store = self.storage.snapshots

        for frame, snapshot in enumerate(trajectory):
            if type(snapshot) is not LoaderProxy:
                loader = LoaderProxy(snapshot_store,
                                     snapshot_store.index[snapshot])
                trajectory[frame] = loader
Ejemplo n.º 5
0
    def _load(self, idx):
        """
        Load a snapshot from the storage.

        Parameters
        ----------
        idx : int
            the integer index of the snapshot to be loaded

        Returns
        -------
        snapshot : Snapshot
            the loaded snapshot instance
        """

        s_idx = int(idx / 2) * 2

        reversed_idx = 2 * s_idx + 1 - idx
        try:
            obj = self.cache[reversed_idx]
            snapshot = Snapshot(
                configuration=obj.configuration,
                momentum=obj.momentum,
                is_reversed=not obj.is_reversed,
                reversed_copy=LoaderProxy(self, reversed_idx)
            )
            return snapshot


        except KeyError:
            pass

        configuration = self.vars['configuration'][s_idx]
        momentum = self.vars['momentum'][s_idx]
        momentum_reversed = self.vars['momentum_reversed'][idx]

        snapshot = Snapshot(
            configuration=configuration,
            momentum=momentum,
            is_reversed=momentum_reversed,
            reversed_copy=LoaderProxy(self, reversed_idx)
        )

        return snapshot
Ejemplo n.º 6
0
    def _load(self, idx):
        cls_name = self.vars['cls'][idx]

        cls = self.class_list[cls_name]
        obj = cls.__new__(cls)
        PathMoveChange.__init__(obj, mover=self.vars['mover'][idx])

        obj.samples = self.vars['samples'][idx]
        obj.subchanges = self.vars['subchanges'][idx]

        obj.details = LoaderProxy(self.storage.details,
                                  int(self.variables['details'][idx]))

        return obj
Ejemplo n.º 7
0
    def _load_partial_samples(self, cls_name, samples_idxs, mover_idx,
                              details_idx):
        cls = self.class_list[cls_name]
        obj = cls.__new__(cls)
        PathMoveChange.__init__(obj,
                                mover=self.storage.pathmovers[int(mover_idx)])

        if len(samples_idxs) > 0:
            obj.samples = [
                self.storage.samples[int(idx)] for idx in samples_idxs
            ]

        obj.details = LoaderProxy(self.storage.details, int(details_idx))

        return obj
Ejemplo n.º 8
0
    def _save(self, snapshot, idx):
        """
        Add the current state of the snapshot in the database.

        Parameters
        ----------
        snapshot : Snapshot()
            the snapshot to be saved
        idx : int or None
            if idx is not None the index will be used for saving in the storage.
            This might overwrite already existing trajectories!

        Notes
        -----
        This also saves all contained frames in the snapshot if not done yet.
        A single Snapshot object can only be saved once!
        """

        s_idx = int(idx / 2) * 2

        reversed_idx = 2 * s_idx + 1 - idx

        self.vars['configuration'][s_idx + 1] = snapshot.configuration
        self.vars['momentum'][s_idx + 1] = snapshot.momentum
        self.write('configuration', s_idx, snapshot)
        self.write('momentum', s_idx, snapshot)

        self.vars['momentum_reversed'][idx] = snapshot.is_reversed
        self.vars['momentum_reversed'][reversed_idx] = not snapshot.is_reversed

        reversed = snapshot._reversed
        snapshot._reversed = LoaderProxy(self, reversed_idx)
        reversed._reversed = LoaderProxy(self, idx)

        # mark reversed as stored
        self.index[reversed] = reversed_idx
Ejemplo n.º 9
0
 def all(self):
     return Trajectory([LoaderProxy(self, idx) for idx in range(len(self))])
Ejemplo n.º 10
0
    def from_samples(self, samples, clear=True):
        if len(samples) == 0:
            # no samples, nothing to do
            # TODO: Raise an exception or just ignore and don't output anything?
            return

        p_x = dict()
        p_y = dict()

        if clear:
            self.renderer.clear()

        t_count = 1
        shift = 0

        lightcolor = "gray"
        font_color = "black"

        first = True

        for sample in samples:
            draw_okay = False
            line_okay = False
            mover_type = type(sample.mover)

            if first is True:
                first = False
                color = 'black'
                draw_okay = True
                shift = 0

                self.renderer.add(
                    self.renderer.label(0, t_count, 1,
                        str(self.storage.trajectories.idx(sample.trajectory)) + 'b',
                        align='end',
                        color='black'
                    )
                )

            elif mover_type is paths.ReplicaExchangeMover:
                # Reversal
                color = 'blue'
                draw_okay = True
                line_okay = True

                # self.renderer.add(
                #     self.renderer.label(shift, t_count, 1, 'RX', align='end',color='black')
                # )

                self.renderer.add(
                    self.renderer.label(shift, t_count, 1, str(
                        self.storage.idx(sample.trajectory)) + 'x', align='end',
                                        color='black')
                )


                self.renderer.add(
                    self.renderer.range(shift, t_count, len(sample), 'lightblue', "ReplicaExchange" ))

            elif mover_type is paths.PathReversalMover:
                # Reversal
                color = 'orange'
                draw_okay = False
                line_okay = True

                self.renderer.add(
                    self.renderer.label(shift, t_count, 1, str(
                        self.storage.idx(sample.trajectory)) + 'r', align='end',
                                        color='black')
                )

                self.renderer.add(
                    self.renderer.range(shift, t_count, len(sample), 'orange', "PathReversal" ))

            elif mover_type in [paths.BackwardExtendMover]:
                color = 'green'
                draw_okay = True
                line_okay = True

                self.renderer.add(
                    self.renderer.range(shift, t_count, len(sample.parent), 'palegreen', "BackwardExtend" ))

                shift = shift - len(sample) + len(sample.parent)

                self.renderer.add(
                    self.renderer.label(shift, t_count, 1, str(
                        self.storage.idx(sample.trajectory)) + 'b', align='end',
                                        color='black')
                )

            elif mover_type in [paths.ForwardExtendMover]:
                color = 'red'
                draw_okay = True
                line_okay = True

                self.renderer.add(
                    self.renderer.range(shift, t_count, len(sample.parent), 'salmon', "ForwardExtend" ))

                self.renderer.add(
                    self.renderer.label(shift + len(sample), t_count, 1, str(
                        self.storage.idx(sample.trajectory)) + 'f', align='start',
                                        color='black')
                )


            elif mover_type in [paths.FirstSubtrajectorySelectMover, paths.FinalSubtrajectorySelectMover]:
                color = 'lightblue'
                draw_okay = True
                line_okay = True

                self.renderer.add(
                    self.renderer.label(shift, t_count, 1, str(
                        self.storage.idx(sample.trajectory)) + 'r', align='end',
                                        color='black')
                )

                shift = shift + sample.parent.trajectory.index(sample[0])

                self.renderer.add(
                    self.renderer.range(shift, t_count, len(sample), 'gray', mover_type.__name__[:-11] )
                )

            elif mover_type in [paths.ForwardShootMover, paths.BackwardShootMover]:
                # ShootingMove
                old_traj = sample.details.initial_trajectory
                old_conf = sample.details.shooting_snapshot
                old_index = sample.details.initial_trajectory.index(old_conf)
                old_conf_idx = self.storage.idx(old_conf)

                # print old_conf
                # print "Initial:", [hex(id(s)) for s in old_traj]
                # print "Trial:", [hex(id(s)) for s in sample.trajectory]

                new_traj = sample.trajectory
                new_index = new_traj.index(old_conf)

                # print type(old_conf), self.storage.snapshots.index.get(old_conf, None)

                if sample.trajectory is new_traj or self.rejected:

                    if old_conf_idx not in p_x:
                        shift = 0
                    else:
                        shift = p_x[old_conf_idx] - new_index 

                    font_color = "black"

                    draw_okay = False

                    if mover_type is paths.BackwardShootMover:
                        color = "green"
                        self.renderer.add(
                            self.renderer.v_connection(shift + new_index,
                                                       p_y[old_conf_idx], t_count,
                                                       color)
                        )
                        self.renderer.add(
                            self.renderer.label(shift, t_count, 1, str(
                                self.storage.idx(new_traj)) + 'b', align='end',
                                                color=font_color)
                        )
                        draw_okay = True

                    elif mover_type is paths.ForwardShootMover:
                        color = "red"

                        self.renderer.add(
                            self.renderer.v_connection(shift + new_index + 1,
                                                       p_y[old_conf_idx], t_count,
                                                       color)
                        )
                        self.renderer.add(
                            self.renderer.label(shift + len(new_traj) - 1,
                                                t_count, 1, str(
                                    self.storage.idx(new_traj)) + 'f',
                                                align='start', color=font_color)
                        )
                        draw_okay = True

            if draw_okay:
                for pos, snapshot in enumerate(sample.trajectory):
                    conf = snapshot
                    conf_idx = self.storage.idx(conf)
                    if not conf_idx in p_y:

                        p_x[conf_idx] = shift + pos
                        p_y[conf_idx] = t_count

                        pos_x = p_x[conf_idx]
                        pos_y = p_y[conf_idx]

                        if self.op is not None:
                            self.renderer.add(
                                self.renderer.block(
                                    pos_x,
                                    pos_y,
                                    color,
                                    self.op(snapshot),
                                    extend_left = pos > 0,
                                    extend_right = pos < len(sample.trajectory) - 1
                                ))
                        else:
                            self.renderer.add(
                                self.renderer.block(pos_x, pos_y, color, ""))
                    elif self.show_redundant:
                        pos_y = t_count
                        pos_x = shift + pos
                        if self.op is not None:
                            self.renderer.add(
                                self.renderer.block(pos_x, pos_y, 'gray',
                                                    self.op(snapshot)))
                        else:
                            self.renderer.add(
                                self.renderer.block(pos_x, pos_y, 'gray', ""))

                        old_x = p_x[conf_idx]
                        old_y = p_y[conf_idx]
                        self.renderer.add(
                            self.renderer.block(old_x, old_y - 0.3, 'blue', ""))

            # self.renderer.add(
            #     self.renderer.label(shift - 2, t_count, 1, sample.replica
            #         , align='end',
            #                         color='black')
            # )

            if line_okay:
                for pos, snapshot in enumerate(sample.trajectory):
                    conf = snapshot
                    conf_idx = self.storage.idx(conf)
                    p_x[conf_idx] = shift + pos
                    p_y[conf_idx] = t_count

            t_count += 1

        self.p_x = p_x
        self.p_y = p_y

        min_x, max_x = self._get_min_max(self.p_x)
        min_y, max_y = self._get_min_max(self.p_y)

        self.renderer.shift_x = min_x - 8.5
        self.renderer.shift_y = 0
        self.renderer.height = max_y - min_y + 5.0
        self.renderer.width = max_x - min_x + 10.0

        matrix = self._to_matrix()

        if hasattr(self, 'states') and len(self.states) > 0:
            for color, op in self.states:
                xp = None
                for y in range(0, max_y - min_y + 1):
                    left = None
                    yp = y + min_y
                    for x in range(0, (max_x - min_x + 1)):
                        xp = x + min_x

                        # if matrix[y][x] is not None:
                        #     self.renderer.pre(
                        #         self.renderer.shade(xp, yp, 0.9,
                        #                             'black')
                        #     )


                        if matrix[y][x] is not None\
                            and bool(op(LoaderProxy(self.storage.snapshots, matrix[y][x]))):
                                if left is None:
                                    left = xp
                        else:
                            if left is not None:
                                self.renderer.pre(
                                    self.renderer.shade(left, yp, xp - left,
                                                        color)
                                )
                                left = None

                    if left is not None:
                        self.renderer.pre(
                            self.renderer.shade(left, yp, xp - left + 1, color)
                        )

        prev = samples[0].trajectory
        old_tc = 1
        trial_list = {}
        for step in self.storage.steps:
            for ch in step.change:
                if ch.samples is not None:
                    for trial in ch.samples:
                        trial_list[trial] = step.mccycle

        for tc, s in enumerate(samples):
            if tc > 0 and not paths.Trajectory.is_correlated(s.trajectory, prev):
                self.renderer.add(
                    self.renderer.h_range(self.renderer.shift_x + 5.5, old_tc - 0.1, 1 + tc - old_tc + 0.2, 'black', "" ))


                old_tc = 1 + tc

                prev = s.trajectory

            self.renderer.add(
                self.renderer.label(self.renderer.shift_x + 5.0, 1 + tc, 1, str(
                    self.storage.idx(s)) , align='end',
                                    color=font_color)
            )

            if s in trial_list:
                txt = str(trial_list[s])
            else:
                txt = '---'

            self.renderer.add(
                self.renderer.label(self.renderer.shift_x + 2.0, 1 + tc, 1, str(
                    txt) , align='end',
                                    color=font_color)
            )

        self.renderer.add(
            self.renderer.h_range(self.renderer.shift_x + 2.0, 0.9, len(samples) + 0.2, 'black', "" ))

        self.renderer.add(
            self.renderer.h_range(self.renderer.shift_x + 5.5, old_tc - 0.1, 1 + len(samples) - old_tc + 0.2, 'black', "", extend_bottom=False))
Ejemplo n.º 11
0
    def add_cv(self, cv, template, allow_incomplete=None, chunksize=None):
        """

        Parameters
        ----------
        cv : :obj:`openpathsampling.CollectiveVariable`
        template : :obj:`openpathsampling.engines.BaseSnapshot`
        chunksize : int
        allow_incomplete : bool

        Returns
        -------
        :obj:`openpathsampling.netcdfplus.ObjectStore`
        int
        """
        if cv in self.cv_list:
            return self.cv_list[cv]

        if allow_incomplete is None:
            allow_incomplete = cv.diskcache_allow_incomplete
        if chunksize is None:
            chunksize = cv.diskcache_chunksize
        if template is None:
            template = cv.diskcache_template

        time_reversible = cv.cv_time_reversible

        if not time_reversible:
            # in the rare case of not time_reversible we use store partial
            allow_incomplete = True

        if not allow_incomplete:
            # in complete mode we force chunk size one to match it to snapshots
            chunksize = self.default_store_chunk_size

        # determine value type and shape
        params = NetCDFPlus.get_value_parameters(cv(template))
        shape = params['dimensions']

        if shape is None:
            chunksizes = None
        else:
            chunksizes = tuple(params['dimensions'])

        cv_idx = self.storage.cvs.index[cv.__uuid__]
        store = SnapshotValueStore(time_reversible=time_reversible,
                                   allow_incomplete=allow_incomplete,
                                   chunksize=chunksize)

        store_name = SnapshotWrapperStore._get_cv_name(cv_idx)
        self.storage.create_store(store_name, store, False)

        if store.allow_incomplete:
            # we are not using the .initialize function here since we
            # only have one variable and only here know its shape
            self.storage.create_dimension(store.prefix, 0)

            if shape is not None:
                shape = tuple(list(shape))
                chunksizes = tuple([chunksize] + list(chunksizes))
            else:
                shape = tuple()
                chunksizes = tuple([chunksize])

            # create the variable
            store.create_variable(
                'value',
                var_type=params['var_type'],
                dimensions=shape,
                chunksizes=chunksizes,
                simtk_unit=params['simtk_unit'],
            )

            store.create_variable('index', 'index')

        else:
            chunksize = self.default_store_chunk_size

            # todo: Remove once netcdf4/python bug in Unidata/netcdf4-python#566 has
            # been resolved. It seems that so far, after data has been written to a
            # dimension you have to use chunksize 1 for any new variables in that
            # dimension Even if the other variables all share the same chunksize
            chunksize = 1

            if shape is not None:
                shape = tuple(['snapshots'] + list(shape))
                chunksizes = tuple([chunksize] + list(chunksizes))
            else:
                shape = tuple(['snapshots'])
                chunksizes = tuple([chunksize])

            # create the variable
            store.storage.create_variable(
                store_name + '_value',
                var_type=params['var_type'],
                dimensions=shape,
                chunksizes=chunksizes,
                simtk_unit=params['simtk_unit'],
            )

        setattr(store, 'value', self.storage.vars[store_name + '_value'])

        store.initialize()

        store_idx = int(len(self.storage.dimensions['cvcache']))
        self.cv_list[cv] = (store, store_idx)
        self.storage.vars['cvcache'][store_idx] = store

        # use the cache and function of the CV to fill the store when it is made
        if not allow_incomplete:

            indices = self.vars['uuid'][:]

            for pos, idx in enumerate(indices):

                proxy = LoaderProxy(self.storage.snapshots, idx)
                value = cv._cache_dict._get(proxy)

                if value is None:
                    # not in cache so compute it if possible
                    if cv._eval_dict:
                        value = cv._eval_dict([proxy])[0]
                    else:
                        value = None

                if value is not None:
                    store.vars['value'][pos] = value
                    store.cache[pos] = value

        cv.set_cache_store(store)
        return store, store_idx