Example #1
0
 def test_state_loading(self):
     a = GeneralizedPairState()
     a.load(self.inputY)
     X = a.toJSON()
     Y = self.inputY
     X['emission'].sort()
     Y['emission'].sort()
     X['durations'].sort()
     Y['durations'].sort()
     self.assertDictEqual(X, Y, "Loading and dumping to JSON does not " + \
                          " work: \n" + str(X) + " != \n" + str(Y))
Example #2
0
 def __init__(self, *args, **_):
     GeneralizedPairState.__init__(self, *args)
     self.dp = config.preparers[config.preparer_index][0](constants.window_size)
     self.clf_fname = 'data/clf/{}{}{}.clf'.format(
         PairClassifier.get_name(),
         config.preparers[config.preparer_index][2],
         constants.window_size,
     )
     self.clf = self._get_classifier()
     self.annotations, self.ann_x, self.ann_y = None, None, None
     self.emission_table = None
Example #3
0
 def test_state_loading(self):
     a = GeneralizedPairState()
     a.load(self.inputY)
     X = a.toJSON()
     Y = self.inputY
     X['emission'].sort()
     Y['emission'].sort()
     X['durations'].sort()
     Y['durations'].sort()
     self.assertDictEqual(X, Y, "Loading and dumping to JSON does not " + \
                          " work: \n" + str(X) + " != \n" + str(Y))
Example #4
0
 def setUp(self):
     self.inputY = {
         "__name__": "GeneralizedPairState",
         "name": "name",
         "startprob": 1.0,
         "emission": [(("0", "0"), 1.0), (("00", ""), 1.0)],
         "endprob": 0.5,
         "durations": [((1, 1), 0.5), ((2, 0), 0.5)]
     } 
     self.HMM = dict()
     
     for mathType in self.mathTypes:
          
         hmmInit = {
             "__name__": "GeneralizedPairHMM",
             "states": [],
             "transitions": [
                 {"from": "one", "to": "one", "prob": mathType(0.3)},
                 {"from": "one", "to": "two", "prob": mathType(0.6)},
                 {"from": "one", "to": "three", "prob": mathType(0.1)},
                 {"from": "two", "to": "one", "prob": mathType(0.5)},
                 {"from": "two", "to": "two", "prob": mathType(0.4)},
                 {"from": "two", "to": "three", "prob": mathType(0.1)},
                 {"from": "three", "to": "one", "prob": mathType(0.2)},
                 {"from": "three", "to": "two", "prob": mathType(0.3)},
                 {"from": "three", "to": "three", "prob": mathType(0.5)},
             ]
         }
         one = GeneralizedPairState(mathType)
         one.load({
             "__name__": "GeneralizedPairState",
             "name": "one",
             "startprob": 0.2,
             "endprob": 1.0,
             "emission": [(("0", "0"), 1.0), (("00", "00"), 1.0)],
             "durations": [((1, 1), 0.5), ((2, 2), 0.5)]
         })
         two = GeneralizedPairState(mathType)
         two.load({
             "__name__": "GeneralizedPairState",
             "name": "two",
             "startprob": 0.8,
             "endprob": 1.0,
             "emission": [(("0", ""), 1.0), (("00", "0"), 1.0)],
             "durations": [((1, 0), 0.5), ((2, 1), 0.5)]
         })
         three = GeneralizedPairState(mathType)
         three.load({
             "__name__": "GeneralizedPairState",
             "name": "three",
             "startprob": 0.8,
             "endprob": 1.0,
             "emission": [(("", "0"), 1.0), (("0", "00"), 1.0)],
             "durations": [((0, 1), 0.5), ((1, 2), 0.5)]
         }) 
         hmmInit["states"] = [one, two, three]
         self.HMM[mathType] = GeneralizedPairHMM(mathType)
         self.HMM[mathType].load(hmmInit)
Example #5
0
 def __init__(self, *args, **_):
     GeneralizedPairState.__init__(self, *args)
     self.dp = config.preparers[config.preparer_index][0](
         constants.window_size)
     self.clf_fname = 'data/clf/{}{}{}.clf'.format(
         PairClassifier.get_name(),
         config.preparers[config.preparer_index][2],
         constants.window_size,
     )
     self.clf = self._get_classifier()
     self.annotations, self.ann_x, self.ann_y = None, None, None
     self.emission_table = None
Example #6
0
 def test_state(self):
     for numType in self.mathTypes:
         state = GeneralizedPairState(numType)
         state.load(self.inputY)
         #test duration
         X = list(state.durationGenerator())
         Y = [((1, 1), numType(0.5)), ((2, 0), numType(0.5))]
         self.assertEqual(X, Y, "HMM.durationGenerator() does not work: " + \
                          str(X) + " != " + str(Y))
         #test emission
         Y = numType(1.0)
         X = state.emission("000", 1, 1, "000", 2, 1)
         self.assertAlmostEqual(X, Y, delta=1e-7,
                                msg="HMM.emission(\"000\", 1, 1) does not " \
                                + "work: " + str(X) + " != " + str(Y))
         Y = numType(1.0)
         X = state.emission("000", 1, 2, "000000", 2, 0)
         self.assertAlmostEqual(X, Y, delta=1e-7,
                                msg="HMM.emission(\"000\", 1, 2) does not " \
                                + "work: " + str(X) + " != " + str(Y))
