def get_signals(self): bounds = [[], []] resources = set() log_signals = self.rawlog.get_signals() timerefs = set() for _, signal_name in self.inputs.items(): check_is_in('signal', signal_name, log_signals) s = log_signals[signal_name] b = s.get_time_bounds() bounds[0].append(b[0]) bounds[1].append(b[1]) timerefs.add(s.get_time_reference()) resources.update(s.get_resources()) bounds = min(bounds[0]), max(bounds[1]) resources = list(resources) timeref = '-'.join(sorted(timerefs)) signals = {} for _, signal_name in self.outputs.items(): warnings.warn('to finish') signal_type = '???' # XXX s = ProcgraphFilterSignal(signal_type, timeref, resources, bounds) signals[signal_name] = s return signals
def read_log(rawlog, signals=None, start=None, stop=None): log_signals = rawlog.get_signals() if not signals: signals = list(log_signals.keys()) else: for x in signals: check_is_in('signal', x, log_signals) # sync to first signal signal = log_signals[signals[0]] bounds = signal.get_time_bounds() if start is not None: start = bounds[0] + start else: start = bounds[0] if stop is not None: stop = bounds[0] + stop else: stop = bounds[1] print('start: %s' % start) print('stop: %s' % stop) for timestamp, (name, value) in rawlog.read(signals, start, stop): # @UnusedVariable if not (start <= timestamp <= stop + 0.001): msg = 'Messages not filtered (%.5f <= %.5f <= %.5f)' % (start, timestamp, stop) msg += '\nname: %s' % name raise Exception(msg) print('reading %.5f (%6.4f) %s' % (timestamp, timestamp - start, name))
def __init__(self, match_method): """ :param max_displ: Maximum pointwise displacement for the diffeomorphism d_max :param match_method: Either "continuous" or "binary" (to compute the error function). """ self.shape = None self.last_y0 = None self.last_y1 = None accepted = [MATCH_CONTINUOUS, MATCH_BINARY] check_is_in('match method', match_method, accepted) self.match_method = match_method self.num_samples = 0
def get_resource_job(self, context, rtype: str, **params): # print('RM %s %s get_resource %s %s' % (id(self), self._context, rtype, params)) key = dict(rtype=rtype, **params) already_done = key in self.allresources if already_done: return self.allresources[key] check_is_in('resource type', rtype, self.providers) prefix = self._make_prefix(rtype, **params) # print('adding job prefix %r' % prefix) c = context.child(name=rtype, add_job_prefix=prefix, add_outdir=rtype) c._job_prefix = prefix # Add this point we should check if we already created the job ok = [] errors = [] for provider in self.providers[rtype]: try: res_i = provider(c, **params) ok.append((provider, res_i)) except ResourceManager.CannotProvide as e: errors.append(e) except Exception as e: msg = 'Error while trying to get resource.\n' msg += ' type: %r params: %s\n' % (rtype, params) msg += 'While calling provider %r:\n' % provider msg += indent(traceback.format_exc(), '> ') raise Exception(msg) if not ok: msg = 'No provider could create this resource:\n' msg += "\n".join('- %s' % str(e) for e in errors) raise Exception(msg) if len(ok) >= 2: msg = 'The same resource could be created by two providers.' msg += '\n%s %s' % (rtype, params) for prov, _ in ok: msg += '\n - %s' % prov raise Exception(msg) assert len(ok) == 1 res = ok[0][1] self.set_resource(res, rtype, **params) return res
def get_resource_job(self, context, rtype, **params): # print('RM %s %s get_resource %s %s' % (id(self), self._context, rtype, params)) key = dict(rtype=rtype, **params) already_done = key in self.allresources if already_done: return self.allresources[key] check_is_in('resource type', rtype, self.providers) prefix = self._make_prefix(rtype, **params) # print('adding job prefix %r' % prefix) c = context.child(name=rtype, add_job_prefix=prefix, add_outdir=rtype) c._job_prefix = prefix # Add this point we should check if we already created the job ok = [] errors = [] for provider in self.providers[rtype]: try: res_i = provider(c, **params) ok.append((provider, res_i)) except ResourceManager.CannotProvide as e: errors.append(e) except Exception as e: msg = 'Error while trying to get resource.\n' msg += ' type: %r params: %s\n' % (rtype, params) msg += 'While calling provider %r:\n' % provider msg += indent(traceback.format_exc(), '> ') raise Exception(msg) if not ok: msg = 'No provider could create this resource:\n' msg += "\n".join('- %s' % str(e) for e in errors) raise Exception(msg) if len(ok) >= 2: msg = 'The same resource could be created by two providers.' msg += '\n%s %s' % (rtype, params) for prov, _ in ok: msg += '\n - %s' % prov raise Exception(msg) assert len(ok) == 1 res = ok[0][1] self.set_resource(res, rtype, **params) return res
def __init__(self, shape, neighborarea, centers=None, topology='plane'): ''' :param shape: :param neighborarea: :param centers: if not None, area around centers Suppose shape = (H, W) nighborarea = (X, Y) A = X * Y N = H * W Then self.neighbor_indices_flat is a K x N array. ''' neighborarea = np.array(neighborarea) if not np.all(neighborarea <= shape): neighborarea = np.minimum(neighborarea, shape) assert neighborarea[0] <= shape[0] assert neighborarea[1] <= shape[1] # for each sensel, create an area cmg = cmap(np.array(neighborarea), max_shape=shape) # if the area size is even, it is bumbed to the next integer assert cmg.shape[0] >= neighborarea[0] assert cmg.shape[1] >= neighborarea[1] assert cmg.shape[0] <= neighborarea[0] + 1 assert cmg.shape[1] <= neighborarea[1] + 1 # but we cannot be bigger than the area anyway assert cmg.shape[0] <= shape[0] assert cmg.shape[1] <= shape[1] self.area_shape = cmg.shape[0], cmg.shape[1] self.shape = shape self.H, self.W = shape self.X = self.area_shape[0] self.Y = self.area_shape[1] self.N = self.H * self.W self.A = self.X * self.Y self.topology = topology check_is_in(topology, self.topology, ['torus', 'plane']) # this is the important state self.neighbor_indices_flat = np.zeros((self.N, self.A), 'int32') # logger.info('Creating Flattening..') self.flattening = Flattening.by_rows(tuple(shape)) # logger.info('..done') if centers is None: self.centers = diffeo_identity(shape) else: if centers.dtype == 'float': # continuous diffeo centers = np.round(centers).astype('int') self.centers = centers for coord in coords_iterate(shape): k = self.flattening.cell2index[coord] cm = cmg.copy() center = self.centers[coord] cm[:, :, 0] += center[0] cm[:, :, 1] += center[1] # torus topology here if self.topology == 'torus': cm[:, :, 0] = cm[:, :, 0] % shape[0] cm[:, :, 1] = cm[:, :, 1] % shape[1] elif self.topology == 'plane': for i in range(2): cmin = cm[:, :, i].min() if cmin < 0: cm[:, :, i] -= cmin assert cm[:, :, i].min() >= 0 cmax = cm[:, :, i].max() if cmax >= shape[i]: delta = -(cmax - (shape[i] - 1)) cm[:, :, i] += delta assert cm[:, :, i].max() < shape[i] assert cm[:, :, i].min() >= 0 else: assert False for i in range(2): cmin, cmax = cm[:, :, i].min(), cm[:, :, i].max() assert cmin >= 0 assert cmax < shape[i] indices = np.zeros(self.area_shape, 'int32') for a, b in coords_iterate(self.area_shape): c = tuple(cm[a, b, :]) indices[a, b] = self.flattening.cell2index[c] # XXX using numpy's flattening indices = np.array(indices.flat) # warnings.warn('remove bias due to ordering') # indices = np.random.permutation(indices) self.neighbor_indices_flat[k, :] = indices
def __init__(self, score, **kwargs): DiffeoActionEstimatorSimple.__init__(self, **kwargs) self.score = score check_is_in('score function', score, DiffeoActionEstimatorNewUnc.scores)
def __init__(self, shape, neighborarea, centers=None, topology="plane"): """ :param shape: :param neighborarea: :param centers: if not None, area around centers Suppose shape = (H, W) nighborarea = (X, Y) A = X * Y N = H * W Then self.neighbor_indices_flat is a K x N array. """ neighborarea = np.array(neighborarea) if not np.all(neighborarea <= shape): neighborarea = np.minimum(neighborarea, shape) assert neighborarea[0] <= shape[0] assert neighborarea[1] <= shape[1] # for each sensel, create an area cmg = cmap(np.array(neighborarea), max_shape=shape) # if the area size is even, it is bumbed to the next integer assert cmg.shape[0] >= neighborarea[0] assert cmg.shape[1] >= neighborarea[1] assert cmg.shape[0] <= neighborarea[0] + 1 assert cmg.shape[1] <= neighborarea[1] + 1 # but we cannot be bigger than the area anyway assert cmg.shape[0] <= shape[0] assert cmg.shape[1] <= shape[1] self.area_shape = cmg.shape[0], cmg.shape[1] self.shape = shape self.H, self.W = shape self.X = self.area_shape[0] self.Y = self.area_shape[1] self.N = self.H * self.W self.A = self.X * self.Y self.topology = topology check_is_in(topology, self.topology, ["torus", "plane"]) # this is the important state self.neighbor_indices_flat = np.zeros((self.N, self.A), "int32") # logger.info('Creating Flattening..') self.flattening = Flattening.by_rows(tuple(shape)) # logger.info('..done') if centers is None: self.centers = diffeo_identity(shape) else: if centers.dtype == "float": # continuous diffeo centers = np.round(centers).astype("int") self.centers = centers for coord in coords_iterate(shape): k = self.flattening.cell2index[coord] cm = cmg.copy() center = self.centers[coord] cm[:, :, 0] += center[0] cm[:, :, 1] += center[1] # torus topology here if self.topology == "torus": cm[:, :, 0] = cm[:, :, 0] % shape[0] cm[:, :, 1] = cm[:, :, 1] % shape[1] elif self.topology == "plane": for i in range(2): cmin = cm[:, :, i].min() if cmin < 0: cm[:, :, i] -= cmin assert cm[:, :, i].min() >= 0 cmax = cm[:, :, i].max() if cmax >= shape[i]: delta = -(cmax - (shape[i] - 1)) cm[:, :, i] += delta assert cm[:, :, i].max() < shape[i] assert cm[:, :, i].min() >= 0 else: assert False for i in range(2): cmin, cmax = cm[:, :, i].min(), cm[:, :, i].max() assert cmin >= 0 assert cmax < shape[i] indices = np.zeros(self.area_shape, "int32") for a, b in coords_iterate(self.area_shape): c = tuple(cm[a, b, :]) indices[a, b] = self.flattening.cell2index[c] # XXX using numpy's flattening indices = np.array(indices.flat) # warnings.warn('remove bias due to ordering') # indices = np.random.permutation(indices) self.neighbor_indices_flat[k, :] = indices
def __init__(self, score, **kwargs): DiffeoActionEstimatorSimple.__init__(self, **kwargs) self.score = score check_is_in("score function", score, DiffeoActionEstimatorNewUnc.scores)