def __getitem__(self, i): if type(i) is slice: start, stop, stride = i.indices(len(self.parent_dataset)) if stride > 1: return Numeric.arrayrange(start, stop, stride) else: return Numeric.arrayrange(start, stop) else: if i < len(self.parent_dataset): return i raise IndexError
def create_graph2d_view (): frame = VFrame (Label ("Graph2D")) frame.topleft = 10, 10 # Create the graph. graph = Graph2D (400, 400) # Lock it, because we set some necessary information and want to # avoid excessive update() calls. graph.lock () # Scale units for the x and y axis. graph.scale_units = ("cm", "kp") # Point of origin. graph.origin = 200, 200 # We want to see negative values. graph.negative = True # The evaluation function and data to use. graph.eval_func = lambda x: x**4.0 - 3 * x**2.0 + 2 * x if Numeric: graph.data = Numeric.arrayrange (-10, 10, .001).tolist() else: graph.data = numpy.arange (-10, 10, .001).tolist() # Done, unlock. graph.unlock () button = Button ("Change function") button.connect_signal (SIG_CLICKED, change, graph) frame.add_child (graph, button) return frame
def set_data (self, evt): dB = evt.data L = len (dB) if self.peak_hold: if self.peak_vals is None: self.peak_vals = dB else: self.peak_vals = Numeric.maximum(dB, self.peak_vals) dB = self.peak_vals x = max(abs(self.facsink.sample_rate), abs(self.facsink.baseband_freq)) sf = 1000.0 units = "ms" x_vals = ((Numeric.arrayrange (L/2) * ( (sf / self.facsink.sample_rate ) )) ) points = Numeric.zeros((len(x_vals), 2), Numeric.Float64) points[:,0] = x_vals points[:,1] = dB[0:L/2] lines = plot.PolyLine (points, colour='green') # DARKRED graphics = plot.PlotGraphics ([lines], title=self.facsink.title, xLabel = units, yLabel = "dB") self.Draw (graphics, xAxis=None, yAxis=self.y_range) self.update_y_range ()
def run (self): while (self.keep_running): s = os.read (self.file_descriptor, gr.sizeof_float * self.fft_size) # if the pipe dies, then quit if not s: self.keep_running = False break dB = Numeric.fromstring (s, Numeric.Float32) L = len(dB) x = max(abs(self.sample_rate), abs(self.baseband_freq)) if x >= 1e9: self.sf = 1e-9 self.units = "GHz" elif x >= 1e6: self.sf = 1e-6 self.units = "MHz" else: self.sf = 1e-3 self.units = "kHz" if self.input_is_real: # only plot 1/2 the points self.x_vals = ((Numeric.arrayrange (L/2) * (self.sample_rate * self.sf / L)) + self.baseband_freq * self.sf) self.y_vals = dB[0:L/2] #points = Numeric.zeros((len(self.x_vals), 2), Numeric.Float64) #points[:,0] = self.x_vals #points[:,1] = self.y_vals else: # the "negative freqs" are in the second half of the array self.x_vals = ((Numeric.arrayrange (-L/2, L/2) * (self.sample_rate * self.sf / L)) + self.baseband_freq * self.sf) self.y_vals = Numeric.concatenate ((dB[L/2:], dB[0:L/2])) #points = Numeric.zeros((len(self.x_vals), 2), Numeric.Float64) #points[:,0] = self.x_vals #points[:,1] = self.y_vals # scale points self.points_subscriptions.send_data((self.x_vals*40, self.y_vals/2))
def sliced_ds(parent_dataset, idx, name=None, filter_label=None, **kwargs): assert isinstance(idx, slice) indicies = idx.indices(len(parent_dataset)) record_ids = Numeric.arrayrange(*indicies) if name is None: name = 'slice_%d_%d_%d_%s' % (indicies + (parent_dataset.name,)) if filter_label is None: filter_label = '[%d:%d:%d] slice' % indicies return FilteredDataset(parent_dataset, record_ids, name=name, filter_label=filter_label, **kwargs)
def sliced_ds(parent_dataset, idx, name=None, filter_label=None, **kwargs): assert isinstance(idx, slice) indicies = idx.indices(len(parent_dataset)) record_ids = Numeric.arrayrange(*indicies) if name is None: name = 'slice_%d_%d_%d_%s' % (indicies + (parent_dataset.name, )) if filter_label is None: filter_label = '[%d:%d:%d] slice' % indicies return FilteredDataset(parent_dataset, record_ids, name=name, filter_label=filter_label, **kwargs)
def suppress_rows(self, suppress_marginal_totals=False): """ Remove marginal totals and suppressed rows from vectors """ if (not (suppress_marginal_totals and self.marginal_total_rows) and not self.suppressed_rows): return all_rows = Numeric.arrayrange(len(self.values()[0].data)) suppressed_rows = self.suppressed_rows if suppress_marginal_totals: suppressed_rows = soomfunc.union(suppressed_rows, self.marginal_total_rows) non_mt_rows = soomfunc.outersect(all_rows, suppressed_rows) for col in self.values(): col.filter_rows(non_mt_rows)
def determinant(a): _assertRank2(a) _assertSquareness(a) t =_commonType(a) a = _fastCopyAndTranspose(t, a) n = a.shape[0] if _array_kind[t] == 1: lapack_routine = lapack_lite.zgetrf else: lapack_routine = lapack_lite.dgetrf pivots = Numeric.zeros((n,), 'l') results = lapack_routine(n, n, a, n, pivots, 0) sign = Numeric.add.reduce(Numeric.not_equal(pivots, Numeric.arrayrange(1, n+1))) % 2 return (1.-2.*sign)*Numeric.multiply.reduce(Numeric.diagonal(a))
def hist(v, nbins=10, vmin=None, vmax=None, show=None, p=None): global HOLD, LAST, TABLE, NROWS, NCOLS, R, C if p is None: if HOLD: p = HOLD else: t = gettable() p = FramedPlot() t[R,C] = p try: nth = p.mplotnum + 1 except KeyError: nth = 0 p.mplotnum = nth if vmin is None: vmin = float(min(v)) if vmax is None: vmax = float(max(v)) binwidth = (vmax - vmin) / float(nbins-1) x = Numeric.arrayrange(vmin, vmax+binwidth, binwidth) y = Numeric.zeros(x.shape, 'i') for i in range(len(v)): n = int(round((float(v[i]) - vmin) / binwidth, 0)) try: y[n] = y[n] + 1 except IndexError: pass xx = Numeric.zeros(len(x) * 2 + 3, 'f') yy = Numeric.zeros(len(y) * 2 + 3) xx[0] = x[0] yy[0] = 0 for i in range(0, len(x)): xx[1+2*i],xx[1+2*i+1] = x[i],x[i]+binwidth yy[1+2*i],yy[1+2*i+1] = y[i],y[i] xx[1+2*i+2] = x[i]+binwidth yy[1+2*i+2] = 0; xx[1+2*i+3] = x[0]; yy[1+2*i+3] = 0; p.add(FillBelow(xx, yy), Curve(xx, yy)) p.yrange = 0, max(y) if show: p.show() LAST = p return p
def hist(v, nbins=10, vmin=None, vmax=None, show=None, p=None): global HOLD, LAST, TABLE, NROWS, NCOLS, R, C if p is None: if HOLD: p = HOLD else: t = gettable() p = FramedPlot() t[R, C] = p try: nth = p.mplotnum + 1 except KeyError: nth = 0 p.mplotnum = nth if vmin is None: vmin = float(min(v)) if vmax is None: vmax = float(max(v)) binwidth = (vmax - vmin) / float(nbins - 1) x = Numeric.arrayrange(vmin, vmax + binwidth, binwidth) y = Numeric.zeros(x.shape, 'i') for i in range(len(v)): n = int(round((float(v[i]) - vmin) / binwidth, 0)) try: y[n] = y[n] + 1 except IndexError: pass xx = Numeric.zeros(len(x) * 2 + 3, 'f') yy = Numeric.zeros(len(y) * 2 + 3) xx[0] = x[0] yy[0] = 0 for i in range(0, len(x)): xx[1 + 2 * i], xx[1 + 2 * i + 1] = x[i], x[i] + binwidth yy[1 + 2 * i], yy[1 + 2 * i + 1] = y[i], y[i] xx[1 + 2 * i + 2] = x[i] + binwidth yy[1 + 2 * i + 2] = 0 xx[1 + 2 * i + 3] = x[0] yy[1 + 2 * i + 3] = 0 p.add(FillBelow(xx, yy), Curve(xx, yy)) p.yrange = 0, max(y) if show: p.show() LAST = p return p
def comparison(self): _token_ = self._peek('"\\\\("', '"not"', 'ID') if _token_ == 'ID': col = self.col() op = self.op() term = self.term() return col.filter_op(op, term, self.filter_keys) elif _token_ == '"\\\\("': self._scan('"\\\\("') expr = self.expr() self._scan('"\\\\)"') return expr else:# == '"not"' self._scan('"not"') comparison = self.comparison() return soomfunc.outersect(Numeric.arrayrange(len(self.dataset)), comparison)
def demo(): data = Numeric.arrayrange(w*h) ## fftdata = FFT.fft(data) ## fftdata2 = FFT.fft(data2) ## fftdata3 = (fftdata + fftdata2) / 2. ## invfftdata = FFT.inverse_fft(fftdata3) ## data = invfftdata.real data = data.astype('l') im = Image.new("RGBA", (w, h)) print len(data.tostring("raw", "RGBX", 0, -1)) print len(im.tostring("raw", "RGBX", 0, -1)) im.fromstring(data.tostring("raw", "RGBX", 0, -1),"raw", "RGBX", 0, -1) root = Tkinter.Tk() image = ImageTk.PhotoImage(im) x = Tkinter.Label(root, image=image) x.pack() root.mainloop()
def demo(): data = Numeric.arrayrange(w * h) ## fftdata = FFT.fft(data) ## fftdata2 = FFT.fft(data2) ## fftdata3 = (fftdata + fftdata2) / 2. ## invfftdata = FFT.inverse_fft(fftdata3) ## data = invfftdata.real data = data.astype('l') im = Image.new("RGBA", (w, h)) print len(data.tostring("raw", "RGBX", 0, -1)) print len(im.tostring("raw", "RGBX", 0, -1)) im.fromstring(data.tostring("raw", "RGBX", 0, -1), "raw", "RGBX", 0, -1) root = Tkinter.Tk() image = ImageTk.PhotoImage(im) x = Tkinter.Label(root, image=image) x.pack() root.mainloop()
def test_arraydict(self): def _check(a): self.assertEqual(a['na'], na) self.assertEqual(a['maa'], maa) self.assertEqual(a['na'][500:-500], na[500:-500]) self.assertEqual(a['maa'][500:-500], maa[500:-500]) scatter = Numeric.array(xrange(0, len(na), 3)) self.assertEqual(a['na'].take(scatter), Numeric.take(na, scatter)) self.assertEqual(Numeric.take(a['na'],scatter), Numeric.take(na, scatter)) self.assertEqual(MA.take(a['maa'], scatter), MA.take(maa, scatter)) tmpfile = TempFile('soomtestarray_tmpfile') try: a = soomarray.ArrayDict(tmpfile.fn(), 'w') self.assertEqual(len(a), 0) self.assertRaises(KeyError, a.__getitem__, 'not_found') na = Numeric.arrayrange(10000) a['na'] = na na[1000] = -2222 # MmapArray __setitem__ a['na'][1000] = -2222 maa = MA.arrayrange(10000) for i in xrange(0, len(maa), 5): maa[i] = MA.masked a['maa'] = maa maa[1000] = -2222 a['maa'][1000] = -2222 _check(a) del a a = soomarray.ArrayDict(tmpfile.fn(), 'r') _check(a) finally: try: del a except UnboundLocalError: pass tmpfile.done()
def op_less_equal(self, value, filter_keys): return Numeric.arrayrange(value+1)
def op_less_than(self, value, filter_keys): return Numeric.arrayrange(value)
p.yrange = 0, max(y) if show: p.show() LAST = p return p def psprint(dest="-"): if TABLE: TABLE.write_eps(dest) if __name__ == '__main__': import sys, Numeric, RandomArray x = Numeric.arrayrange(-10, 10) y = x**2 e = y / 4 a = RandomArray.random([20, 20, 3]) imagesc(a, x=range(-10, 10), y=range(-10, 10)) drawnow() sys.stdin.readline() a = Numeric.array([[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]) imagesc(a) drawnow() sys.stdin.readline()
src_band = src_ds.GetRasterBand(band_number) # ---------------------------------------------------------------------------- # Ensure we recognise the driver. dst_driver = gdal.GetDriverByName(format) if dst_driver is None: print '"%s" driver not registered.' % format sys.exit(1) # ---------------------------------------------------------------------------- # Build color table. lookup = [ Numeric.arrayrange(256), Numeric.arrayrange(256), Numeric.arrayrange(256), Numeric.ones(256) * 255 ] ct = src_band.GetRasterColorTable() if ct is not None: for i in range(min(256, ct.GetCount())): entry = ct.GetColorEntry(i) for c in range(4): lookup[c][i] = entry[c] # ---------------------------------------------------------------------------- # Create the working file.
terms.add(term_re) trans_re = re.compile(r"\b(%s)\b" % "|".join(terms), re.I) outtrans_fns[column] = Highlighter(trans_re) return outtrans_fns def go(self): kwargs = self.get_params() ds = self.workspace.get_dataset() filterexpr = self.workspace.params.dsparams.filterexpr if filterexpr: try: dsrows = ds.filter(filterexpr).record_ids except Exception, e: raise UIError('Could not make filter: %s' % e) else: dsrows = Numeric.arrayrange(len(ds)) # set any output translations self.generate_title(ds, filterexpr) self.workspace.output.dsrows = dsrows self.workspace.output.highlight_fns = self.get_highlight_fns( filterexpr) self.workspace.output.colnames = kwargs['stratacols'] try: self.workspace.output.pagesize = int( self.workspace.params.pagesize) except ValueError: self.workspace.output.pagesize = 0 self.hide_params = True class Highlighter:
def op_greater_equal(self, value, filter_keys): return Numeric.arrayrange(value, len(self))
def op_between(self, value, filter_keys): return Numeric.arrayrange(*value)
# This module is a lite version of LinAlg.py module which contains
def op_less_equal(self, value, filter_keys): return Numeric.arrayrange(value + 1)
sys.exit(1) src_band = src_ds.GetRasterBand(band_number) # ---------------------------------------------------------------------------- # Ensure we recognise the driver. dst_driver = gdal.GetDriverByName(format) if dst_driver is None: print '"%s" driver not registered.' % format sys.exit(1) # ---------------------------------------------------------------------------- # Build color table. lookup = [ Numeric.arrayrange(256), Numeric.arrayrange(256), Numeric.arrayrange(256), Numeric.ones(256)*255 ] ct = src_band.GetRasterColorTable() if ct is not None: for i in range(min(256,ct.GetCount())): entry = ct.GetColorEntry(i) for c in range(4): lookup[c][i] = entry[c] # ---------------------------------------------------------------------------- # Create the working file.
def op_greater_than(self, value, filter_keys): return Numeric.arrayrange(value + 1, len(self))
p.add(FillBelow(xx, yy), Curve(xx, yy)) p.yrange = 0, max(y) if show: p.show() LAST = p return p def psprint(dest="-"): if TABLE: TABLE.write_eps(dest) if __name__=='__main__' : import sys, Numeric, RandomArray x = Numeric.arrayrange(-10,10); y = x**2; e = y/4 a = RandomArray.random([20,20,3]) imagesc(a, x=range(-10,10), y=range(-10,10)) drawnow() sys.stdin.readline() a = Numeric.array([[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0],
terms.add(term_re) trans_re = re.compile(r"\b(%s)\b" % "|".join(terms), re.I) outtrans_fns[column] = Highlighter(trans_re) return outtrans_fns def go(self): kwargs = self.get_params() ds = self.workspace.get_dataset() filterexpr = self.workspace.params.dsparams.filterexpr if filterexpr: try: dsrows = ds.filter(filterexpr).record_ids except Exception, e: raise UIError('Could not make filter: %s' % e) else: dsrows = Numeric.arrayrange(len(ds)) # set any output translations self.generate_title(ds, filterexpr) self.workspace.output.dsrows = dsrows self.workspace.output.highlight_fns = self.get_highlight_fns(filterexpr) self.workspace.output.colnames = kwargs['stratacols'] try: self.workspace.output.pagesize = int(self.workspace.params.pagesize) except ValueError: self.workspace.output.pagesize = 0 self.hide_params = True class Highlighter: def __init__(self, trans_re): self.trans_re = trans_re
def op_greater_than(self, value, filter_keys): return Numeric.arrayrange(value+1, len(self))
import Numeric import FFT import Tkinter import Image import ImageTk import sys w = 256 h = 256 if __name__ == '__main__': data = Numeric.arrayrange(w*h) data = data.astype('l') im = Image.new("RGBA", (w, h)) print len(data.tostring()) print len(im.tostring()) im.fromstring(data.tostring()) root = Tkinter.Tk() image = ImageTk.PhotoImage(im) x = Tkinter.Label(root, image=image) x.pack() root.mainloop()