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 test_merge_definitions_3__duplicated_events(bayesian_network_1): merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*02']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) with pytest.raises(ValueError) as e: merge_defs.set_merge_definitions([merge_def1]) assert str( e.value) == DistributionEventMergeDefinitions.ERR_DUPLICATE_EVENT_MSG
def test_merge_definitions_2__event_not_found(bayesian_network_1): merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*04']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) with pytest.raises(ValueError) as e: merge_defs.set_merge_definitions([merge_def1]) assert str( e.value) == DistributionEventMergeDefinitions.ERR_EVENT_NOT_FOUND
def create_merg_defs(bn): merge_def1 = EventMergeDefinition('v_choice:TRBV7-2') merge_def1.extend([ 'v_choice:TRBV7-2*01', 'v_choice:TRBV7-2*02', 'v_choice:TRBV7-2*03', 'v_choice:TRBV7-2*04' ]) merge_defs = DistributionEventMergeDefinitions( 'GeneChoice_V_gene_Undefined_side_prio7_size147', bn, allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) return merge_defs
def test_merge_definitions_4__unspecified_event(bayesian_network_1): merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=False, assert_merge_definitions=True) with pytest.raises(ValueError) as e: merge_defs.set_merge_definitions([merge_def1]) assert str( e.value ) == DistributionEventMergeDefinitions.ERR_UNSPECIFIED_EVENTS_NOT_ALLOWED_MSG
def test_merge_definitions_5__one_merge_def_only(bayesian_network_1): merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_def2 = EventMergeDefinition('V2') merge_def2.extend(['V2*01']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) with pytest.raises(ValueError) as e: merge_defs.set_merge_definitions([merge_def1, merge_def2]) assert str( e.value ) == DistributionEventMergeDefinitions.ERR_ONLY_ONE_MERGE_DEF_ALLOWED_MSG
def test_marginals_7__merge_all_alleles(bayesian_network_3): wrapper = BayesianNetworkWrapper(bayesian_network_3) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', wrapper.get_network(), allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) assert (merged_network is not None) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename=None, probability_type=ProbabilityType.Conditional) assert len(margs) == 7
def test_marginals_5__merge_v_alleles(bayesian_network_1): wrapper = BayesianNetworkWrapper(bayesian_network_1) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename='v_gene', probability_type=ProbabilityType.Conditional) assert len(margs) == 2 assert utilities.almost_equal(margs.iloc[0, 0], 0.6, 0.0001) assert utilities.almost_equal(margs.iloc[1, 0], 0.4, 0.0001)
def test_marginals_4__merge_v_alleles(bayesian_network_1): wrapper = BayesianNetworkWrapper(bayesian_network_1) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename=None, probability_type=ProbabilityType.Marginal) assert len(margs) == 2 for key, value in margs.iteritems(): tot = value.sum() assert utilities.almost_equal(tot, 1.0, 0.0001)
def test_merge_definitions_6__events_not_unique(bayesian_network_0): merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) with pytest.raises(ValueError) as e: merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_0, allow_unspecified_events=True, assert_merge_definitions=True) assert str( e.value ) == DistributionEventMergeDefinitions.ERR_NETWORK_HAS_DUPLICATED_EVENT_NAMES