def testCluster(self): # preparation clusteringBuilder = clustering.ClusteringBuilder() clusteringParams = {"type": "rmsd", "params": {"ligandResname": "AIN", "contactThresholdDistance": 8}} clusteringInstance = clusteringBuilder.buildClustering(clusteringParams, "ain_report", 3) trajNames = ["tests/data/aspirin_data/traj*"] # function to test clusteringInstance.cluster(trajNames) # assertion allClusters = clusteringInstance.clusters.clusters goldenNumberOfClusters = 2 goldenEnergyCluster1 = -8424.8 goldenEnergyCluster2 = -8453.29 goldenElementsCluster1 = 2 goldenElementsCluster2 = 1 self.assertEqual(len(allClusters), goldenNumberOfClusters) self.assertAlmostEqual(allClusters[0].getMetric(), goldenEnergyCluster1, 2) self.assertAlmostEqual(allClusters[1].getMetric(), goldenEnergyCluster2, 2) self.assertEqual(allClusters[0].elements, goldenElementsCluster1) self.assertEqual(allClusters[1].elements, goldenElementsCluster2)
def testCluster_protein_protein(self): # preparation clusteringBuilder = clustering.ClusteringBuilder() clusteringParams = {"type": "rmsd", "params": {"ligandChain": "B", "contactThresholdDistance": 8}, "thresholdCalculator" : { "type" : "constant", "params" : { "value" : 4.0 } }} clusteringInstance = clusteringBuilder.buildClustering(clusteringParams, "report", 3) trajNames = ["tests/data/protein_protein_data/traj*"] # function to test clusteringInstance.cluster(trajNames) # assertion allClusters = clusteringInstance.clusters.clusters goldenNumberOfClusters = 2 goldenEnergyCluster1 = -18906.7 goldenEnergyCluster2 = -18905 goldenElementsCluster1 = 8 goldenElementsCluster2 = 3 self.assertEqual(len(allClusters), goldenNumberOfClusters) self.assertAlmostEqual(allClusters[0].getMetric(), goldenEnergyCluster1, 2) self.assertAlmostEqual(allClusters[1].getMetric(), goldenEnergyCluster2, 2) self.assertEqual(allClusters[0].elements, goldenElementsCluster1) self.assertEqual(allClusters[1].elements, goldenElementsCluster2)
def getWorkingClusteringObjectAndReclusterIfNecessary( firstRun, outputPathConstants, clusteringBlock, spawningParams, simulationRunner, topologies, processManager): """ It reads the previous clustering method, and, if there are changes, it reclusters the previous trajectories. Returns the clustering object to use :param firstRun: New epoch to run :type firstRun: int :param outputPathConstants: Contains outputPath-related constants :type outputPathConstants: :py:class:`.OutputPathConstants` :param clusteringBlock: Contains the new clustering block :type clusteringBlock: json :param spawningParams: Spawning params, to know what reportFile and column to read :type spawningParams: :py:class:`.SpawningParams` :param topologies: Topology object containing the set of topologies needed for the simulation :type topologies: :py:class:`.Topology` :param processManager: Object to synchronize the possibly multiple processes :type processManager: :py:class:`.ProcessesManager` :returns: :py:class:`.Clustering` -- The clustering method to use in the adaptive sampling simulation """ if not processManager.isMaster(): for ij in range(firstRun): topologies.readMappingFromDisk( outputPathConstants.epochOutputPathTempletized % ij, ij) return lastClusteringEpoch = firstRun - 1 clusteringObjectPath = outputPathConstants.clusteringOutputObject % ( lastClusteringEpoch) oldClusteringMethod = utilities.readClusteringObject(clusteringObjectPath) clusteringBuilder = clustering.ClusteringBuilder() clusteringMethod = clusteringBuilder.buildClustering( clusteringBlock, spawningParams.reportFilename, spawningParams.reportCol) clusteringMethod.setProcessors(simulationRunner.getWorkingProcessors()) if needToRecluster(oldClusteringMethod, clusteringMethod): utilities.print_unbuffered("Reclustering!") startTime = time.time() clusterPreviousEpochs(clusteringMethod, firstRun, outputPathConstants.epochOutputPathTempletized, simulationRunner, topologies, outputPathConstants.allTrajsPath) endTime = time.time() utilities.print_unbuffered("Reclustering took %s sec" % (endTime - startTime)) else: clusteringMethod = oldClusteringMethod clusteringMethod.setCol(spawningParams.reportCol) for ij in range(firstRun): topologies.readMappingFromDisk( outputPathConstants.epochOutputPathTempletized % ij, ij) return clusteringMethod
def buildNewClusteringAndWriteInitialStructuresInNewSimulation( debug, controlFile, outputPathConstants, clusteringBlock, spawningParams, initialStructures, simulationRunner, processManager): """ Build the clustering object and copies initial structures from control file. Returns the clustering object to use and the initial structures filenames as string :param debug: In debug, it will not remove the simulations :type debug: bool :param controlFile: Adaptive sampling control file :type controlFile: str :param outputPathConstants: Contains outputPath-related constants :type outputPathConstants: :py:class:`.OutputPathConstants` :param clusteringBlock: Contains the new clustering block :type clusteringBlock: json :param spawningParams: Spawning params :type spawningParams: :py:class:`.SpawningParams` :param initialStructures: Control file initial structures :type initialStructures: list :param simulationRunner: :py:class:`.SimulationRunner` Simulation runner object :type simulationRunner: :py:class:`.SimulationRunner` :param processManager: Object to synchronize the possibly multiple processes :type processManager: :py:class:`.ProcessesManager` :returns: :py:class:`.Clustering`, str -- The clustering method to use in the adaptive sampling simulation and the initial structures filenames """ firstRun = 0 if processManager.isMaster(): saveInitialControlFile(controlFile, outputPathConstants.originalControlFile) copyInitialStructures(initialStructures, outputPathConstants.tmpInitialStructuresTemplate, firstRun) processManager.barrier() initialStructuresAsString = simulationRunner.createMultipleComplexesFilenames( len(initialStructures), outputPathConstants.tmpInitialStructuresTemplate, firstRun) if processManager.isMaster(): clusteringBuilder = clustering.ClusteringBuilder() clusteringMethod = clusteringBuilder.buildClustering( clusteringBlock, spawningParams.reportFilename, spawningParams.reportCol) else: clusteringMethod = None return clusteringMethod, initialStructuresAsString
def test_cluster_accumulative(self): # preparation clusteringParams = {"type": "contactMap", "params": {"ligandResname": "AIN", "contactThresholdDistance": 8, "similarityEvaluator": "correlation"}, "thresholdCalculator": { "type": "constant", "params": { "value": 0.15 } }} clusteringBuilder = clustering.ClusteringBuilder() clusteringInstance = clusteringBuilder.buildClustering(clusteringParams, "ain_report", 3) trajNames = ["tests/data/aspirin_data/traj*"] # function to test clusteringInstance.cluster(trajNames) # assertion allClusters = clusteringInstance.clusters.clusters # goldenNumberOfClusters = 3 # goldenEnergyCluster1 = -8421.5 # goldenEnergyCluster2 = -8424.8 # goldenEnergyCluster3 = -8453.29 # goldenElementsCluster1 = 1 # goldenElementsCluster2 = 1 # goldenElementsCluster3 = 1 goldenNumberOfClusters = 2 goldenEnergyCluster1 = -8424.8 goldenEnergyCluster2 = -8453.29 goldenElementsCluster1 = 2 goldenElementsCluster2 = 1 self.assertEqual(len(allClusters), goldenNumberOfClusters) self.assertAlmostEqual(allClusters[0].getMetric(), goldenEnergyCluster1, 2) self.assertAlmostEqual(allClusters[1].getMetric(), goldenEnergyCluster2, 2) # self.assertAlmostEqual(allClusters[2].getMetric(), goldenEnergyCluster3, 2) self.assertEqual(allClusters[0].elements, goldenElementsCluster1) self.assertEqual(allClusters[1].elements, goldenElementsCluster2)