本文整理汇总了Python中msmbuilder.MSMLib.get_count_matrix_from_assignments方法的典型用法代码示例。如果您正苦于以下问题:Python MSMLib.get_count_matrix_from_assignments方法的具体用法?Python MSMLib.get_count_matrix_from_assignments怎么用?Python MSMLib.get_count_matrix_from_assignments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msmbuilder.MSMLib
的用法示例。
在下文中一共展示了MSMLib.get_count_matrix_from_assignments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_count_matrix_from_assignments_3
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_3():
np.random.seed(42)
assignments = np.random.randint(3, size=(10, 10))
val = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=2, sliding_window=False).todense()
eq(val, np.matrix([[5.0, 3.0, 4.0], [2.0, 12.0, 3.0], [4.0, 3.0, 4.0]]))
val = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=2, sliding_window=True).todense()
eq(val, np.matrix([[8.0, 9.0, 11.0], [5.0, 18.0, 6.0], [11.0, 5.0, 7.0]]))
示例2: test_get_count_matrix_from_assignments_3
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_3():
np.random.seed(42)
assignments = np.random.randint(3, size=(10,10))
val = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=2, sliding_window=False).todense()
npt.assert_equal(val, np.matrix([[ 5., 3., 4.],
[ 2., 12., 3.],
[ 4., 3., 4.]]))
val = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=2, sliding_window=True).todense()
npt.assert_equal(val, np.matrix([[8., 9., 11.],
[ 5., 18., 6.],
[ 11., 5., 7.]]))
示例3: build_msm
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def build_msm(self, lag_time=None):
"""Build an MSM from the loaded trajectories."""
if lag_time is None:
lag_time = self.good_lag_time
else:
self.good_lag_time = lag_time
# Do assignment
trajs = get_data.get_shimtraj_from_trajlist(self.traj_list)
metric = classic.Euclidean2d()
# Allocate array
n_trajs = len(self.traj_list)
max_traj_len = max([t.shape[0] for t in self.traj_list])
assignments = -1 * np.ones((n_trajs, max_traj_len), dtype='int')
# Prepare generators
pgens = metric.prepare_trajectory(self.clusterer.get_generators_as_traj())
for i, traj in enumerate(trajs):
ptraj = metric.prepare_trajectory(traj)
for j in xrange(len(traj)):
d = metric.one_to_all(ptraj, pgens, j)
assignments[i, j] = np.argmin(d)
counts = msml.get_count_matrix_from_assignments(assignments, n_states=None, lag_time=lag_time)
rev_counts, t_matrix, populations, mapping = msml.build_msm(counts)
return t_matrix
示例4: main
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def main(assfile, lag, nproc):
lag=int(lag)
nproc=int(nproc)
Assignments=io.loadh(assfile)
num=int(assfile.split('Assignments_sub')[1].split('.h5')[0])
dir=os.path.dirname(assfile)
newdir='%s/boot-sub%s' % (dir, num)
ref_sub=numpy.loadtxt('%s/times.h5' % dir, usecols=(1,))
ref_total=numpy.loadtxt('%s/times.h5' % dir, usecols=(2,))
times=dict()
for (i,j) in zip(ref_sub, ref_total):
times[i]=j
proj=Project.load_from('%s/ProjectInfo.yaml' % dir.split('Data')[0])
multinom=int(times[num])
if not os.path.exists(newdir):
os.mkdir(newdir)
if 'Data' in Assignments.keys():
Assignments=Assignments['Data']
else:
Assignments=Assignments['arr_0']
print Assignments.shape
NumStates = max(Assignments.flatten()) + 1
Counts = MSMLib.get_count_matrix_from_assignments(Assignments, lag_time=int(lag), sliding_window=True)
Counts=Counts.todense()
Counts=Counts*(1.0/lag)
T=numpy.array(Counts)
frames=numpy.where(T==0)
T[frames]=1
Popsample=dict()
iteration=0
total_iteration=100/nproc
print "%s total iterations" % total_iteration
if 100 % nproc != 0:
remain=100 % nproc
else:
remain=False
print "iterating thru tCount samples"
count=0
while iteration < 100:
if count*nproc > 100:
nproc=remain
print "sampling iteration %s" % iteration
Tfresh=T.copy()
input = zip([Tfresh]*nproc, [multinom]*nproc, range(0, NumStates))
pool = multiprocessing.Pool(processes=nproc)
result = pool.map_async(parallel_get_matrix, input)
result.wait()
all = result.get()
pool.terminate()
for c_matrix in all:
scipy.io.mmwrite('%s/tCounts-%s' % (newdir, iteration), c_matrix)
#rev_counts, t_matrix, Populations, Mapping=x
#scipy.io.mmwrite('%s/tProb-%s' % (newdir, iteration), t_matrix)
#numpy.savetxt('%s/Populations-%s' % (newdir, iteration), Populations)
#numpy.savetxt('%s/Mapping-%s' % (newdir, iteration), Mapping)
iteration+=1
count+=1
print "dont with iteration %s" % iteration*nproc
示例5: test_get_count_matrix_from_assignments_1
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_1():
assignments = np.zeros((10,10))
val = MSMLib.get_count_matrix_from_assignments(assignments).todense()
correct = np.matrix([[90.0]])
npt.assert_equal(val, correct)
示例6: test_estimate_rate_matrix_1
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_estimate_rate_matrix_1():
np.random.seed(42)
assignments = np.random.randint(2, size=(10, 10))
counts = MSMLib.get_count_matrix_from_assignments(assignments)
K = MSMLib.estimate_rate_matrix(counts, assignments).todense()
correct = np.matrix([[-40.40909091, 0.5], [0.33928571, -50.55357143]])
eq(K, correct)
示例7: test_get_count_matrix_from_assignments_1
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_1():
assignments = np.zeros((10, 10), "int")
val = MSMLib.get_count_matrix_from_assignments(assignments).todense()
correct = np.matrix([[90.0]])
eq(val, correct)
示例8: test_1
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_1(self):
C = MSMLib.get_count_matrix_from_assignments(self.assignments, 2)
rc, t, p, m = MSMLib.build_msm(C, symmetrize="MLE", ergodic_trimming=True)
eq(rc.todense(), np.matrix([[6.46159184, 4.61535527], [4.61535527, 2.30769762]]), decimal=4)
eq(t.todense(), np.matrix([[0.58333689, 0.41666311], [0.66666474, 0.33333526]]), decimal=4)
eq(p, np.array([0.61538595, 0.38461405]), decimal=5)
eq(m, np.array([0, 1]))
示例9: test_get_count_matrix_from_assignments_2
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_2():
np.random.seed(42)
assignments = np.random.randint(3, size=(10, 10))
val = MSMLib.get_count_matrix_from_assignments(assignments).todense()
correct = np.matrix([[11.0, 9.0, 10.0], [9.0, 17.0, 7.0], [10.0, 7.0, 10.0]])
eq(val, correct)
示例10: dump_count_matrix
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def dump_count_matrix(self,assignfn,lagtime=1,outfn="count_matrix.txt"):
from msmbuilder import io
from msmbuilder import MSMLib
assignments = io.loadh(assignfn, 'arr_0')
# returns sparse lil_matrix
counts = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=lagtime,
sliding_window=True)
counts = counts.tocoo()
np.savetxt(outfn,(counts.row, counts.col, counts.data))
示例11: construct_counts_matrix
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def construct_counts_matrix(assignments):
"""Build and return a counts matrix from assignments.
Symmetrize either with transpose or MLE based on the value of the
self.symmetrize variable
Also modifies the assignments file that you pass it to reflect ergodic
trimming
Parameters
----------
assignments : np.ndarray
2D array of MSMBuilder assignments
Returns
-------
counts : scipy.sparse.csr_matrix
transition counts
"""
n_states = np.max(assignments.flatten()) + 1
raw_counts = MSMLib.get_count_matrix_from_assignments(assignments, n_states,
lag_time=Project().lagtime,
sliding_window=True)
ergodic_counts = None
if Project().trim:
raise NotImplementedError(('Trimming is not yet supported because '
'we need to keep track of the mapping from trimmed to '
' untrimmed states for joint clustering to be right'))
try:
ergodic_counts, mapping = MSMLib.ergodic_trim(raw_counts)
MSMLib.apply_mapping_to_assignments(assignments, mapping)
counts = ergodic_counts
except Exception as e:
logger.warning("MSMLib.ergodic_trim failed with message '{0}'".format(e))
else:
logger.info("Ignoring ergodic trimming")
counts = raw_counts
if Project().symmetrize == 'transpose':
logger.debug('Transpose symmetrizing')
counts = counts + counts.T
elif Project().symmetrize == 'mle':
logger.debug('MLE symmetrizing')
counts = MSMLib.mle_reversible_count_matrix(counts)
elif Project().symmetrize == 'none' or (not Project().symmetrize):
logger.debug('Skipping symmetrization')
else:
raise ValueError("Could not understand symmetrization method: %s" % Project().symmetrize)
return counts
示例12: test_get_count_matrix_from_assignments_2
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def test_get_count_matrix_from_assignments_2():
np.random.seed(42)
assignments = np.random.randint(3, size=(10,10))
val = MSMLib.get_count_matrix_from_assignments(assignments).todense()
correct = np.matrix([[ 11., 9., 10.],
[ 9., 17., 7.],
[ 10., 7., 10.]])
npt.assert_equal(val, correct)
示例13: main
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def main(assfile, lag, nproc):
lag=int(lag)
nproc=int(nproc)
Assignments=io.loadh(assfile)
dir=os.path.dirname(assfile)
newdir='%s/sample-counts' % dir
proj=Project.load_from('%s/ProjectInfo.yaml' % dir.split('Data')[0])
multinom=sum(proj.traj_lengths)
if not os.path.exists(newdir):
os.mkdir(newdir)
if 'Data' in Assignments.keys():
Assignments=Assignments['Data']
else:
Assignments=Assignments['arr_0']
print Assignments.shape
NumStates = max(Assignments.flatten()) + 1
Counts = MSMLib.get_count_matrix_from_assignments(Assignments, lag_time=int(lag), sliding_window=True)
Counts=Counts.todense()
Counts=Counts*(1.0/lag)
T=numpy.array(Counts)
frames=numpy.where(T==0)
T[frames]=1
Popsample=dict()
iteration=0
total_iteration=100/nproc
print "%s total iterations" % total_iteration
if 100 % nproc != 0:
remain=100 % nproc
else:
remain=False
print "iterating thru tCount samples"
count=0
while iteration < 100:
if count*nproc > 100:
nproc=remain
print "sampling iteration %s" % iteration
Tfresh=T.copy()
counts=range(0, nproc)
input = zip([Tfresh]*nproc, [multinom]*nproc, [NumStates]*nproc, counts)
pool = multiprocessing.Pool(processes=nproc)
result = pool.map_async(parallel_get_matrix, input)
result.wait()
all = result.get()
print "computed resampled matrices"
pool.terminate()
for count_matrix in all:
#rev_counts, t_matrix, Populations, Mapping=x
scipy.io.mmwrite('%s/tCounts-%s' % (newdir, iteration), count_matrix)
# scipy.io.mmwrite('%s/tProb-%s' % (newdir, iteration), t_matrix)
# numpy.savetxt('%s/Populations-%s' % (newdir, iteration), Populations)
# numpy.savetxt('%s/Mapping-%s' % (newdir, iteration), Mapping)
iteration+=1
count+=1
print "dont with iteration %s" % iteration*nproc
示例14: msm
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def msm(traj_list, n_clusters, n_medoid_iters=10, lag_time=1, distance_cutoff=None):
"""Use classic clustering methods."""
print "Building a classic MSM"
hkm = cluster(traj_list, n_clusters, n_medoid_iters, distance_cutoff)
# centroids = hkm.get_generators_as_traj()
# centroids_nf = centroids['XYZList'][:, 0, 0:dim]
counts = msml.get_count_matrix_from_assignments(hkm.get_assignments(), n_clusters, lag_time)
rev_counts, t_matrix, populations, mapping = msml.build_msm(counts)
return t_matrix
示例15: run
# 需要导入模块: from msmbuilder import MSMLib [as 别名]
# 或者: from msmbuilder.MSMLib import get_count_matrix_from_assignments [as 别名]
def run(lagtime, assignments, symmetrize='MLE', input_mapping="None", trim=True, out_dir="./Data/"):
# set the filenames for output
FnTProb = os.path.join(out_dir, "tProb.mtx")
FnTCounts = os.path.join(out_dir, "tCounts.mtx")
FnMap = os.path.join(out_dir, "Mapping.dat")
FnAss = os.path.join(out_dir, "Assignments.Fixed.h5")
FnPops = os.path.join(out_dir, "Populations.dat")
# make sure none are taken
outputlist = [FnTProb, FnTCounts, FnMap, FnAss, FnPops]
arglib.die_if_path_exists(outputlist)
# Check for valid lag time
assert lagtime > 0, 'Please specify a positive lag time.'
# if given, apply mapping to assignments
if input_mapping != "None":
MSMLib.apply_mapping_to_assignments(assignments, input_mapping)
n_assigns_before_trim = len(np.where(assignments.flatten() != -1)[0])
counts = MSMLib.get_count_matrix_from_assignments(assignments, lag_time=lagtime, sliding_window=True)
rev_counts, t_matrix, populations, mapping = MSMLib.build_msm(counts, symmetrize=symmetrize, ergodic_trimming=trim)
if trim:
MSMLib.apply_mapping_to_assignments(assignments, mapping)
n_assigns_after_trim = len(np.where(assignments.flatten() != -1)[0])
# if had input mapping, then update it
if input_mapping != "None":
mapping = mapping[input_mapping]
# Print a statement showing how much data was discarded in trimming
percent = (1.0 - float(n_assigns_after_trim) / float(n_assigns_before_trim)) * 100.0
logger.warning("Ergodic trimming discarded: %f percent of your data", percent)
else:
logger.warning("No ergodic trimming applied")
# Save all output
np.savetxt(FnPops, populations)
np.savetxt(FnMap, mapping, "%d")
scipy.io.mmwrite(str(FnTProb), t_matrix)
scipy.io.mmwrite(str(FnTCounts), rev_counts)
io.saveh(FnAss, assignments)
for output in outputlist:
logger.info("Wrote: %s", output)
return