def testEpsilonCalculatorWithDensity(self): params = spawning.SpawningParams() params.epsilon = 0.5 params.metricWeights = "linear" params.nclusters = 100 clusters = clustering.Clusters() sizes = [6, 2, 3, 1] energies = [-4, -2, -2, 0] contacts = [4, 3, 2, 1] for size, energy, ncontacts in zip(sizes, energies, contacts): cluster = clustering.Cluster(None, None, None, None, metricCol=0) cluster.elements = size cluster.metrics = [energy] cluster.contacts = ncontacts clusters.addCluster(cluster) clusteringBlock = { "density": { "type": "heaviside", "params": { "values": [6, 2, 3, 1], "conditions": [3, 2, 1] } } } densityCalculatorBuilder = densitycalculator.DensityCalculatorBuilder() densityCalculator = densityCalculatorBuilder.build(clusteringBlock) epsilon = spawning.EpsilonDegeneracyCalculator(params, densityCalculator) trajs = 16 degeneracy = epsilon.calculate(clusters.clusters, trajs) golden = np.array([6, 4, 4, 2]) np.testing.assert_array_equal(degeneracy, golden)
def testPeriodicVariableEpsilonCalculator(self): params = spawning.SpawningParams() params.epsilon = 0.5 params.varEpsilonType = "linearVariation" params.maxEpsilon = 0.75 params.minEpsilon = 0.5 params.variationWindow = 20 params.maxEpsilonWindow = 1 params.metricWeights = "linear" params.nclusters = 100 params_test = {"period": 8} params.period = params_test.get("period", params.variationWindow) params.period += np.sign(np.abs(params.variationWindow - params.period)) variable_epsilon = spawning.VariableEpsilonDegeneracyCalculator(params) # rateVariation = (params.maxEpsilon-params.minEpsilon)/3 clusters = clustering.Clusters() sizes = [6, 2, 3, 1] energies = [-4, -2, -2, -1] for size, energy in zip(sizes, energies): cluster = clustering.Cluster(None, None, None, None, metricCol=0) cluster.elements = size cluster.metrics = [energy] clusters.addCluster(cluster) trajs = 20 degeneracy6 = variable_epsilon.calculate(clusters.clusters, trajs, 0) golden6 = np.array([7, 4, 4, 5]) np.testing.assert_array_equal(degeneracy6, golden6) self.assertAlmostEqual(params.epsilon, params.minEpsilon) for i in range(1, params.variationWindow): degeneracy7 = variable_epsilon.calculate(clusters.clusters, trajs, i)
def testInverselyProportionalToPopulationCalculatorWithDensity(self): # test 3 clusters = clustering.Clusters() sizes = [6, 2, 3, 1] contacts = [4, 3, 2, 1] for size, ncontacts in zip(sizes, contacts): cluster = clustering.Cluster(None, None, None, None) cluster.elements = size cluster.contacts = ncontacts clusters.addCluster(cluster) clusteringBlock = { "density": { "type": "heaviside", "params": { "values": [6, 2, 3, 1], "conditions": [3, 2, 1] } } } densityCalculatorBuilder = densitycalculator.DensityCalculatorBuilder() densityCalculator = densityCalculatorBuilder.build(clusteringBlock) params = spawning.SpawningParams() inverselyProp = spawning.InverselyProportionalToPopulationCalculator( params, densityCalculator) trajs = 8 degeneracy = inverselyProp.calculate(clusters.clusters, trajs) golden = [2, 2, 2, 2] self.assertEqual(degeneracy, golden)
def testMSMMetastability(self): params = spawning.SpawningParams() params.lagtime = 1 dtrajs = [ np.array([1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 1]) ] cl = ClMock(dtrajs) MSMP = spawning.MetastabilityMSMCalculator(params) ntrajs = 30 degeneracy = MSMP.calculate(cl, ntrajs) golden = np.array([10, 10, 10]) np.testing.assert_array_equal(degeneracy, golden)
def cluster_traject(resname, trajToDistribute, columnToChoose, distance_contact, clusterThreshold, path_to_cluster, output_path, mapping_out, epsilon=0.5, report_basename="report", condition="min", metricweights="linear", nclusters=5): outputPathConst = constants.OutputPathConstants(output_path) outputPathConst.tmpFolder = output_path outputPathConst.buildTmpFolderConstants(outputPathConst.tmpFolder) utilities.makeFolder(outputPathConst.tmpFolder) thresholdCalc = thresholdcalculator.ThresholdCalculatorConstant( value=clusterThreshold) similarityEval = clustering.CMSimilarityEvaluator("Jaccard") clusteringObject = clustering.ContactMapAccumulativeClustering( thresholdCalc, similarityEval, resname=resname, reportBaseFilename=report_basename, columnOfReportFile=columnToChoose, contactThresholdDistance=distance_contact, altSelection=True) clusteringObject.cluster([path_to_cluster], ignoreFirstRow=True) spawning_params = spawning.SpawningParams() spawning_params.reportFilename = report_basename spawning_params.epsilon = epsilon spawning_params.nclusters = nclusters spawning_params.metricWeights = metricweights spawning_params.condition = condition density = densitycalculator.NullDensityCalculator() spawningObject = spawning.EpsilonDegeneracyCalculator( spawning_params, density) degeneracy = spawningObject.calculate(clusteringObject.clusters, trajToDistribute, spawning_params) spawningObject.log() _, procMapping = spawningObject.writeSpawningInitialStructures( outputPathConst, degeneracy, clusteringObject, 0) processorManagerFilename = "processorMapping.txt" utilities.writeProcessorMappingToDisk(mapping_out, processorManagerFilename, procMapping)
def testInverselyProportionalToPopulationCalculator(self): clusters = clustering.Clusters() sizes = [6, 2, 3, 1] for size in sizes: cluster = clustering.Cluster(None, None, None, None) cluster.elements = size clusters.addCluster(cluster) params = spawning.SpawningParams() inverselyProp = spawning.InverselyProportionalToPopulationCalculator( params) trajs = 10 degeneracy = inverselyProp.calculate(clusters.clusters, trajs) golden = [1, 2, 2, 5] self.assertEqual(degeneracy, golden)
def testUCBCalculator(self): params = spawning.SpawningParams() params.alpha = 8.0 UCB = spawning.UCBCalculator(params) clusters = clustering.Clusters() sizes = [6, 2, 3, 1] energies = [-4, -2, -2, -1] for size, energy in zip(sizes, energies): cluster = clustering.Cluster(None, None, None, None, metricCol=0) cluster.elements = size cluster.metrics = [energy] clusters.addCluster(cluster) trajs = 20 degeneracy = UCB.calculate(clusters.clusters, trajs) golden = np.array([3, 5, 3, 9]) np.testing.assert_array_equal(degeneracy, golden)
def testSameWeightDegeneracyCalculator(self): params = spawning.SpawningParams() sameWeightDegCalculator = spawning.SameWeightDegeneracyCalculator( params) clusters = clustering.Clusters() sizes = [6, 2, 3, 1] for size in sizes: cluster = clustering.Cluster(None, None, None, None) cluster.elements = size clusters.addCluster(cluster) trajs = 10 degeneracy = sameWeightDegCalculator.calculate(clusters.clusters, trajs) golden = [1, 1, 1, 1] self.assertEqual(degeneracy, golden)
def testMSMUncertainty(self): golden_q = np.array([ 1.855343631686e-05, 1.087041734182e-05, 1.219694386094e-07, 9.760782392578e-07, 5.936453161858e-04, 9.940065039403e-05 ]) counts = np.array([[4380, 153, 15, 2, 0, 0], [211, 4788, 1, 0, 0, 0], [169, 1, 4604, 226, 0, 0], [3, 13, 158, 4823, 3, 0], [0, 0, 0, 4, 4978, 18], [7, 5, 0, 0, 62, 4926]]) params = spawning.SpawningParams() params.lagtime = 1 dtrajs = generateDTrajs(counts) cl = ClMock(dtrajs) MSMP = spawning.UncertaintyMSMCalculator(params) calc_q, _ = MSMP.calculate_q(counts, 6) ntrajs = 100 degeneracy = MSMP.calculate(cl, ntrajs) golden = np.array([3, 1, 0, 0, 82, 14]) np.testing.assert_almost_equal(calc_q, golden_q) np.testing.assert_array_equal(degeneracy, golden)
def testEpsilonCalculator(self): params = spawning.SpawningParams() params.epsilon = 0.5 params.metricWeights = "linear" params.nclusters = 100 epsilon = spawning.EpsilonDegeneracyCalculator(params) clusters = clustering.Clusters() sizes = [6, 2, 3, 1] energies = [-4, -2, -2, -1] for size, energy in zip(sizes, energies): cluster = clustering.Cluster(None, None, None, None, metricCol=0) cluster.elements = size cluster.metrics = [energy] clusters.addCluster(cluster) trajs = 20 degeneracy = epsilon.calculate(clusters.clusters, trajs) golden = np.array([7, 4, 4, 5]) np.testing.assert_array_equal(degeneracy, golden)
def main(args): # Parameters clusteringObj = utilities.readClusteringObject(args.clusteringObj) native = args.native pathwayFilename = args.pathwayFilename ntrajs = args.ntrajs threshold = args.threshold RMSDCalc = RMSDCalculator.RMSDCalculator(clusteringObj.symmetries) # use graph algorithm to establish a path initial_cluster = 0 final_cluster = getOptimalCluster(clusteringObj, native, RMSDCalc) distanceMatrix = createNetworkMatrix(clusteringObj, threshold, RMSDCalc) predecessors = obtainShortestPath(distanceMatrix) pathway = createPathway(initial_cluster, final_cluster, predecessors) print "Pathway clusters:" print pathway # write pathway into a single trajectory writePathwayTrajectory(clusteringObj, pathway, pathwayFilename, native) # create clustering object with only the pathway clusters ClPath = clustering.Clustering() ClPath.clusters.clusters = map( lambda x: clusteringObj.clusters.clusters[x], pathway) # spawning along the trajectory spawningParams = spawning.SpawningParams() densityCalculatorBuilder = densitycalculator.DensityCalculatorBuilder() densityCalculator = densityCalculatorBuilder.build({}) spawningPathway = spawning.InverselyProportionalToPopulationCalculator( densityCalculator) # Set a least 1 processors from the extrems of the path degeneracies = spawningPathway.calculate(ClPath.clusters.clusters, ntrajs - 2, spawningParams) degeneracies[0] += 1 degeneracies[-1] += 1 print "degeneracies over pathway:" print degeneracies print ""
# densityCalculator = densityCalculatorBuilder.build({ # "density": { # "type": "heaviside", # "params": { # "conditions": [1.5, 1.0, 0.0], # "values": [8.0, 2.37, 1.0, 0.5] # } # } # } # ) densityCalculator = densityCalculatorBuilder.build({}) densityCalculator = densityCalculatorBuilder.build( {"density": { "type": "continuous" }}) spawnParams = spawning.SpawningParams() spawnParams.buildSpawningParameters({ "type": "inverselyProportional", "params": { "epsilon": 0.0, "T": 1000, "reportFilename": "report", "metricColumnInReport": 5 } }) spawnParams.buildSpawningParameters({ "type": "epsilon", "params": { "epsilon": 0.0, "T": 1000, "reportFilename": "report",
def main(jsonBlock): # Parameters firstControlFile = jsonBlock["firstControlFile"] ntrajs = jsonBlock["ntrajs"] resname = jsonBlock["resname"] symmetries = jsonBlock["symmetries"] trajFolder = jsonBlock["trajFolder"] clusteringObject = jsonBlock["clusteringObject"] clusteringThreshold = jsonBlock["clusteringThreshold"] pathwayFilename = jsonBlock["pathwayFilename"] templetizedInitialName = jsonBlock["templetizedInitialName"].encode() secondControlFileTemp = jsonBlock["secondControlFileTemp"] secondControlFile = jsonBlock["secondControlFile"] distanceThreshold = jsonBlock["distanceThreshold"] if firstControlFile: # Run first adaptive adaptiveSampling.main(firstControlFile) # Cluster trajectories ClOrd, thresholdCalculator = clusterTrajectories(resname, trajFolder, clusteringObject, clusteringThreshold, symmetries=symmetries) # use graph algorithm to establish a path initial_cluster = 0 final_cluster = ClOrd.getOptimalMetric() pathway = createPathway(initial_cluster, final_cluster, ClOrd, distanceThreshold) # write pathway into a single trajectory writePathwayTrajectory(ClOrd, pathway, pathwayFilename) # create clustering object with only the pathway clusters ClPath = OrderedContactsClustering(thresholdCalculator, resname=resname, reportBaseFilename="report", columnOfReportFile=4, symmetries=symmetries) ClPath.clusters.clusters = map(lambda x: ClOrd.clusters.clusters[x], pathway) # spawning along the trajectory spawningParams = spawning.SpawningParams() densityCalculatorBuilder = densitycalculator.DensityCalculatorBuilder() densityCalculator = densityCalculatorBuilder.build({}) spawningPathway = spawning.InverselyProportionalToPopulationCalculator( densityCalculator) # Set a least 1 processors from the extrems of the path degeneracies = spawningPathway.calculate(ClPath.clusters.clusters, ntrajs - 2, spawningParams) degeneracies[0] += 1 degeneracies[-1] += 1 print "degeneracies over pathway:" print degeneracies print "" plots = False if plots: plotMetricsDegeneracy(ClPath, resname, degeneracies) # Prepare second adaptive makeNewControlFile(degeneracies, ClPath, templetizedInitialName, secondControlFileTemp, secondControlFile) adaptiveSampling.main(secondControlFile)