Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
super(FilterByReplica, self).__init__()
if type(replicas) is not list:
replicas = [replicas]
self.replicas = replicas
self.mover = mover
# TODO: clean this up
pass
def move(self, globalstate):
filtered_gs = paths.SampleSet(
[s for s in globalstate if s.replica in self.replicas]
)
return self.mover.move(filtered_gs)
class FilterBySample(PathMover):
def __init__(self, mover, selected_samples):
super(FilterBySample, self).__init__()
if type(selected_samples) is not list:
selected_samples = [selected_samples]
self.selected_samples = selected_samples
self.mover = mover
def move(self, globalstate):
return paths.FilterSamplesPathMoveChange(
self.mover.move(globalstate),
mover=self
)
class WrappedMover(PathMover):
"""Mover that delegates to a single submover
###############################################################################
class MoverType(object):
pass
class SwappingMover(MoverType):
"""
A mover that swaps samples from ensembles in some way. Relevant for mixing
"""
###############################################################################
# GENERATORS
###############################################################################
class SampleGeneratingMover(PathMover):
engine = None
@classmethod
def metropolis(cls, trials):
"""Implements the Metropolis acceptance for a list of trial samples
The Metropolis uses the .bias for each sample and checks of samples
are valid - are in the proposed ensemble. This will give an acceptance
probability for all samples. If the product is smaller than a random
number the change will be accepted.
Parameters
----------
trials : list of openpathsampling.Sample
the list of all samples to be applied in a change.
def _flatten(ensembles):
if type(ensembles) is list:
return [s for ens in ensembles for s in PathMover._flatten(ens)]
else:
return [ensembles]
details.inputs = [rep_sample]
details.trials = [rep_sample]
details.mover = self
setattr(details, 'rep_from', mypair[0])
setattr(details, 'rep_to', mypair[1])
return paths.AcceptedSamplePathMoveChange(
samples=[new_sample],
mover=self,
details=details
)
# TODO: Filter moves are not used at all, do we need these?
# TODO: Turn Filter into real mover with own movechange ?
class FilterByReplica(PathMover):
def __init__(self, mover, replicas):
super(FilterByReplica, self).__init__()
if type(replicas) is not list:
replicas = [replicas]
self.replicas = replicas
self.mover = mover
# TODO: clean this up
pass
def move(self, globalstate):
filtered_gs = paths.SampleSet(
[s for s in globalstate if s.replica in self.replicas]
)
return self.mover.move(filtered_gs)
----------
sample_set : `openpathsampling.SampleSet`
the sampleset from which to pick specific samples matching certain
criteria
ensembles : list of `openpathsampling.Ensembles` or `None`
the ensembles to pick from or `None` for all
replicas : list of int or None
the replicas to pick or `None` for all
"""
if replicas is None:
replicas = 'all'
logger.debug(
"replicas: " + str(replicas) + " ensembles: " + repr(ensembles))
legal = PathMover.legal_sample_set(sample_set, ensembles, replicas)
for sample in legal:
logger.debug(
"legal: (" + str(sample.replica) +
"," + str(sample.trajectory) +
"," + repr(sample.ensemble) +
")")
selected = random.choice(legal)
logger.debug(
"selected sample: (" + str(selected.replica) +
"," + str(selected.trajectory) +
"," + repr(selected.ensemble) +
")")
return selected
for idx, mover in reversed(list(enumerate(self.movers))):
if not found:
for ens in mover.input_ensembles:
if ens not in present_ensembles:
# ens might be required but is not present
weights[idx] = 0.0
if weights[idx] > 0.0:
found = True
else:
weights[idx] = 0.0
return weights
class ConditionalMover(PathMover):
"""
An if-then-else structure for PathMovers.
Returns a SequentialMoveChange of the if_move movepath and the then_move
movepath (if if_move is accepted) or the else_move movepath (if if_move
is rejected).
"""
def __init__(self, if_mover, then_mover, else_mover):
"""
Parameters
----------
if_mover : openpathsampling.PathMover
then_mover : openpathsampling.PathMover
else_mover : openpathsampling.PathMover
"""
"""
Save the initial state as an MCStep to the storage
"""
mcstep = MCStep(
simulation=self,
mccycle=self.step,
previous=None,
active=self.globalstate,
change=paths.EmptyPathMoveChange()
)
if self.storage is not None:
self.storage.steps.save(mcstep)
self.storage.sync()
class BootstrapPromotionMove(PathMover):
'''
Bootstrap promotion is the combination of an EnsembleHop (to the next
ensemble up) with incrementing the replica ID.
'''
def __init__(self, bias=None, shooters=None,
ensembles=None):
"""
Parameters
----------
bias : None
not used yet, only for API consistency and later implementation
shooters : list of ShootingMovers
list of ShootingMovers for each ensemble
ensembles : list of Ensembles
list of ensembles the move should act on
for mover in self.movers:
logger.debug("Starting sequential move step "+str(mover))
# Run the sub mover
movepath = mover.move(subglobal)
samples = movepath.results
subglobal = subglobal.apply_samples(samples)
pathmovechanges.append(movepath)
if not movepath.accepted:
break
return paths.ConditionalSequentialPathMoveChange(pathmovechanges, mover=self)
class RestrictToLastSampleMover(PathMover):
def __init__(self, mover):
super(RestrictToLastSampleMover, self).__init__()
self.mover = mover
@property
def submovers(self):
return [self.mover]
def _get_in_ensembles(self):
return [ sub.input_ensembles for sub in self.submovers ]
def move(self, globalstate):
movepath = self.mover.move(globalstate)
return paths.KeepLastSamplePathMoveChange(movepath, mover=self)
samples : MoveChange
the MoveChange instance describing the change from the old to
the new SampleSet
"""
return paths.EmptyMoveChange() # pragma: no cover
def __str__(self):
if self.name == self.__class__.__name__:
return self.__repr__()
else:
return self.name
class IdentityPathMover(PathMover):
"""
The simplest Mover that does nothing !
Notes
-----
Since is does nothing it is considered rejected everytime!
It can be used to test function of PathMover
Parameters
----------
counts_as_trial : bool
Whether this mover should count as a trial or not. If `True`, the
`EmptyMoveChange` returned includes this mover, which means it gets
counted as a trial in analysis of acceptance. If `False` (default),
the mover for the returned move change is `None`, which does not get
counted as a trial.
if ifclause.accepted:
if self.then_mover is not None:
resultclause = self.then_mover.move(subglobal)
else:
resultclause = paths.EmptyPathMoveChange()
else:
if self.else_mover is not None:
resultclause = self.else_mover.move(subglobal)
else:
resultclause = paths.EmptyPathMoveChange()
return paths.SequentialPathMoveChange([ifclause, resultclause], mover=self)
class SequentialMover(PathMover):
"""
Performs each of the moves in its movers list. Returns all samples
generated, in the order of the mover list.
For example, this would be used to create a move that does a sequence of
replica exchanges in a given order, regardless of whether the moves
succeed or fail.
"""
def __init__(self, movers, ensembles=None):
super(SequentialMover, self).__init__(ensembles=ensembles)
self.movers = movers
initialization_logging(init_log, self, ['movers'])
@property
def submovers(self):
return self.movers