Example #7
0
 def test_state(self):
     for numType in self.mathTypes:
         state = GeneralizedPairState(numType)
         state.load(self.inputY)
         #test duration
         X = list(state.durationGenerator())
         Y = [((1, 1), numType(0.5)), ((2, 0), numType(0.5))]
         self.assertEqual(X, Y, "HMM.durationGenerator() does not work: " + \
                          str(X) + " != " + str(Y))
         #test emission
         Y = numType(1.0)
         X = state.emission("000", 1, 1, "000", 2, 1)
         self.assertAlmostEqual(X, Y, delta=1e-7, 
                                msg="HMM.emission(\"000\", 1, 1) does not " \
                                + "work: " + str(X) + " != " + str(Y))
         Y = numType(1.0)
         X = state.emission("000", 1, 2, "000000", 2, 0)
         self.assertAlmostEqual(X, Y, delta=1e-7, 
                                msg="HMM.emission(\"000\", 1, 2) does not " \
                                + "work: " + str(X) + " != " + str(Y))
Example #8
0
    def setUp(self):
        self.inputY = {
            "__name__": "GeneralizedPairState",
            "name": "name",
            "startprob": 1.0,
            "emission": [(("0", "0"), 1.0), (("00", ""), 1.0)],
            "endprob": 0.5,
            "durations": [((1, 1), 0.5), ((2, 0), 0.5)]
        }
        self.HMM = dict()

        for mathType in self.mathTypes:

            hmmInit = {
                "__name__":
                "GeneralizedPairHMM",
                "states": [],
                "transitions": [
                    {
                        "from": "one",
                        "to": "one",
                        "prob": mathType(0.3)
                    },
                    {
                        "from": "one",
                        "to": "two",
                        "prob": mathType(0.6)
                    },
                    {
                        "from": "one",
                        "to": "three",
                        "prob": mathType(0.1)
                    },
                    {
                        "from": "two",
                        "to": "one",
                        "prob": mathType(0.5)
                    },
                    {
                        "from": "two",
                        "to": "two",
                        "prob": mathType(0.4)
                    },
                    {
                        "from": "two",
                        "to": "three",
                        "prob": mathType(0.1)
                    },
                    {
                        "from": "three",
                        "to": "one",
                        "prob": mathType(0.2)
                    },
                    {
                        "from": "three",
                        "to": "two",
                        "prob": mathType(0.3)
                    },
                    {
                        "from": "three",
                        "to": "three",
                        "prob": mathType(0.5)
                    },
                ]
            }
            one = GeneralizedPairState(mathType)
            one.load({
                "__name__": "GeneralizedPairState",
                "name": "one",
                "startprob": 0.2,
                "endprob": 1.0,
                "emission": [(("0", "0"), 1.0), (("00", "00"), 1.0)],
                "durations": [((1, 1), 0.5), ((2, 2), 0.5)]
            })
            two = GeneralizedPairState(mathType)
            two.load({
                "__name__": "GeneralizedPairState",
                "name": "two",
                "startprob": 0.8,
                "endprob": 1.0,
                "emission": [(("0", ""), 1.0), (("00", "0"), 1.0)],
                "durations": [((1, 0), 0.5), ((2, 1), 0.5)]
            })
            three = GeneralizedPairState(mathType)
            three.load({
                "__name__": "GeneralizedPairState",
                "name": "three",
                "startprob": 0.8,
                "endprob": 1.0,
                "emission": [(("", "0"), 1.0), (("0", "00"), 1.0)],
                "durations": [((0, 1), 0.5), ((1, 2), 0.5)]
            })
            hmmInit["states"] = [one, two, three]
            self.HMM[mathType] = GeneralizedPairHMM(mathType)
            self.HMM[mathType].load(hmmInit)
Example #9
0
 def expand(self, params=None):
     if params == None:
         raise "Houston, we have a problem"
     consensuses = params['consensus']
     prefix_t = self.stateName + '_{}_'
     prefix_i = 0
     states = list()
     transitions = list()
     init = self.stateName + '_Init'
     end = self.stateName + '_End'
     total = 0.0
     for consensus in consensuses:
         prefix = prefix_t.format(prefix_i)
         prefix_i += 1
         model = self.factory.getHMM(consensus)
         probability = self.consensusDistribution[consensus]
         total += probability
         json = model.toJSON()
         new_init = prefix + 'Init'
         new_end = prefix + 'End'
         st = map(deepcopy, model.states)
         for i in range(len(st)):
             st[i].stateName = prefix + st[i].stateName
         tr = map(
             lambda x: {
                 'from': prefix + x['from'],
                 'to': prefix + x['to'],
                 'prob': x['prob'],
             },
             json['transitions'],
         )
         st, tr, new_init, new_end = double_track_hmm(
             st, tr, new_init, new_end, self.mathType
         )
         transitions.extend(tr)
         states.extend(st)
         transitions.extend([
             {
                 'from': init,
                 'to': new_init,
                 'prob': probability,
             },
             {
                 'from': new_end,
                 'to': end,
                 'prob': 1.0,
             },
         ])
     #transitions.append({
     #    'from': init,
     #    'to': init,
     #    'prob': 1.0 - total,
     #})
     template = {
         '__name__': 'GeneralizedPairState',
         'name': init,
         'startprob': 1.0,
         'endprob': 0.0,
         'emission': [(('', ''), self.mathType(1.0))],
         'durations': [((0, 0), self.mathType(1.0))],
     }
     st = GeneralizedPairState(self.mathType)
     st.load(template)
     states.append(st)
     template['name'] = end
     template['startprob'] = 0.0
     template['endprob'] = 1.0
     st = GeneralizedPairState(self.mathType)
     st.load(template)
     states.append(st)
     return states, transitions, init, end