예제 #1
0
파일: model.py 프로젝트: timmc/kpawebgen
 def tag_closure(tagdag):
   """Return data for the transitive closure of tag relationships.
   
   The output is not in the same format as the input."""
   result = {} # don't alter the original
   bycat = util.group(tagdag, 'catID')
   for catID, rels in bycat.iteritems(): #TODO(Py3k) use dict comprehension
     result[catID] = KPA._close_one_cat(rels)
   return result
예제 #2
0
    def __iter__(self):
        data_source = self.data_source
        grouping = self.grouping
        indices = list(range(len(data_source)))
        if self.shuffle:
            random.shuffle(indices)

        group = {i: grouping(data_source[i]) for i in indices}
        indices_pos = [i for i in indices if group[i]]
        indices_neg = [i for i in indices if not group[i]]

        pos_batch_size = self.pos_batch_size
        neg_batch_size = self.neg_batch_size

        batch_pos = util.group(pos_batch_size, indices_pos)
        batch_neg = util.group(neg_batch_size, indices_neg)

        for x, y in zip(batch_pos, batch_neg):
            yield from x
            yield from y
예제 #3
0
 def test_group(self):
   """Grouping a list of dicts by one key's value."""
   # empty
   self.assert_eq_rec({}, util.group([], "hello"))
   
   #bad key
   self.expect_error(lambda:util.group([{}], "imaginary"), KeyError)
   
   #normal
   start = [
     {0:0,1:0},{0:0,1:5},{0:0,1:2},{0:4,1:2},{0:3,1:2},{0:8,1:9},
     {0:3,1:3},{0:0,1:7},{0:4,1:6},{0:4,1:7},{0:0,1:8},{0:8,1:9}
   ]
   expected = {
     0:[{0:0,1:0},{0:0,1:5},{0:0,1:2},{0:0,1:7},{0:0,1:8}],
     4:[{0:4,1:2},{0:4,1:6},{0:4,1:7}],
     3:[{0:3,1:2},{0:3,1:3}],
     8:[{0:8,1:9},{0:8,1:9}]
   }
   actual = util.group(start, 0)
   self.assert_eq_rec(expected, actual)