def testwrite(self): y, mapobj, _ = maparray('tmp.foo', 'd') y[1] = 37 mapobj.flush() z, _, _ = maparray('tmp.foo', 'd') self.failUnless( Numeric.alltrue( z == Numeric.array([0, 37, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]))) self.failUnless(Numeric.alltrue(z == y))
def testLogical (self): "Test logical_and, logical_or, sometrue, alltrue" x = Numeric.array([1,1,0,0]) y = Numeric.array([1,0,1,0]) assert_eq(Numeric.logical_and (x,y), [1,0,0,0]) assert_eq(Numeric.logical_or (x,y), [1,1,1,0]) assert Numeric.sometrue(x) assert not Numeric.sometrue(Numeric.zeros((3,))) assert Numeric.alltrue(Numeric.ones((3,))) assert not Numeric.alltrue(x)
def to_R(self, ds, dates_as_factor, suppress_all_value=True, **kwargs): if not self.colname: return col = ds[self.colname] if col.is_datetimetype(): rdata = rconv.Missing_Date_to_R(col) if not dates_as_factor: return rdata elif MA.isMaskedArray(col.data): if self.missing_as_none: rdata = MA.filled(col.data, sys.maxint) else: if Numeric.alltrue(col.data.mask()): raise PlotError('%r: all data-points masked' % col.name) rdata = rconv.MA_to_R(col.data) else: rdata = col.data if col.is_discrete(): if 0 and suppress_all_value: row_vecs = [ vec for value, vec in col.inverted.items() if value != col.all_value ] row_vec = union(*row_vecs) rdata = MA.take(rdata, row_vec) return self.discrete_col_to_R(col, rdata, suppress_all_value) else: return self.continuous_col_to_R(col, rdata)
def testnewlines(self): # If we forgot to open the file in binary mode, some bytes would # get mangled on some systems. x = Numeric.arange(258).astype('b') # bytes x[-2:, ] = (13, 10) # CRLF open('tmp.foo', 'wb').write(x.tostring()) y, _, _ = maparray('tmp.foo', 'b') self.failUnless(Numeric.alltrue(x == y))
def testimplicitflush(self): y, _, _ = maparray('tmp.foo', 'd') y[1] = 37 del y, _ # trigger implicit flush as refcounts go to 0 z, _, _ = maparray('tmp.foo', 'd') self.failUnless( Numeric.alltrue( z == Numeric.array([0, 37, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])))
def to_R(self, ds, dates_as_factor, **kwargs): col = ds[self.colname] if MA.isMaskedArray(col.data): if Numeric.alltrue(col.data.mask()): raise PlotError('%r: all data-points masked' % col.name) return rconv.MA_to_R(col.data) elif col.is_datetimetype(): return rconv.Missing_Date_to_R(col.data) else: return col.data
def testPickle (self): "Test pickling of Numeric arrays." import pickle x = Numeric.arange(10) fpik = open('foo.pik', 'wb') pickle.dump(x, fpik, 0) fpik.close() fpik = open('foo.pik', 'rb') y = pickle.load(open('foo.pik', 'rb')) fpik.close() assert_eq(y,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) assert Numeric.alltrue(Numeric.equal(x,y)) assert Numeric.sometrue(Numeric.equal(x,3)) assert y.shape == (10,) os.remove('foo.pik')
def to_frame(self, frame, ds, **kwargs): limit_cols = self.limit_cols(ds) if limit_cols: self.max = 0 self.min = 0 for colname in (self.colname, ) + limit_cols: col = ds[colname] if MA.isMaskedArray(col.data): if Numeric.alltrue(col.data.mask()): raise PlotError('%r: all data-points masked' % col.name) data = rconv.MA_to_R(col.data) self.max = max(MA.maximum(col.data), self.max) self.min = min(MA.minimum(col.data), self.min) else: data = col.data self.max = max(max(col.data), self.max) self.min = min(min(col.data), self.min) frame[rconv.rname(colname)] = data else: super(RMeasureCondCol, self).to_frame(frame, ds, **kwargs)
def unique(inarray): """Returns unique items in the FIRST dimension of the passed array. Only works on arrays NOT including string items (e.g., type 'O' or 'c'). """ inarray = asarray(inarray) uniques = N.array([inarray[0]]) if len(uniques.shape) == 1: # IF IT'S A 1D ARRAY for item in inarray[1:]: if N.add.reduce(N.equal(uniques, item).flat) == 0: try: uniques = N.concatenate([uniques, N.array[N.NewAxis, :]]) except TypeError: uniques = N.concatenate([uniques, N.array([item])]) else: # IT MUST BE A 2+D ARRAY if inarray.typecode() != "O": # not an Object array for item in inarray[1:]: if not N.sum(N.alltrue(N.equal(uniques, item), 1)): try: uniques = N.concatenate([uniques, item[N.NewAxis, :]]) except TypeError: # the item to add isn't a list uniques = N.concatenate([uniques, N.array([item])]) else: pass # this item is already in the uniques array else: # must be an Object array, alltrue/equal functions don't work for item in inarray[1:]: newflag = 1 for unq in uniques: # NOTE: cmp --> 0=same, -1=<, 1=> test = N.sum(abs(N.array(map(cmp, item, unq)))) if test == 0: # if item identical to any 1 row in uniques newflag = 0 # then not a novel item to add break if newflag == 1: try: uniques = N.concatenate([uniques, item[N.NewAxis, :]]) except TypeError: # the item to add isn't a list uniques = N.concatenate([uniques, N.array([item])]) return uniques
def _is_sorted(self, x): "return true if x is sorted" if len(x)<2: return 1 return numpy.alltrue(x[1:]-x[0:-1]>=0)
def _is_sorted(self, x): "return true if x is sorted" if len(x) < 2: return 1 return numpy.alltrue(x[1:] - x[0:-1] >= 0)
def testread(self): y, _, _ = maparray('tmp.foo', 'd') self.failUnless(Numeric.alltrue(self.x == y))
def testx(self): self.failUnless( Numeric.alltrue(self.x == Numeric.array( [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])))