def aggregate(dataset, timeline, aggregator=None, **aggregator_param): # nothing to aggregate if dataset and/or timeline are/is empty if dataset.isEmpty() or timeline.isEmpty(): return PYODataset.Empty() # params will be passed to the aggregator function # it is meant to contain the user-provided parameters # and additional parameters (such as the current timerange) params = {} for p, param in enumerate(aggregator_param): params[param] = aggregator_param[param] nTimeranges = timeline.getNumberOfTimeranges() datas = [None] * nTimeranges number = np.zeros((nTimeranges, ), dtype=np.int32) for t, timerange in enumerate(timeline): subdataset = sub(dataset, timerange) # try and aggregate subdataset: # some aggregator may fail in case of empty dataset # some other may return something try: if aggregator == None: datas[t] = np.copy(subdataset.data) else: params['timerange'] = timerange datas[t] = aggregator(subdataset, **params) number[t] = datas[t].shape[0] except Exception, e: raise e datas[t] = None number[t] = 0
def sub(dataset, extent): timeline = pinocchIO.utils.timeline.sub(dataset.getTimeline(), extent) if timeline.isEmpty(): return PYODataset.Empty() trIDs = dataset.getTimeline().indexOfTimerangesInPeriod(extent, strict=False) # note: trIDs cannot be empty -- otherwise timeline would have been empty # and we would have already returned an empty dataset number = dataset.getNumber()[trIDs] data = np.concatenate([dataset[i] for i in trIDs]) return PYODataset.PYODataset(data, number, timeline)