#!/usr/local/bin/python '''This file is intended to hold some routines that are used in multiple places (hence need to be importable) but perform high-level (and not very general) tasks.''' import os import sets from PIPRes.util.crude_xml import xmlFilePathToPipres, toXMLGrouping, toPipresXML from PIPRes.util.io import cipresGetLogger _LOG = cipresGetLogger('adhoc') class Statistic: "placeholder for a generic statistic class" pass def simulateAndGetSplitFreqs(simulator, splitFreqSuppliers, splits, tempFile = None): '''Performs a simulation study using iteration through "simulator" and analyzing the data with "splitFreqSuppliers" to get the split frequencies for every split in the iterable "splits". Returns a list of results that can be serialized using toPipresXML(). ''' resultsList = toXMLGrouping('simCondition') resultsList.nSites = simulator.nSites for n, matrix in enumerate(iter(simulator)): try: print '\n#################### REALIZATION', n, '####################\n' resultsSaver = toXMLGrouping('realization') resultsSaver.number = n resultsSaver.simseed = simulator.getPrevSeed() results = {} for supplier in splitFreqSuppliers: supplier.setMatrix(matrix) results[supplier.TMP_Name] = supplier.getSplitsTable() if splits: splitsToRecord = splits else: splitsToRecord = sets.Set() for freqs in results.itervalues(): splitsToRecord.update(freqs.keys()) for spl in splitsToRecord: resultForSplitSaver = toXMLGrouping('supportFreq') resultForSplitSaver.append(['split', spl]) for supplier in splitFreqSuppliers: statistic = Statistic() statistic.type = supplier.TMP_Type statistic.name = supplier.TMP_Name statistic.value = results[supplier.TMP_Name].get(spl, -1.0) resultForSplitSaver.append(['genericstat', statistic]) resultsSaver.append(resultForSplitSaver) resultsList.append(resultsSaver) _LOG.warn('CLEANING temp Dir!!!') os.system('/bin/sh /Users/mholder/bin/cleanCIPRESTemps.sh') finally: if tempFile: tempoutput = open(tempFile, 'w') tempoutput.write(toPipresXML([resultsList])) tempoutput.close() return resultsList def readSimulationModelSpecFile(filepath): '''Reads a file for tree and splits. The first tree will be the model tree.''' d = xmlFilePathToPipres(filepath, ['sim_conditions', 'tree', 'split']) try: tree = d['tree'] except: raise RuntimeError, 'Expecting %s to contain a tree' try: simConditions = d['sim_conditions'][0] except: raise RuntimeError, 'Expecting %s to contain a sim_conditions element' return simConditions, tree, d.get('split', [])