def valids(): # the returned fixture value will be shared for # all tests needing it t = gia((3, 4)) t.flat[0] = fl.getNAN(t.dtype) t.flat[-1] = fl.getNAN(t.dtype) space = Bld._buildInfoSpace(t, 5) exp = { 1: [t[:, 1:2].squeeze(), t[1:2, :].squeeze()], 0: [t[1:2, 1:2].squeeze(axis=0)], 2: [t[1:, :-1], t[:-1, 1:], t[:, 1:-1]] } return [ (space, exp), ]
def getctx(ispace, val): icontext = ispace[val] try: INTNAN = getNAN(icontext.context[0].data.dtype) except IndexError: return icontext icontext = [ x if x.data[0] != INTNAN else InformationContext.create( data=x.data[::-1], **x.info) for x in icontext.context ] return InformationContext(icontext)
def feed(self, seq, restriction): seq = _check_input(seq, BaseSequence) nan = getNAN(seq.data.dtype) fillvalue = np.max(seq.data) + 1 arr = np.ones(shape=seq.shape, dtype=seq.dtype) * nan # self.reset() for i, searchidx in enumerate(seq.sequence): truth = seq.data[i] arr.flat[searchidx] = fillvalue yield truth, self.builder.build_infospace(arr, searchval=fillvalue, restriction=restriction) arr.flat[searchidx] = truth
def build_infospace(array, searchval=None, searchidx=None, restriction=None): intnan = fl.getNAN(array.dtype) if searchval: origin = np.argwhere(array == searchval)[0] elif searchidx: origin = np.unravel_index(searchidx, array.shape) else: raise Exception("No value or index given.") sval = array[tuple(origin)] if restriction is not None: array = _getSubssetInRangeOf(array, restriction, sval) spacedict = _buildInfoSpace(arr=array, val=sval) space = _set_searchval_to_intnan(spacedict, sval, intnan) return space
def _get_nan_position(data): """Find index position of np.nan value. Note ==== Now supports all kind of dtypes. """ if data.dtype in (np.float32, np.float64): nans = tuple(np.transpose(np.isnan(data).nonzero())) else: nanval = getNAN(data.dtype) nans = tuple(np.transpose((data == nanval).nonzero())) if not nans: raise Exception("Non np.nan value found.") if len(nans) > 1: message = "Only one np.nan value allowed, found {}." raise Exception(message.format(len(nans))) return nans[0]
def setrandomNANs(arr, size=None, seed=None): """Set random NAN values to IndexArray.""" INTNAN = getNAN(arr.dtype) def _randomNAN(mi, ma, size): return np.random.randint(mi, ma, size) if seed: np.random.seed(seed) mi = 1 ma = arr.size if not size: size = _randomNAN(mi, ma, 1) result = arr.copy() for i in _randomNAN(mi, ma, size): result.flat[i] = INTNAN return result
def _set_context(self, value): if not all([isinstance(x, CTX) for x in value]): err_msg = "All values must be of type {}, got {}".format( CTX, [type(x) for x in value]) raise TypeError(err_msg) if not all([isinstance(x.data, np.ndarray) for x in value]): err_msg = "All values must be of type np.ndarray, got {}.".format( [type(x) for x in value]) raise TypeError(err_msg) if not all([x.data.ndim == value[0].data.ndim for x in value]): err_msg = "All arrays must have the same dimension." raise ValueError(err_msg) for i, arr in enumerate(value): if arr.data.size <= 1: del value[i] elif np.sum([y == getNAN(arr.data.dtype) for y in arr.data]) != 1: err_msg = "All arrays must have exactly one(!) NaN value,"\ " got in \n{}.".format(arr.data) raise ValueError(err_msg) self._context = value
def build_infospace(array, searchval=None, searchidx=None, restriction=None): intnan = fl.getNAN(array.dtype) if searchval is not None: origin = np.argwhere(array == searchval)[0] elif searchidx is not None: origin = np.unravel_index(searchidx, array.shape) else: raise Exception("No value or index given, got {} {}".format( searchval, searchidx)) sval = array[origin] # Candidates are slices going through searchval candidates = [] for i in range(len(origin)): tmp = list(origin) tmp[i] = slice(None, None, None) candidates.append(tmp) # Result is 1d array going forward (+1) # and backward (-1) in each dimension result = [] for i, x in enumerate(candidates): source = origin[i] assert array[x][ source] == sval, "Error in indexing: {} != {}".format( array[x][source], sval) tmp = array[x].copy() tmp.data[source] = intnan if restriction is not None: tmp = tmp[slice(max(source - restriction, 0), min(source + restriction, array[x].size) + 1)] if tmp.data[tmp.mask == False].size > 1: data = tmp.data[tmp.mask == False] result.append(IC.create(data=data, id=(i, ), size=data.size)) ispace = InformationSpace({1: IC(result)}) return ispace
def __init__(self, vht, *args, **kwargs): super().__init__(vht, vpt=1, *args, **kwargs) self.vht = self.vht.astype(np.int32) self.weights = np.insert(_get_pascal_weights(vht), [0], [0] * vht, axis=0) self.nan = getNAN(self.vht.dtype)
def nan(self): return getNAN(self.dtype)