def create_merged_bayesian_network_for_event( event, prefix, bayesian_network, assert_merge_definitions=False, logger=StdOutLogger(verbose=False)): merge_def_list = BayesianNetworkMergeUtils.create_merge_definition_list( event, prefix, merge_singleton_events=False) logger.log('Merging...', includeTimestamp=True) logger.set_carriage_reset(True) count = len(merge_def_list) for index, merge_def in enumerate(merge_def_list): logger.log("\rMerging '%s' (%i of %i)" % (merge_def.get_merged_event(), index + 1, count), includeTimestamp=True) merge_defs = DistributionEventMergeDefinitions( event.name, bayesian_network, allow_unspecified_events=True, assert_merge_definitions=assert_merge_definitions) merge_defs.set_merge_definitions([merge_def]) bn_wrapper = BayesianNetworkWrapper(bayesian_network) bayesian_network = bn_wrapper.create_network_with_merged_events( merge_defs, bake=True) logger.set_carriage_reset(False) logger.log('Merging done!', includeTimestamp=True) return bayesian_network
def instantiate(foldername, logger=StdOutLogger(verbose=False)): broker = DefaultPersistenceHandler._get_filenames_broker(foldername) out = DefaultPersistenceHandler._load_cells_from_filename_broker( broker, logger) logger.log('Donors loaded from folder ' + foldername, includeTimestamp=True, onlyIfVerbose=False) return out
def checkFloatValBounds(self, dictKey, valueString, lowerBound=None, upperBound=None): val = self.configParser.getfloat(dictKey, valueString) if lowerBound != None: if val < lowerBound: val = self.getFloatDefault(dictKey, valueString) Log.err("value of " + valueString + " must be >= " + str(lowerBound) + ", using default instead = " + str(val)) if upperBound != None: if val > upperBound: val = self.getFloatDefault(dictKey, valueString) Log.err("value of " + valueString + " must be <= " + str(upperBound) + ", using default instead = " + str(val)) return val
def create_merged_bayesian_network_for_events( events, prefixes, bayesian_network, assert_merge_definitions=False, logger=StdOutLogger(verbose=False)): for event, prefix in zip(events, prefixes): bayesian_network = BayesianNetworkMergeUtils.create_merged_bayesian_network_for_event( event, prefix, bayesian_network, assert_merge_definitions, logger) return bayesian_network
def __updateCollisions(self): """This is a private method, and should only be called from the public update() method. Discovers collisions and determines merges.""" for focus in self.foci.values(): if focus.isEnabled: collisions = {} for i in xrange(len(focus.perimeter)): perimeterCell = focus.perimeter[i] perimeterNeighbours = Worldspace.getMooreNeighbours( self.world, perimeterCell.location, EpithelialStates.INFECTION_DEATH) for neighbour in perimeterNeighbours: if neighbour.focusId != perimeterCell.focusId: if collisions.has_key(neighbour.focusId): if not (neighbour.location in collisions[neighbour.focusId]): collisions[neighbour.focusId].append( neighbour.location) else: continue else: collisions[neighbour.focusId] = [ neighbour.location ] for collisionList in collisions.values(): collisionCount = len(collisionList) if collisionCount > ( len(focus.perimeter) * (FocusSystem.COLLISION_MERGE_PERCENTAGE / 100.0) if len(focus.perimeter) * (FocusSystem.COLLISION_MERGE_PERCENTAGE / 100.0) > 1 else 1): focus.isEnabled = False if FocusSystem.DEBUG_TEXT_ENABLED: Log.out("Merge detected on Focus #%s" % focus.id) self.mergeDetected.append(focus)
def __init__(self, params_fn, marginals_fn, name='', logger=StdOutLogger(verbose=False)): self._logger = logger self._params_fn = params_fn self._marginal_fn = marginals_fn self._name = name self._genmodel = GenModel(params_fn, marginals_fn) self._logger.log('GenModelWrapper.GenModel class loaded (%s, %s)' % (params_fn, marginals_fn), onlyIfVerbose=True) self._init_names_and_nicknames() self._insertion_compositions = self._init_insertion_compositions() self._insertion_lengths = self._init_insertion_lengths() self._network_wrapper = self._init_network_wrapper() self._logger.log('GenModelWrapper loaded', onlyIfVerbose=True)
def __init__(self, logger=StdOutLogger(False)): self._items = {} self._models = GenModelWrappers(logger) self._logger = logger
def run(self, settings): """Main loop.""" self.avgFociAreaMM2 = None if Graph.SHOW: data = SimulationData() graph = OverallSimulationDataGraph() graph.setXMeasurement('hours') graph.setTimestepsInXMeasurement(6) fociAreaGraph = FociAreaGraph() #fociAreaGraph = FociAreaGraph(True, Worldspace.GRID_WIDTH, Worldspace.GRID_HEIGHT) fociAreaGraph.setXMeasurement('hours') fociAreaGraph.setTimestepsInXMeasurement(6) if SimVis.ENABLED: q.put((simVis.display, (), {})) self.numberOfRuns = settings["iNumberOfRuns"] self.runTime = settings["iRunTime"] self.debugTextEnabled = settings["bDebugTextEnabled"] #initialise runs run = 0 while run < self.numberOfRuns: if self.debugTextEnabled: startTime = time.clock() Log.out("Start time: %s" % startTime) #re-initialize world and systems if not on the initial run world = [] for x in xrange(Worldspace.GRID_WIDTH): world.append([]) for y in xrange(Worldspace.GRID_HEIGHT): world[x].append(Worldsite(Vector2d(x, y))) eSys = Systems.EpithelialSystem(world) immSys = Systems.ImmuneSystem(world) eSys.initialise() if(Systems.ImmuneSystem.ISENABLED): immSys.initialise() if Graph.SHOW: graph.setTotalEpithelialCells(Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) graph.setBaseImmuneCells(immSys.INIT_CELLS) graph.initRun() fociAreaGraph.initRun() if SimVis.ENABLED: simVis.init(world, run + 1) # Run simulation for a given number of timesteps # 10 days = 1440 timesteps timesteps = 0 while timesteps <= self.runTime: eSys.update() if(Systems.ImmuneSystem.ISENABLED): immSys.update() eSys.synchronise() if(Systems.ImmuneSystem.ISENABLED): immSys.synchronise() if Graph.SHOW: data.time = timesteps data.eCellsHealthy = eSys.healthyCount data.eCellsContaining = eSys.containingCount data.eCellsExpressing = eSys.expressingCount data.eCellsInfectious = eSys.infectiousCount data.eCellsDead = eSys.naturalDeathCount + eSys.infectionDeathCount data.immCellsTotal = immSys.virginCount + immSys.matureCount graph.addSimulationData(data) if(Systems.FocusSystem.ENABLED): area = 0.0 c = 0 for foci in eSys.fSys.foci.values() : if foci.isEnabled : if foci.cellCount != 0 : area += foci.cellCount c += 1 if c == 0 : area = 0 else : area = area / c self.avgFociAreaMM2 = fociAreaGraph.addAverageFociAreaData(area, timesteps) if self.debugTextEnabled: Log.out('%d: %d' %(run + 1, timesteps)) Log.out("Infected cells: %s" % (eSys.containingCount + eSys.expressingCount + eSys.infectiousCount)) Log.out("Healthy: %s" % (eSys.healthyCount)) Log.out("Containing: %s" % (eSys.containingCount)) Log.out("Expressing: %s" % (eSys.expressingCount)) Log.out("Infectious: %s" % (eSys.infectiousCount)) Log.out("Dead: %s" % (eSys.naturalDeathCount + eSys.infectionDeathCount)) Log.out("Virgin: %s" % (immSys.virginCount)) Log.out("Mature: %s" % (immSys.matureCount)) if Systems.FocusSystem.ENABLED and self.avgFociAreaMM2 != None : #Log.out("Average focus area: %s" % (eSys.avgFociArea)) Log.out("Average focus area (mm2): %s" % (self.avgFociAreaMM2)) Log.out("\n") if SimVis.ENABLED: if timesteps == 0 or timesteps % 72 == 0 : simVis.drawSimWorld(True, timesteps) else : simVis.drawSimWorld(False, timesteps) if Systems.FocusSystem.ENABLED: if len(eSys.fSys.mergeDetected) > 0: for i in xrange(len(eSys.fSys.mergeDetected) - 1, -1, -1): if SimVis.HIGHLIGHT_COLLISIONS: focus = eSys.fSys.mergeDetected[i] for perimeterCell in focus.perimeter: simVis.drawCollision(perimeterCell) del eSys.fSys.mergeDetected[i] # HACK: For debugging/testing purposes. Will be removed/refactored soon. if SimVis.HIGHLIGHT_COLLISIONS and SimVis.ENABLED: simVis._SimVis__savePILImageToFile(False) simVis._SimVis__updateCanvas(False) #if Systems.FocusSystem.DEBUG_TEXT_ENABLED: #raw_input() timesteps += 1 if(Systems.FocusSystem.ENABLED): if self.debugTextEnabled : out = "remaining usable foci: " c = 0 for focus in eSys.fSys.foci.values() : if focus.isEnabled and focus.cellCount > 0: c += 1 out += str(focus.id) + ", " Log.out(out + " count = " + str(c) +"\n") if self.debugTextEnabled: endTime = time.clock() Log.out("End time: %s" % endTime) Log.out("Elapsed time: %s" % (endTime - startTime)) #increment run run += 1 # All runs finished: display results graph if Graph.SHOW: if(Systems.FocusSystem.ENABLED): q.put((fociAreaGraph.showGraph, ([True]), {})) q.put((graph.showGraph, ([True]), {})) running = False
def __init__(self, logger=StdOutLogger(verbose=False)): self._logger = logger self._gen_model_wrapper = None self._gen_model = None
def __init__(self, logger=StdOutLogger(verbose=False)): super(DefaultPersistenceHandler, self).__init__(logger)
def __debugPrint(self): """Debug method, to be removed. Prints the focus by id and it's area.""" for focus in self.foci.values(): if focus.isEnabled: Log.out("Focus #%s: %s" % (focus.id, focus.cellCount))
def __init__(self, logger=StdOutLogger(verbose=False)): super(VChoiceContentValidator, self).__init__(logger)
def run(self, settings): """Main loop.""" self.avgFociAreaMM2 = None if Graph.SHOW: data = SimulationData() graph = OverallSimulationDataGraph() graph.setXMeasurement('hours') graph.setTimestepsInXMeasurement(6) fociAreaGraph = FociAreaGraph() #fociAreaGraph = FociAreaGraph(True, Worldspace.GRID_WIDTH, Worldspace.GRID_HEIGHT) fociAreaGraph.setXMeasurement('hours') fociAreaGraph.setTimestepsInXMeasurement(6) if SimVis.ENABLED: q.put((simVis.display, (), {})) self.numberOfRuns = settings["iNumberOfRuns"] self.runTime = settings["iRunTime"] self.debugTextEnabled = settings["bDebugTextEnabled"] #initialise runs run = 0 while run < self.numberOfRuns: if self.debugTextEnabled: startTime = time.clock() Log.out("Start time: %s" % startTime) #re-initialize world and systems if not on the initial run world = [] for x in xrange(Worldspace.GRID_WIDTH): world.append([]) for y in xrange(Worldspace.GRID_HEIGHT): world[x].append(Worldsite(Vector2d(x, y))) eSys = Systems.EpithelialSystem(world) immSys = Systems.ImmuneSystem(world) eSys.initialise() if (Systems.ImmuneSystem.ISENABLED): immSys.initialise() if Graph.SHOW: graph.setTotalEpithelialCells(Worldspace.GRID_WIDTH * Worldspace.GRID_HEIGHT) graph.setBaseImmuneCells(immSys.INIT_CELLS) graph.initRun() fociAreaGraph.initRun() if SimVis.ENABLED: simVis.init(world, run + 1) # Run simulation for a given number of timesteps # 10 days = 1440 timesteps timesteps = 0 while timesteps <= self.runTime: eSys.update() if (Systems.ImmuneSystem.ISENABLED): immSys.update() eSys.synchronise() if (Systems.ImmuneSystem.ISENABLED): immSys.synchronise() if Graph.SHOW: data.time = timesteps data.eCellsHealthy = eSys.healthyCount data.eCellsContaining = eSys.containingCount data.eCellsExpressing = eSys.expressingCount data.eCellsInfectious = eSys.infectiousCount data.eCellsDead = eSys.naturalDeathCount + eSys.infectionDeathCount data.immCellsTotal = immSys.virginCount + immSys.matureCount graph.addSimulationData(data) if (Systems.FocusSystem.ENABLED): area = 0.0 c = 0 for foci in eSys.fSys.foci.values(): if foci.isEnabled: if foci.cellCount != 0: area += foci.cellCount c += 1 if c == 0: area = 0 else: area = area / c self.avgFociAreaMM2 = fociAreaGraph.addAverageFociAreaData( area, timesteps) if self.debugTextEnabled: Log.out('%d: %d' % (run + 1, timesteps)) Log.out("Infected cells: %s" % (eSys.containingCount + eSys.expressingCount + eSys.infectiousCount)) Log.out("Healthy: %s" % (eSys.healthyCount)) Log.out("Containing: %s" % (eSys.containingCount)) Log.out("Expressing: %s" % (eSys.expressingCount)) Log.out("Infectious: %s" % (eSys.infectiousCount)) Log.out( "Dead: %s" % (eSys.naturalDeathCount + eSys.infectionDeathCount)) Log.out("Virgin: %s" % (immSys.virginCount)) Log.out("Mature: %s" % (immSys.matureCount)) if Systems.FocusSystem.ENABLED and self.avgFociAreaMM2 != None: #Log.out("Average focus area: %s" % (eSys.avgFociArea)) Log.out("Average focus area (mm2): %s" % (self.avgFociAreaMM2)) Log.out("\n") if SimVis.ENABLED: if timesteps == 0 or timesteps % 72 == 0: simVis.drawSimWorld(True, timesteps) else: simVis.drawSimWorld(False, timesteps) if Systems.FocusSystem.ENABLED: if len(eSys.fSys.mergeDetected) > 0: for i in xrange( len(eSys.fSys.mergeDetected) - 1, -1, -1): if SimVis.HIGHLIGHT_COLLISIONS: focus = eSys.fSys.mergeDetected[i] for perimeterCell in focus.perimeter: simVis.drawCollision(perimeterCell) del eSys.fSys.mergeDetected[i] # HACK: For debugging/testing purposes. Will be removed/refactored soon. if SimVis.HIGHLIGHT_COLLISIONS and SimVis.ENABLED: simVis._SimVis__savePILImageToFile(False) simVis._SimVis__updateCanvas(False) #if Systems.FocusSystem.DEBUG_TEXT_ENABLED: #raw_input() timesteps += 1 if (Systems.FocusSystem.ENABLED): if self.debugTextEnabled: out = "remaining usable foci: " c = 0 for focus in eSys.fSys.foci.values(): if focus.isEnabled and focus.cellCount > 0: c += 1 out += str(focus.id) + ", " Log.out(out + " count = " + str(c) + "\n") if self.debugTextEnabled: endTime = time.clock() Log.out("End time: %s" % endTime) Log.out("Elapsed time: %s" % (endTime - startTime)) #increment run run += 1 # All runs finished: display results graph if Graph.SHOW: if (Systems.FocusSystem.ENABLED): q.put((fociAreaGraph.showGraph, ([True]), {})) q.put((graph.showGraph, ([True]), {})) running = False
merge_defs.set_merge_definitions([merge_def1]) return merge_defs data_folder = get_testdata_folder() fn_ced = os.path.join(data_folder, 'models_imgt_ref_dir_sep2019', 'HC') fn_1363 = os.path.join(fn_ced, '1363_TRB') out_marginals = os.path.join(fn_1363, '1363_TRB_marginals.txt') out_params = os.path.join(fn_1363, '1363_TRB_params.txt') print out_marginals print out_params out_marginals = 'C:/CiR/pTCR/IGOR_models/Unproductive_models/models_imgt_ref_dir_sep2019/HC/1365_TRB/1365_TRB_marginals.txt' out_params = 'C:/CiR/pTCR/IGOR_models/Unproductive_models/models_imgt_ref_dir_sep2019/HC/1365_TRB/1365_TRB_params.txt' gmw = GenModelWrapper(out_params, out_marginals, '1363_TRB_', StdOutLogger()) gmw.merge_alleles(assert_merge_definitions=True) bn = gmw.get_bayesian_network_wrapper().get_network() # truncate_bn = create_truncated_network(bn) # truncate_bn_wrapper = BayesianNetworkWrapper(truncate_bn) merge_defs = create_merg_defs(bn) merged_bayesian_network = gmw.get_bayesian_network_wrapper( ).create_network_with_merged_events(merge_defs, bake=False) merged_bayesian_network.bake() bnwrapper = BayesianNetworkWrapper(merged_bayesian_network) states = {} n = gmw.get_eventname_for_nickname('v_choice') state = bnwrapper.get_state(n) states[n] = state
def __init__(self, logger=StdOutLogger(verbose=False)): super(DefaultValidator, self).__init__(logger) self._v_choice_content_validator = VChoiceContentValidator() self._out = None self._template = None
def SetConfiguration(self): """Reads the values from the config.ini file into a dictionary and returns it. Returns dict() <str, dyanmic> """ try: self.configParser.read(os.getcwd() + "\\config.ini") sections = self.configParser.sections() if len(sections) == 0: self.__reconstruct() return self.SetConfiguration() for section in xrange(len(sections)): configSettings = dict() str = sections[section] if str == "World": configSettings[ "bIsToroidal"] = self.configParser.getboolean( str, "bIsToroidal") configSettings["iGridWidth"] = self.checkIntValBounds( str, "iGridWidth", 1) configSettings["iGridHeight"] = self.checkIntValBounds( str, "iGridHeight", 1) Worldspace.Configure(configSettings) elif str == "General": self.configSettings[ "iNumberOfRuns"] = self.checkIntValBounds( str, "iNumberOfRuns", 1) self.configSettings["iRunTime"] = self.checkIntValBounds( str, "iRunTime", 0) self.configSettings[ "bDebugTextEnabled"] = self.configParser.getboolean( str, "bDebugTextEnabled") elif str == "Interface": pass elif str == "ImmuneSystem": configSettings["fBaseImmCell"] = self.checkFloatValBounds( str, "fBaseImmCell", 0) configSettings[ "bIsEnabled"] = self.configParser.getboolean( str, "bIsEnabled") configSettings["fRecruitment"] = self.checkFloatValBounds( str, "fRecruitment", 0) configSettings["iRecruitDelay"] = self.checkIntValBounds( str, "iRecruitDelay", 0) Systems.ImmuneSystem.Configure(configSettings) elif str == "EpithelialSystem": configSettings["fInfectInit"] = self.checkFloatValBounds( str, "fInfectInit", 0) configSettings[ "bRegenEnabled"] = self.configParser.getboolean( str, "bRegenEnabled") configSettings[ "bRandomAge"] = self.configParser.getboolean( str, "bRandomAge") Systems.EpithelialSystem.Configure(configSettings) elif str == "FocusSystem": configSettings[ "bIsEnabled"] = self.configParser.getboolean( str, "bIsEnabled") configSettings[ "iCollisionsForMergePercentage"] = self.checkIntValBounds( str, "iCollisionsForMergePercentage", 0, 100) configSettings[ "bDebugTextEnabled"] = self.configParser.getboolean( str, "bDebugTextEnabled") Systems.FocusSystem.Configure(configSettings) elif str == "ImmuneCell": configSettings["iImmuneLifespan"] = self.checkIntValBounds( str, "iImmuneLifespan", 0) Cells.ImmuneCell.Configure(configSettings) elif str == "EpithelialCell": configSettings[ "iEpithelialLifespan"] = self.checkIntValBounds( str, "iEpithelialLifespan", 0) configSettings["iDivisionTime"] = self.checkIntValBounds( str, "iDivisionTime", 0) configSettings["iExpressDelay"] = self.checkIntValBounds( str, "iExpressDelay", 0) configSettings["iInfectDelay"] = self.checkIntValBounds( str, "iInfectDelay", 0) configSettings["iInfectLifespan"] = self.checkIntValBounds( str, "iInfectLifespan", 0) configSettings["fInfectRate"] = self.checkFloatValBounds( str, "iInfectRate", 0) Cells.EpithelialCell.Configure(configSettings) elif str == "SimulationVisualisation": configSettings[ "bIsEnabled"] = self.configParser.getboolean( str, "bIsEnabled") configSettings[ "bSnapshotEnabled"] = self.configParser.getboolean( str, "bSnapshotEnabled") configSettings["iSnapshotHeight"] = self.checkIntValBounds( str, "iSnapshotHeight", 0) configSettings["iSnapshotWidth"] = self.checkIntValBounds( str, "iSnapshotWidth", 0) configSettings["iSquareSize"] = self.checkIntValBounds( str, "iSquareSize", 1) configSettings[ "bDebugFocusIdEnabled"] = self.configParser.getboolean( str, "bDebugFocusIdEnabled") configSettings[ "bHighlightCollisions"] = self.configParser.getboolean( str, "bHighlightCollisions") SimulationVisualization.SimVis.Configure(configSettings) elif str == "Graph": configSettings[ "bShowGraphOnFinish"] = self.configParser.getboolean( str, "bShowGraphOnFinish") Graph.Graph.Configure(configSettings) except Exception as e: if self.reconstruct == False: Log.err("Error in config file: " + e.message) Log.err("Restoring to defaults...") self.__reconstruct() return self.SetConfiguration() else: raise Exception("Failed to reconstruct config.ini file") return self.configSettings
def __init__(self, logger=StdOutLogger(verbose=False)): self._logger = logger self._items = {}