Example #1
0
def test_load_file_simple():

    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/DiabetesEQW_3_Class1_with_mapping.csv')
Example #2
0
def runKarmaLego(time_intervals_path,
                 min_ver_support,
                 num_relations,
                 max_gap,
                 label,
                 max_tirp_length=5,
                 num_comma=2,
                 num_of_bins=3,
                 symbol_type='int'):
    """
    this method runs  the process of KarmaLego with all relevant inputs
    :param time_intervals_path: String, the time intervals file
    :param class_b_path: String, the time intervals file for class
    :param min_ver_support: float, the minimum vertical support value
    :param num_relations: int, number of relations
    :param max_gap: int, the max_gap between the intervals for creating the index
    :return:
    """
    karma = Karma(min_ver_support,
                  num_relations=num_relations,
                  max_gap=max_gap,
                  label=label)
    karma.fit(time_intervals_path, num_comma, num_of_bins, symbol_type)
    lego = Lego(karma, max_tirp_length)
    lego.fit()
    return lego
Example #3
0
def test_load_file():

    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/DiabetesEQW_3_Class1_with_mapping.csv',
              True)
    logger.info('[PASSED] karma build for skipped followers ')
Example #4
0
def test_get_relations_for_two_symbols():
    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/single_entity_2_bins.csv')

    relations = karma.get_relations_for_two_symbols(1, 2)

    if len(relations) == 1 and relations[0] == AllenSevenRelationEngine.BEFORE:
        logger.info('[PASSED] relations fetched ' + str(relations))
    else:
        logger.info('[FAILED] relations not fetched' + str(relations))
Example #5
0
def test_is_pair_exist():
    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/single_entity_2_bins.csv')
    sti_a = SymbolicTimeInterval(0, 2, 1)
    sti_b = SymbolicTimeInterval(4, 6, 2)
    if karma.is_pair_exist(0, 1, sti_a, sti_b):
        logger.info('[PASSED] pair exist ' + str(sti_a) + ' ' + str(sti_b))
    else:
        logger.info('[FAILED] pair dont exist ' + str(sti_a) + ' ' +
                    str(sti_b))
Example #6
0
def test_get_pairs():
    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/single_entity_2_bins.csv')
    sti = SymbolicTimeInterval(0, 2, 1)
    pairs = karma.get_pairs(1, 2, 0, 1, sti)

    if 2 == len(pairs):
        logger.info('[PASSED] pairs fetched ' + str(pairs))
    else:
        logger.info('[FAILED] pairs not fetched - ' + str(pairs))
Example #7
0
def run_KL(input_path, output_folder, epsilon, max_gap, vertical_support):
    output_path = output_folder
    suportVec = float(vertical_support)
    num_relations = 7
    num_symbols = count_symbols(input_path, output_path)
    max_gap = float(max_gap)
    epsilon = float(epsilon)
    karma = Karma(min_ver_support=suportVec,
                  num_relations=num_relations,
                  max_gap=max_gap,
                  epsilon=epsilon)
    karma.fit(input_path, num_comma=NUM_COMMA, num_of_bins=num_symbols)
    lego = Lego(karma)
    lego.fit()
    lego.print_frequent_tirps(output_path)
Example #8
0
def test_test_lego_fit():

    suportVec = 0.5
    num_relations = 7
    max_gap = 15
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec, num_relations=num_relations, max_gap=max_gap)
    karma.fit('../KarmaLego_TestsData/DiabetesEQW_3_Class0_short.csv')
    lego = Lego(karma)
    lego.fit()
    lego.print_frequent_tirps(
        '../KarmaLego_TestsData/DiabetesEQW_3_Class0_50_15_output_sequence.csv'
    )
    depth = lego.tirps_tree_root.get_max_depth()
    leafs = lego.tirps_tree_root.get_all_leafs()
    logger.info('[PASSED] Lego fit completed, tree depth is: ' + str(depth) +
                ' with ' + str(len(leafs)) + " leafs")
