Exemplo n.º 1
0
def _average_flowgrams(mapping, flowgrams, sample_keys):
    """average flowgrams according to cluster mapping.

    mapping: a dictionary of lists as cluster mapping

    flowgrams:  an iterable flowgram source, all flowgram ids from this source must be in the mapping

    sample_keys: the keys that should be averaged over for each cluster.
    """

    # accumulates flowgram for each key until sample for this key is empty
    flows = defaultdict(list)
    invert_map = invert_mapping(mapping)
    for f in flowgrams:
        key = invert_map[f.Name]
        samples = sample_keys[key]
        if (f.Name in samples):
            flows[key].append(f.flowgram)
            samples.remove(f.Name)
            if (len(samples) == 0):
                # we gathered all sampled flowgrams for this cluster,
                # now average
                ave_flowgram = build_averaged_flowgram(flows[key])
                ave_f = Flowgram(ave_flowgram, Name=key)

                del(flows[key])
                yield ave_f, key
Exemplo n.º 2
0
def _average_flowgrams(mapping, flowgrams, sample_keys):
    """average flowgrams according to cluster mapping.

    mapping: a dictionary of lists as cluster mapping

    flowgrams:  an iterable flowgram source, all flowgram ids from this source must be in the mapping

    sample_keys: the keys that should be averaged over for each cluster.
    """

    # accumulates flowgram for each key until sample for this key is empty
    flows = defaultdict(list)
    invert_map = invert_mapping(mapping)
    for f in flowgrams:
        key = invert_map[f.Name]
        samples = sample_keys[key]
        if (f.Name in samples):
            flows[key].append(f.flowgram)
            samples.remove(f.Name)
            if (len(samples) == 0):
                # we gathered all sampled flowgrams for this cluster,
                # now average
                ave_flowgram = build_averaged_flowgram(flows[key])
                ave_f = Flowgram(ave_flowgram, Name=key)

                del (flows[key])
                yield ave_f, key
Exemplo n.º 3
0
    def test_invert_mapping(self):
        """invert_prefix_map inverts a dictionary mapping."""

        actual = invert_mapping(self.test_map)
        self.assertEqual(
            {'1': '1',
             'a': '1',
             'b': '1',
             'c': '1',
             '2': '2',
             'd': '2',
             'e': '2',
             'f': '2'},
            actual)
Exemplo n.º 4
0
    def test_invert_mapping(self):
        """invert_prefix_map inverts a dictionary mapping."""

        actual = invert_mapping(self.test_map)
        self.assertEqual(
            {'1': '1',
             'a': '1',
             'b': '1',
             'c': '1',
             '2': '2',
             'd': '2',
             'e': '2',
             'f': '2'},
            actual)
Exemplo n.º 5
0
    def test_invert_mapping(self):
        """invert_prefix_map inverts a dictionary mapping."""

        actual = invert_mapping(self.test_map)
        self.assertEqual({"1": "1", "a": "1", "b": "1", "c": "1", "2": "2", "d": "2", "e": "2", "f": "2"}, actual)