def addNewHistory(histories, index, file=None, stats=None, braney=None, tree=None): # Just do not process if run time > MAX_TIMER_LENGTH if time.time() > TIMER_END: return histories # Generate new history from the last one newHistory = chooseNewHistory(histories[-1], TEMPERATURE, depth=1, index=index) # DEBUG #if debug.TRIGGER_TEST: # newHistory.validate() # debug.TRIGGER_TEST = False c = newHistory.rearrangementCost() FH = flattened.flattenGraph(newHistory) S = FH.simplifyStubsAndTrivials() F = S.removeLowRatioEvents(debug.RATIO_CUTOFF) O = ordered.OrderedHistory(F) if stats is not None: stats.write("%s\n" % newHistory.stats()) if braney is not None: # The + 1 takes into account the fact that history 0 is the one created initially, # the MCMC histories start at index 1 braney.write("%s\n" % O.braneyText(index + 1, c)) if tree is not None: tree.write("%s\n" % O.newick()) if file is None: histories.append(newHistory) return histories else: # Really, who need to pickle files when there's a perfectly good zipped braney file next to it? #pickle.dump(newHistory, file) # Cheap trick: only store minimal and latest histories if sum(c) / 2 < sum(histories[0].rearrangementCost()) / 2: # Memory optimization trick -> Frees unused matrices and overlap tables histories[0].embalmDeadHistories(newHistory) return [newHistory] else: return [histories[0], newHistory]
def addNewHistory(histories, index, file=None, stats=None, braney=None, tree=None): # Just do not process if run time > MAX_TIMER_LENGTH if time.time() > TIMER_END: return histories # Generate new history from the last one newHistory = chooseNewHistory(histories[-1], TEMPERATURE, depth=1, index=index) # DEBUG #if debug.TRIGGER_TEST: # newHistory.validate() # debug.TRIGGER_TEST = False c = newHistory.rearrangementCost() FH = flattened.flattenGraph(newHistory) S = FH.simplifyStubsAndTrivials() F = S.removeLowRatioEvents(debug.RATIO_CUTOFF) O = ordered.OrderedHistory(F) if stats is not None: stats.write("%s\n" % newHistory.stats()) if braney is not None: # The + 1 takes into account the fact that history 0 is the one created initially, # the MCMC histories start at index 1 braney.write("%s\n" % O.braneyText(index + 1, c)) if tree is not None: tree.write("%s\n" % O.newick()) if file is None: histories.append(newHistory) return histories else: # Really, who need to pickle files when there's a perfectly good zipped braney file next to it? #pickle.dump(newHistory, file) # Cheap trick: only store minimal and latest histories if sum(c)/2 < sum(histories[0].rearrangementCost())/2: # Memory optimization trick -> Frees unused matrices and overlap tables histories[0].embalmDeadHistories(newHistory) return [newHistory] else: return [histories[0], newHistory]
def main(): options = _parseOptions() sampleGraphCycles.TEMPERATURE = options.temp if options.dir is not None: if not os.path.exists(options.dir): os.mkdir(options.dir) os.chdir(options.dir) if options.index is None: ## Initial graph construction G = _parseGraph(options) B = balancedAVG.BalancedAVG(G) C = cactus.Cactus(B) pickle.dump(C, open('CACTUS', "wb")) else: H = None if options.debug: ## Picking up from where we started OC = pickle.load(open('CACTUS_%i' % options.index)) random.setstate(pickle.load(open("STATE_%i" % options.index))) elif options.cont: ## Picking up from where we stopped OC = pickle.load(open('CACTUS_%i' % options.index)) ## Going through all the histories to the last in file file= open('HISTORIES_%i' % (options.index)) while True: try: H = pickle.load(file) except: break file.close() else: ## Just moving from there pickle.dump(random.getstate(), open("STATE_%i" % options.index, "wb")) C = pickle.load(open('CACTUS')) ## Sampling possible cactus NC = normalized.NormalizedCactus(C) if debug.RATIO_TO_OFFSET: BC = balancedCactus.BalancedCactus(NC) else: BC = NC OC = oriented.OrientedCactus(BC) ## Saving sampled cactus pickle.dump(OC, open('CACTUS_%i' % options.index, "wb")) # Moving into historical space if options.integer: debug.INTEGER_HISTORY = True if H is None: H = cycleCover.initialHistory(OC) FH = flattened.flattenGraph(H) S = FH.simplifyStubsAndTrivials() F = S.removeLowRatioEvents(debug.RATIO_CUTOFF) O = ordered.OrderedHistory(F) # Preparing file for progressive write if options.cont: stats_file = open("HISTORY_STATS_%li" % options.index, "a") pickle_file = open('HISTORIES_%i' % options.index, "ab") braney_file = gzip.open("HISTORIES_%i.braney" % options.index, "a") else: stats_file = open("HISTORY_STATS_%li" % options.index, "w") pickle_file = open('HISTORIES_%i' % options.index, "wb") braney_file = gzip.open("HISTORIES_%i.braney" % options.index, "w") stats_file.write("%s\n" % H.stats()) #pickle.dump(H, pickle_file) braney_file.write("%s\n" % O.braneyText(0, H.rearrangementCost())) #tree_file = open("HISTORY_TREES_%li" % options.index, "w") #tree_file.write("%s\n" % O.newick()) tree_file = None # Sampling for i in range(options.size): H2 = copy.copy(H) # Shuffle events for event in list(H.parent): H2.correctSchedulingError(event) H = H2 stats_file.write("%s\n" % H.stats()) FH = flattened.flattenGraph(H) S = FH.simplifyStubsAndTrivials() F = S.removeLowRatioEvents(debug.RATIO_CUTOFF) O = ordered.OrderedHistory(F) braney_file.write("%s\n" % O.braneyText(i+1, H.rearrangementCost())) # Cleaning up stats_file.close() pickle_file.close() braney_file.close() #tree_file.close() ## Removing temp file if os.path.exists("STATE_%i" % options.index): os.remove("STATE_%i" % options.index)