Example #9
0
def test_get_all_pairs():
    suportVec = 0.5
    logger.debug('loading file for karma with support vec value: ' +
                 str(suportVec))
    karma = Karma(suportVec)
    karma.fit('../KarmaLego_TestsData/single_entity_2_bins.csv')
    karma.get_all_pairs_mapped_by_entity_id(1, 2, 0)
def runKarmaLegoCD(class_a_path,class_b_path,min_ver_support,num_relations,max_gap,min_vs_gap, alpha, weights,num_comma):
    """
    this method runs all the process of KarmaLegoCD with all relevant inputs
    :param class_a_path: String, the time intervals file for class A
    :param class_b_path: String, the time intervals file for class B
    :param min_ver_support: float, the minimum vertical support value
    :param num_relations: int, number of relations
    :param max_gap: int, the max_gap between the intervals for creating the index
    :param min_vs_gap: float, the minimum vertical support threshold between the classes
    :param alpha: float, the minimum alpha for the statistical tests
    :param weights: list[float] the weight for the scoring function
    :return: newLego,  returns the NewLego object
    """
    karma_a = Karma(min_ver_support, num_relations=num_relations, max_gap=max_gap,label=0)
    karma_b = Karma(min_ver_support, num_relations=num_relations, max_gap=max_gap,label=1)
    karma_a.fit(class_a_path,num_comma=num_comma,min_vertical_support_for_trim=0.3)
    karma_b.fit(class_b_path,num_comma=num_comma,min_vertical_support_for_trim=0.3)
    newLego=NewLego(karma_a=karma_a,karma_b=karma_b,min_vs_gap=min_vs_gap,alpha=alpha,weights=weights)
    newLego.fit()
    return newLego
Example #11
0
def test_lego_generate_candidates():
    """

    the tested relations are the 7 allens as defined in RelationHandler

                | B | C | D |
              --|---|---|---|
              A | 2 |  2|  ?|
              --|---|---|---|
              B |   |  2|  ?|
              --|---|---|---|
              C |   |   |  2|

    should generate 5 matching candidates

    [[0,0,2],[0,1,2],[0,2,2],[1,2,2],[2,2,2]]

    for future tirps as:

                | B | C | D |     | B | C | D |    | B | C | D |     | B | C | D |    | B | C | D |
              --|---|---|---|   --|---|---|---|  --|---|---|---|   --|---|---|---|  --|---|---|---|
              A | 2 |  2|  0|   A | 2 |  2|  0|  A | 2 |  2|  0|   A | 2 |  2|  1|  A | 2 |  2|  2|
              --|---|---|---|   --|---|---|---|  --|---|---|---|   --|---|---|---|  --|---|---|---|
              B |   |  2|  0|   B |   |  2|  1|  B |   |  2|  2|   B |   |  2|  2|  B |   |  2|  2|
              --|---|---|---|   --|---|---|---|  --|---|---|---|   --|---|---|---|  --|---|---|---|
              C |   |   |  2|   C |   |   |  2|  C |   |   |  2|   C |   |   |  2|  C |   |   |  2|

              .....

    recursion tree should be:

     0   0  0  1  2
     |   |  \  |  /
     |   |   \ | /
     |   |    \|/
     0   1    2
     \   |   /
      \  |  /
       \ | /
        \|/
         2

    """
    expected = [[0, 0, 2], [0, 1, 2], [0, 2, 2], [1, 2, 2], [2, 2, 2]]
    result = True
    reason = 'as expected'

    tirp = TIRP('A', 'B', 2)
    tirp._symbols.append('C')
    tirp._tirp_matrix.extend([2, 2])

    lego = Lego(Karma(1))

    # expending with overlap (2) when others are overlap too
    candidates = lego.generate_candidats(tirp, 2)

    if len(candidates) != len(expected):
        result = False
        reason = 'candidates amount not match'
    elif candidates != expected:
        result = False
        reason = 'candidates not as expected'

    if result:
        logger.info('[PASSED] Lego candidates generation success: ' + reason)
    else:
        logger.error('[FAILED] Lego candidates generation failed, reason: ' +
                     reason)