def convert_segments_to_vertex(self, data): """ Given the list of segments and their connections, provides vertex-to-vertex information """ vertices = { 'vertex_start': [], 'vertex_end': [], 'distance': [], 'type': [], 'geometry': [], 'tree_number': [], } # 'park_weight': []} # loop over all the segments for i in range(len(data['type'])): # loop over the 2 extremities of each segment for which in ['start', 'end']: name1 = names.lon_lat_to_name(data['lon_' + which][i], data['lat_' + which][i]) # loop over the connected segments for j in data['connections_' + which][i]: # find the vertices who are the same to identify which # distance to keep if data['name_start'][j] == name1: name2 = data['name_end'][j] elif data['name_end'][j] == name1: name2 = data['name_start'][j] else: raise ValueError('Problem!!!') # find if the vertex is already defined is_defined1 = pl.array(vertices["vertex_start"]) == name1 is_defined2 = pl.array(vertices['vertex_end']) == name2 is_definedx = pl.logical_and(is_defined1, is_defined2) # find if the vertex is already defined is_defined1 = pl.array(vertices["vertex_start"]) == name2 is_defined2 = pl.array(vertices['vertex_end']) == name1 is_definedy = pl.logical_and(is_defined1, is_defined2) is_defined = pl.logical_or(is_definedx, is_definedy) is_defined = len(pl.where(is_defined)[0]) # stores the path informations if not is_defined: vertices['vertex_start'].append(name1) vertices['vertex_end'].append(name2) vertices['distance'].append(data['distance'][j]) vertices['tree_number'].append(data['tree_number'][j]) vertices['type'].append(data['type'][j]) vertices['geometry'].append(data['geometry'][j]) # === write function to add tree numbers return vertices
def getDissim(data, atype, vbose=0, minRank=0, maxRank=50, NPOZ=50): ks = data.keys() matr = pylab.ones(len(ks)**2) matr = pylab.reshape(matr, (len(ks), len(ks))) scs = [] names = [] for ik, k_con in enumerate(ks): name = ik if not k_con in names: names.append(k_con) for jk, k_pl in enumerate(ks): ss1 = computeSimSc(data, k_con, k_pl, vbose=vbose, minRank=minRank, maxRank=maxRank, NPOZ=NPOZ) ss2 = computeSimSc(data, k_pl, k_con, vbose=vbose, minRank=minRank, maxRank=maxRank, NPOZ=NPOZ) if atype=='abs': sc1 = sum(ss1) sc2 = sum(ss2) elif atype=='rms': sc1 = pylab.sqrt(pylab.sum(ss1**2)) sc2 = pylab.sqrt(pylab.sum(ss2**2)) elif atype=='met': sc1 = sum(pylab.logical_and(ss1!=0, True)) sc2 = sum(pylab.logical_and(ss2!=0, True)) if vbose>=1: print 'score for ', k_con, k_pl, ss1, sc1, ss2, sc2 oldsc = sc1 + sc2 oldsc *= 0.5 l1 = len(data[k_con]) l2 = len(data[k_pl]) iscale = min(l1, l2) nsc = oldsc/(1.0*iscale) if vbose>=1: print k_con, k_pl, 'oldsc', oldsc, l1, l2, iscale, 'nsc', nsc matr[ik][jk] = nsc if jk<=ik: continue print nsc, 'xx', ik, k_con, jk, k_pl scs.append(nsc) return names, pylab.array(scs), matr
def spec_demo(): ''' the demo in matplotlib. But calls interactive.specgram ''' from pylab import arange, sin, where, logical_and, randn, pi dt = 0.0005 t = arange(0.0, 20.0, dt) s1 = sin(2 * pi * 100 * t) s2 = 2 * sin(2 * pi * 400 * t) # create a transient "chirp" mask = where(logical_and(t > 10, t < 12), 1.0, 0.0) s2 = s2 * mask # add some noise into the mix nse = 0.01 * randn(len(t)) x = s1 + s2 + nse # the signal NFFT = 1024 # the length of the windowing segments Fs = int(1.0 / dt) # the sampling frequency from ifigure.interactive import figure, spec, nsec, plot, isec, clog, hold figure() hold(True) nsec(2) isec(0) plot(t, x) isec(1) spec(t, x, NFFT=NFFT, noverlap=900) clog()
def specgram_demo(): ''' the demo in matplotlib. But calls interactive.specgram ''' from pylab import arange, sin, where, logical_and, randn, pi dt = 0.0005 t = arange(0.0, 20.0, dt) s1 = sin(2*pi*100*t) s2 = 2*sin(2*pi*400*t) # create a transient "chirp" mask = where(logical_and(t>10, t<12), 1.0, 0.0) s2 = s2 * mask # add some noise into the mix nse = 0.01*randn(len(t)) x = s1 + s2 + nse # the signal NFFT = 1024 # the length of the windowing segments Fs = int(1.0/dt) # the sampling frequency from ifigure.interactive import figure, specgram, nsec, plot, isec, clog, hold figure() hold(True) nsec(2) isec(0) plot(t, x) isec(1) specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900) clog()
def plotdata(trendselection=None, startdate='2000-01-01', enddate='2018-01-01', trendline=False): # Handle inputs startyear = convertdate(startdate, '%Y-%m-%d') endyear = convertdate(enddate, '%Y-%m-%d') trendoptions = getoptions(tojson=False) if trendselection is None: trendselection = trendoptions.keys()[0] datatype = trendoptions[trendselection] # Make graph fig = pl.figure() fig.add_subplot(111) thesedata = df.findrows(key=datatype, col='type') years = thesedata['date'] vals = thesedata['close'] validinds = sc.findinds(pl.logical_and(years>=startyear, years<=endyear)) x = years[validinds] y = vals[validinds] pl.plot(x, y) pl.xlabel('Date') pl.ylabel('Trend index') # Add optional trendline if trendline: newy = sc.smoothinterp(x, x, y, smoothness=200) pl.plot(x, newy, lw=3) # Convert to FE graphjson = sw.mpld3ify(fig, jsonify=False) # Convert to dict return graphjson # Return the JSON representation of the Matplotlib figure
def save_sonogram(self, replace=False, n_fft=settings.N_FFT, \ min_freq=settings.MIN_FREQ, \ max_freq=settings.MAX_FREQ, \ dpi=100, width=1000, height=350, max_framerate=settings.MAX_FRAMERATE): filename = self.get_sonogram_name() name = os.path.join(settings.SONOGRAM_DIR, filename) path = os.path.join(settings.MEDIA_ROOT, name) try: if not os.path.exists(path): replace = True except (ValueError, SuspiciousOperation, AttributeError): replace = True if replace: audio, framerate = self.get_audio(max_framerate=max_framerate) Pxx, freqs, bins, im = specgram(audio, NFFT=n_fft, Fs=framerate) f = where(logical_and(freqs > min_freq, freqs <= max_freq))[0] Pxx[where(Pxx > percentile(Pxx[f].flatten(), 99.99))] = percentile( Pxx[f].flatten(), 99.99) Pxx[where(Pxx < percentile(Pxx[f].flatten(), 0.01))] = percentile( Pxx[f].flatten(), 0.01) clf() fig = figure(figsize=(float(width) / dpi, float(height) / dpi), dpi=dpi) imshow(flipud(10 * log10(Pxx[f, ])), extent=(bins[0], bins[-1], freqs[f][0], freqs[f][-1]), aspect='auto', cmap=cm.gray) gca().set_ylabel('Frequency (Hz)') gca().set_xlabel('Time (s)') axis_pixels = gca().transData.transform( np.array((gca().get_xlim(), gca().get_ylim())).T) st, created = SonogramTransform.objects.get_or_create( n_fft=n_fft, framerate=framerate, min_freq=min_freq, max_freq=max_freq, duration=self.duration, width=width, height=height, dpi=dpi, top_px=max(axis_pixels[:, 1]), bottom_px=min(axis_pixels[:, 1]), left_px=min(axis_pixels[:, 0]), right_px=max(axis_pixels[:, 0]), ) savefig(open(path, 'wb'), format='jpg', dpi=dpi) sonogram, created = Sonogram.objects.get_or_create(snippet=self, transform=st, path=name) close()
def save_sonogram( self, replace=False, n_fft=settings.N_FFT, min_freq=settings.MIN_FREQ, max_freq=settings.MAX_FREQ, dpi=100, width=1000, height=350, max_framerate=settings.MAX_FRAMERATE, ): filename = self.get_sonogram_name() name = os.path.join(settings.SONOGRAM_DIR, filename) path = os.path.join(settings.MEDIA_ROOT, name) try: if not os.path.exists(path): replace = True except (ValueError, SuspiciousOperation, AttributeError): replace = True if replace: audio, framerate = self.get_audio(max_framerate=max_framerate) Pxx, freqs, bins, im = specgram(audio, NFFT=n_fft, Fs=framerate) f = where(logical_and(freqs > min_freq, freqs <= max_freq))[0] Pxx[where(Pxx > percentile(Pxx[f].flatten(), 99.99))] = percentile(Pxx[f].flatten(), 99.99) Pxx[where(Pxx < percentile(Pxx[f].flatten(), 0.01))] = percentile(Pxx[f].flatten(), 0.01) clf() fig = figure(figsize=(float(width) / dpi, float(height) / dpi), dpi=dpi) imshow( flipud(10 * log10(Pxx[f,])), extent=(bins[0], bins[-1], freqs[f][0], freqs[f][-1]), aspect="auto", cmap=cm.gray, ) gca().set_ylabel("Frequency (Hz)") gca().set_xlabel("Time (s)") axis_pixels = gca().transData.transform(np.array((gca().get_xlim(), gca().get_ylim())).T) st, created = SonogramTransform.objects.get_or_create( n_fft=n_fft, framerate=framerate, min_freq=min_freq, max_freq=max_freq, duration=self.duration, width=width, height=height, dpi=dpi, top_px=max(axis_pixels[:, 1]), bottom_px=min(axis_pixels[:, 1]), left_px=min(axis_pixels[:, 0]), right_px=max(axis_pixels[:, 0]), ) savefig(open(path, "wb"), format="jpg", dpi=dpi) sonogram, created = Sonogram.objects.get_or_create(snippet=self, transform=st, path=name) close()
def main(): XC = loadtxt('iris.data', delimiter=',', dtype=float, converters={4: cnvt}) ind = arange(150) # indices into the dataset ind = permutation(ind) # random permutation L = ind[0:90] # learning set indices T = ind[90:] # test set indices # Learning Set X = transpose(XC[L, 0:4]) nnc = NNb(X, XC[L, -1]) # Classification of Test Set c = zeros(len(T)) for i in arange(len(T)): print sys.argv[1] print int(sys.argv[1]) c[i] = nnc.classify(XC[T[i], 0:4], int(sys.argv[1])) # Confusion Matrix CM = zeros((3, 3)) for i in range(3): for j in range(3): CM[i, j] = sum(logical_and(XC[T, 4] == (i + 1), c == (j + 1))) print(CM) # Plot Test Set figure(1) color = array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) for i in range(4): for j in range(4): subplot(4, 4, 4 * i + j + 1) if i == j: continue print color[XC[T, 4].astype(int) - 1] print[1, 1, 1] * len(T) print color[c.astype(int) - 1] scatter(XC[T, i], XC[T, j], s=100, marker='s', edgecolor=color[XC[T, 4].astype(int) - 1], facecolor=[1, 1, 1] * len(T)) scatter(XC[T, i], XC[T, j], s=30, marker='+', edgecolor=color[c.astype(int) - 1]) savefig('figures/nnbtest.pdf')
def fit_modes_0(o, a, v, ixrange): ix = [plt.where(plt.logical_and(-1<o, o<1)[:,i]) for i in range(o.shape[1])] o = [o[ix[i],i][0] for i in range(o.shape[1])] a = [a[ix[i],i][0] for i in range(a.shape[1])] ax = [plt.argmax(a[i][:]) for i in range(len(a))] o = plt.array([o[i][ax[i]] for i in range(len(o))]) a = plt.array([a[i][ax[i]] for i in range(len(a))]) x, y = v[list(ixrange)], o[list(ixrange)] p = plt.polyfit(x, y, 1) z = v*p[0] + p[1] return x, y, z, p
def test(x_offset, y_offset, angle, min_distance = 0.25, max_distance = 3.6): max_dist = (max_distance*scale) ** 2 min_dist = (min_distance*scale) ** 2 xs = Xs - x_offset ys = Ys - y_offset angles = pylab.arctan2( xs, ys ) angles = (angles - radians(angle)) % (2*pi) angles -= 2*pi * (angles > pi) evidence = wide_angle_function(angles) dist = xs**2 + ys**2 evidence *= pylab.logical_and( dist < max_dist, dist > min_dist) return evidence
def test(x_offset, y_offset, angle, min_distance=0.25, max_distance=3.6): max_dist = (max_distance * scale)**2 min_dist = (min_distance * scale)**2 xs = Xs - x_offset ys = Ys - y_offset angles = pylab.arctan2(xs, ys) angles = (angles - radians(angle)) % (2 * pi) angles -= 2 * pi * (angles > pi) evidence = wide_angle_function(angles) dist = xs**2 + ys**2 evidence *= pylab.logical_and(dist < max_dist, dist > min_dist) return evidence
def fft_filter(time, signal, result, window_func=pl.hanning, \ samp_freq=8, freq_limit = (0.0033, 0.04)): """ Applies an Band Pass filter to signal in the frequency domain and returns filterd PSD. In: time : ndarray, relative R occurance times vector signal : ndarray, HRV vector result : dict, current fragment info (see process function) samp_freq : int, signal sample frequency freq_limit : tuple, (min, max) frequencies for band pass filter Out: freq_filt_log : ndarray, frequency in logarithm scale spec_filt_log : ndarray, PSD in logarithm scale """ window = window_func(len(signal)) # window signal_wed = signal * window n = len(signal)/2 spec = (pl.absolute(pl.rfft(signal_wed))[:-1]/(n))**2 # entering to frequency domain freq = pl.fftfreq(len(signal), 1.0/samp_freq)[:n] # get freqs axis values spec_filt = pl.zeros(spec.shape, dtype='float') freq_filt = pl.zeros(freq.shape, dtype='float') # same for filtered for i in range(n): # filter by frequency if pl.logical_and(freq_limit[0] <= abs(freq[i]), abs(freq[i]) <= freq_limit[1]): freq_filt[i] = freq[i] spec_filt[i] = spec[i] else: freq_filt[i] = 0 # fill invalid frequencies with 0 value spec_filt[i] = 0 spec_filt = pl.ma.masked_equal(spec_filt,0) # cutt off invalid values spec_filt = pl.ma.compressed(spec_filt) freq_filt = pl.ma.masked_equal(freq_filt,0) freq_filt = pl.ma.compressed(freq_filt) spec_filt_log = pl.log10(spec_filt) # for output freq_filt_log = pl.log10(freq_filt) return freq_filt_log, spec_filt_log
def specgram(): """ smartplotlib example of specgram """ from pylab import np, pi, sin, arange, logical_and, where, randn, cm from smartplotlib import xyplot, subplot dt = 0.0005 t = arange(0.0, 20.0, dt) s1 = sin(2*pi*100*t) s2 = 2*sin(2*pi*400*t) # create a transient "chirp" mask = where(logical_and(t>10, t<12), 1.0, 0.0) s2 = s2 * mask # add some noise into the mix nse = 0.01*randn(len(t)) x = s1 + s2 + nse # the signal NFFT = 1024 # the length of the windowing segments Fs = int(1.0/dt) # the sampling frequency # Pxx is the segments x freqs array of instantaneous power, freqs is # the frequency vector, bins are the centers of the time bins in which # the power is computed, and im is the matplotlib.image.AxesImage # instance xy = xyplot(t, x, axes=211, xlabel="t", ylabel="x") ### # xy.ydata is a dataplot from whish data is aliased to "y" # a dataplot contain a collection of plot that can be obtain # from a 1d data spec = xy.ydata.specgram(NFFT=NFFT, Fs=Fs, noverlap=900, cmap=cm.gist_heat, axes=212) xy.fclear() xy.axes() xy.plot() # above is same as xy.go("fclear", "axes", "plot") spec.go("axes", "imshow", "show", "axes", "draw") return spec
def main(k): XC = loadtxt('data/iris.data', delimiter=',', dtype=float, converters={4: cnvt}) ind = arange(150) # indices into the dataset ind = permutation(ind) # random permutation L = ind[0:90] # learning set indices T = ind[90:] # test set indices # Learning Set # The k-NNb classifier is called for given k. X = transpose(XC[L, 0:4]) nnc = NNb(X, XC[L, -1], k) # Classification of Test Set c = zeros(len(T)) for i in range(len(T)): c[i] = nnc.classify(XC[T[i], 0:4]) # Confusion Matrix CM = zeros((3, 3)) for i in range(3): for j in range(3): CM[i, j] = sum(logical_and(XC[T, 4] == (i + 1), c == (j + 1))) print(CM) # Plot Test Set plt.figure(1) color = array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) for i in range(4): for j in range(4): plt.subplot(4, 4, 4 * i + j + 1) if i == j: continue plt.scatter(XC[T, i], XC[T, j], s=100, marker='s', edgecolor=color[XC[T, 4].astype(int) - 1], facecolor=[1, 1, 1] * len(T)) plt.scatter(XC[T, i], XC[T, j], s=30, marker='+', edgecolor=color[c.astype(int) - 1]) plt.savefig('lab_42.png')
def __filter__(self, _data_vector): indexes = pl.find(pl.logical_and(_data_vector.signal >= self.min_value, _data_vector.signal <= self.max_value)) if len(indexes) == len(_data_vector.signal): # nothing change return _data_vector else: signal = _data_vector.signal[indexes] annotation = _data_vector.annotation[indexes] signal_plus = signal[pl.arange(0, len(signal) - self.__shift__)] signal_minus = signal[pl.arange(self.__shift__, len(signal))] time = _data_vector.time[indexes] if _data_vector.time else None return DataVector( signal=signal, signal_plus=signal_plus, signal_minus=signal_minus, time=time, annotation=annotation, signal_unit=_data_vector.signal_unit, signal_header=_data_vector.signal_header, annotation_header=_data_vector.annotation_header, time_header=_data_vector.time_header, )
def __filter__(self, _data_vector): indexes = pl.find( pl.logical_and(_data_vector.signal >= self.min_value, _data_vector.signal <= self.max_value)) if len(indexes) == len(_data_vector.signal): # nothing change return _data_vector else: signal = _data_vector.signal[indexes] annotation = _data_vector.annotation[indexes] signal_plus = signal[pl.arange(0, len(signal) - self.__shift__)] signal_minus = signal[pl.arange(self.__shift__, len(signal))] time = _data_vector.time[indexes] if _data_vector.time else None return DataVector(signal=signal, signal_plus=signal_plus, signal_minus=signal_minus, time=time, annotation=annotation, signal_unit=_data_vector.signal_unit, signal_header=_data_vector.signal_header, annotation_header=_data_vector.annotation_header, time_header=_data_vector.time_header)
cross_list.append(cross) alpha_list.append(alpha) else: nlwlhti += 1 elif i == 2: # First ionized species print("\nFind possible ABO broadening for excited species in", IFILE0,'...') if SPE.strip().upper() in ['BE', 'MG', 'CA', 'SR', 'BA', 'FE', 'CR']: eps_lbd = 0.5 # A eps_e = 30.0 # cm⁻¹ abo_data = pl.loadtxt(IFILE0, dtype='S') abo_data = abo_data.compress(abo_data.T[1] == SPE.strip().upper(), axis=0) for line in init_lines_list: crit1 = abs(pl.array(map(float, abo_data.T[4]))-line.lower.e) < eps_e crit2 = abs(pl.array(map(float, abo_data.T[5]))-line.upper.e) < eps_e dumb = abo_data.compress(pl.logical_and(crit1, crit2), axis=0) #dumb = abo_data.compress(abs(pl.array(map(float, abo_data.T[3]))- line.lbd) < eps_lbd, axis=0) if len(dumb) >= 1: if len(dumb) == 1: dumb = list(dumb.T.flatten()) cross = float(dumb[8]) alpha = float(dumb[9]) line.gv = cross + alpha #print(line.gv) nabo += 1 else: # Compute mean of cross and alpha #print("Warning:", len(dumb), dumb.T[8], dumb.T[9], end=' ') cross = pl.array(map(float, dumb.T[8])) cross = cross.mean() alpha = pl.array(map(float, dumb.T[9]))
def fig12_flatness( path, fig, ax, tmin=0, tmax=1000, delta_t=0.5, key_var=("uy", "ux"), run_nb=0, ax_inset=None, cache=False, n_colors=10, ): sim = fls.load_sim_for_plot(path, merge_missing_params=True) order = [2, 4] rxs, So_var_dict, deltax = _rxs_str_func( sim, order, tmin, tmax, delta_t, key_var, cache=cache ) ax.set_xlabel("$r/L_f$") # ax.set_ylabel('$F_T, F_L$') # ax.set_title('Flatness of longitundinal and transverse increments') # ax.hold(True) ax.set_xscale("log") ax.set_yscale("log") _label = {"ux": "F_L", "uy": "F_T"} L_f = pl.pi / _k_f(sim.params) # color_list = ['r', 'b', 'g', 'c', 'm', 'r', 'b'] color_list = sns.color_palette(n_colors=n_colors) def get_F(key): So_4 = So_var_dict["{0}_{1:.0f}".format(key, 4)] So_2 = So_var_dict["{0}_{1:.0f}".format(key, 2)] return So_4 / So_2 ** 2 if len(key_var) == 1: key = key_var[0] F = get_F(key) label = _label[key] # r^-1 line cond = pl.logical_and(rxs > 0.015 * L_f, rxs < 0.17 * L_f) fac = 12 ax.plot(rxs[cond] / L_f, fac / rxs[cond], "k", linewidth=0.5) x_text = rxs[cond].mean() / 2 / L_f + 0.02 y_text = fac / rxs[cond].mean() * 2 ax.text(x_text, y_text, "$r^{-1}$") else: F = np.divide(*map(get_F, key_var)) print("F=", F.shape) label = f"{_label[key_var[0]]}/{_label[key_var[1]]}" label = f"${label}$" color1 = color_list[run_nb] ax.plot(rxs / L_f, F, c=color1, linewidth=1, label=label) if ax_inset is not None: ax_inset.semilogx( rxs / L_f, get_F("uy") / get_F("ux"), color_list[run_nb], linewidth=1 )
T = ind[90:] # test set indices # Learning Set X = transpose(XC[L,0:4]) nnc = NNb(X, XC[L,-1]) # Classification of Test Set c = zeros(len(T)) for i in arange(len(T)): c[i] = nnc.classify(XC[T[i],0:4]) # Confusion Matrix CM = zeros((3,3)) for i in range(3): for j in range(3): CM[i,j] = sum( logical_and(XC[T,4] == (i + 1), c == (j + 1)) ) print(CM) # Plot Test Set figure(1) color = array( [[1,0,0],[0,1,0],[0,0,1]] ) for i in range(4): for j in range(4): subplot(4,4,4*i+j+1) if i==j: continue scatter( XC[T,i], XC[T,j], s=100, marker='s', edgecolor=color[XC[T,4].astype(int)-1], facecolor=[1,1,1]*len(T)) scatter( XC[T,i], XC[T,j], s=30, marker='+',
def drawmeridians(self,meridians,color='k',linewidth=1., \ linestyle='--',dashes=[1,1],labels=[0,0,0,0],\ font='rm',fontsize=12): """ draw meridians (longitude lines). meridians - list containing longitude values to draw (in degrees). color - color to draw meridians (default black). linewidth - line width for meridians (default 1.) linestyle - line style for meridians (default '--', i.e. dashed). dashes - dash pattern for meridians (default [1,1], i.e. 1 pixel on, 1 pixel off). labels - list of 4 values (default [0,0,0,0]) that control whether meridians are labelled where they intersect the left, right, top or bottom of the plot. For example labels=[1,0,0,1] will cause meridians to be labelled where they intersect the left and bottom of the plot, but not the right and top. Labels are drawn using mathtext. font - mathtext font used for labels ('rm','tt','it' or 'cal', default 'rm'). fontsize - font size in points for labels (default 12). """ # get current axes instance. ax = pylab.gca() # don't draw meridians past latmax, always draw parallel at latmax. latmax = 80. # not used for cyl, merc projections. # offset for labels. yoffset = (self.urcrnry-self.llcrnry)/100./self.aspect xoffset = (self.urcrnrx-self.llcrnrx)/100. if self.projection not in ['merc','cyl']: lats = pylab.arange(-latmax,latmax+1).astype('f') else: lats = pylab.arange(-90,91).astype('f') xdelta = 0.1*(self.xmax-self.xmin) ydelta = 0.1*(self.ymax-self.ymin) for merid in meridians: lons = merid*pylab.ones(len(lats),'f') x,y = self(lons,lats) # remove points outside domain. testx = pylab.logical_and(x>=self.xmin-xdelta,x<=self.xmax+xdelta) x = pylab.compress(testx, x) y = pylab.compress(testx, y) testy = pylab.logical_and(y>=self.ymin-ydelta,y<=self.ymax+ydelta) x = pylab.compress(testy, x) y = pylab.compress(testy, y) if len(x) > 1 and len(y) > 1: # split into separate line segments if necessary. # (not necessary for mercator or cylindrical). xd = (x[1:]-x[0:-1])**2 yd = (y[1:]-y[0:-1])**2 dist = pylab.sqrt(xd+yd) split = dist > 500000. if pylab.asum(split) and self.projection not in ['merc','cyl']: ind = (pylab.compress(split,pylab.squeeze(split*pylab.indices(xd.shape)))+1).tolist() xl = [] yl = [] iprev = 0 ind.append(len(xd)) for i in ind: xl.append(x[iprev:i]) yl.append(y[iprev:i]) iprev = i else: xl = [x] yl = [y] # draw each line segment. for x,y in zip(xl,yl): # skip if only a point. if len(x) > 1 and len(y) > 1: l = Line2D(x,y,linewidth=linewidth,linestyle=linestyle) l.set_color(color) l.set_dashes(dashes) ax.add_line(l) # draw labels for meridians. # search along edges of map to see if parallels intersect. # if so, find x,y location of intersection and draw a label there. if self.projection == 'cyl': dx = 0.01; dy = 0.01 else: dx = 1000; dy = 1000 for dolab,side in zip(labels,['l','r','t','b']): if not dolab: continue # for cyl or merc, don't draw meridians on left or right. if self.projection in ['cyl','merc'] and side in ['l','r']: continue if side in ['l','r']: nmax = int((self.ymax-self.ymin)/dy+1) if self.urcrnry < self.llcrnry: yy = self.llcrnry-dy*pylab.arange(nmax) else: yy = self.llcrnry+dy*pylab.arange(nmax) if side == 'l': lons,lats = self(self.llcrnrx*pylab.ones(yy.shape,'f'),yy,inverse=True) else: lons,lats = self(self.urcrnrx*pylab.ones(yy.shape,'f'),yy,inverse=True) lons = pylab.where(lons < 0, lons+360, lons) lons = [int(lon*10) for lon in lons.tolist()] lats = [int(lat*10) for lat in lats.tolist()] else: nmax = int((self.xmax-self.xmin)/dx+1) if self.urcrnrx < self.llcrnrx: xx = self.llcrnrx-dx*pylab.arange(nmax) else: xx = self.llcrnrx+dx*pylab.arange(nmax) if side == 'b': lons,lats = self(xx,self.llcrnry*pylab.ones(xx.shape,'f'),inverse=True) else: lons,lats = self(xx,self.urcrnry*pylab.ones(xx.shape,'f'),inverse=True) lons = pylab.where(lons < 0, lons+360, lons) lons = [int(lon*10) for lon in lons.tolist()] lats = [int(lat*10) for lat in lats.tolist()] for lon in meridians: if lon<0: lon=lon+360. # find index of meridian (there may be two, so # search from left and right). try: nl = lons.index(int(lon*10)) except: nl = -1 try: nr = len(lons)-lons[::-1].index(int(lon*10))-1 except: nr = -1 if lon>180: lonlab = r'$\%s{%g\/^{\circ}\/W}$'%(font,pylab.fabs(lon-360)) elif lon<180 and lon != 0: lonlab = r'$\%s{%g\/^{\circ}\/E}$'%(font,lon) else: lonlab = r'$\%s{%g\/^{\circ}}$'%(font,lon) # meridians can intersect each map edge twice. for i,n in enumerate([nl,nr]): lat = lats[n]/10. # no meridians > latmax for projections other than merc,cyl. if self.projection not in ['merc','cyl'] and lat > latmax: continue # don't bother if close to the first label. if i and abs(nr-nl) < 100: continue if n >= 0: if side == 'l': pylab.text(self.llcrnrx-xoffset,yy[n],lonlab,horizontalalignment='right',verticalalignment='center',fontsize=fontsize) elif side == 'r': pylab.text(self.urcrnrx+xoffset,yy[n],lonlab,horizontalalignment='left',verticalalignment='center',fontsize=fontsize) elif side == 'b': pylab.text(xx[n],self.llcrnry-yoffset,lonlab,horizontalalignment='center',verticalalignment='top',fontsize=fontsize) else: pylab.text(xx[n],self.urcrnry+yoffset,lonlab,horizontalalignment='center',verticalalignment='bottom',fontsize=fontsize) # make sure axis ticks are turned off ax.set_xticks([]) ax.set_yticks([]) # set axes limits to fit map region. self.set_axes_limits()
p.plot(t, v) p.xlabel("time [s]") p.ylabel("V_mem [converted mV]") for _, region in regions.items(): p.axvspan((region[0] + start_offset) / 1000. / 10000., region[1] / 1000. / 10000., alpha=.1, color='k') p.savefig(os.path.join(args.outdir, "v.png")) results = {} for name, region in regions.items(): mask = p.logical_and(t >= (region[0] + start_offset) / 1000. / 10000., t <= region[1] / 1000. / 10000.) vv = v[mask] mu = p.mean(vv) std = p.std(vv, ddof=1) results[name] = (name, region[0] + start_offset, region[1], mu, std, len(vv)) f = open(os.path.join(args.outdir, "result.txt"), "w") for name, l in results.items(): f.write("\t".join([name] + [str(i) for i in l]) + "\n") f.close()
def select_data(i, data, lbdmin, lbdmax, obslist, lbdunitlist, hshiftlist): '''Selection of spectrum in a given wavelength range ''' bla = False global p # Case where wavelength are not in Angstrom but in nm if lbdunitlist[i] in ['a', 'Å', 'A', '', None]: pass elif lbdunitlist[i] == 'nm': data[:, 0] = data[:, 0]*10 else: print "Wavelength unit unknown. Try Å|A|a or nm." quit(1) if hshiftlist[i] in [None, '']: hshift = 0.0 else: hshift = float(hshiftlist[i]) data[:, 0] = data[:, 0] + hshift crit = pl.logical_and(data[:, 0] <= lbdmax, data[:, 0] >= lbdmin) if not crit.any(): # Case of 1 spectrum only without any idea of wavelength unit #if len(obslist) == 1: data[:, 0] = data[:, 0]*10 crit = pl.logical_and(data[:, 0] <= lbdmax, data[:, 0] >= lbdmin) print "wavelenght of the spectrum are outside the selection range." print "Maybe wavelengths are in nm ... convert wavelengths in Å and try again..." if not crit.any(): # print "Wavelength range outside the spectrum." # quit(1) # Skip this spectrum #else: print "Required (or default) wavelength range outside of this spectrum." print " Number of wavelength points:", len(data[:, 0]).__format__('11g') print " Lambda min: ", min(data[:, 0]/10).__format__('9.3f') print " Lambda max: ", max(data[:, 0]/10).__format__('9.3f') return None, None, p if bla: print " Number of wavelength points:", len(data[:, 0]).__format__('11g') print " Lambda min: ", min(data[:, 0]).__format__('9.3f') print " Lambda max: ", max(data[:, 0]).__format__('9.3f') x = pl.compress(crit, data[:, 0]) y = pl.compress(crit, data[:, 1]) # Case where there is a third column in input ASCII data considered as the absolute flux if abs_flux: try: y = pl.compress(crit, data[:, 2]) except: pass if bla: print " Number of selected points: ", len(x).__format__('11g') print " Selected lambda min: ", min(x).__format__('9.3f') print " Selected lambda max: ", max(x).__format__('9.3f') if hshift != 0.0: print " Including a shift of: ", hshift.__format__('9.3f') #Flag to plot spectra p = True return x, y, p
def select_ll(llfile, lbdmin, lbdmax, lbdrange): '''Select lines to show on the plot by vertical lines ''' bla = False try: llfile = pl.loadtxt(llname, dtype='str', comments='#', delimiter='\n') except IOError: print "Linelist file does not exist." quit(1) #return None, None, None elt_ll = [line[0:9] for line in llfile] lbd_ll = [float(line[9:18]) for line in llfile] try: lgf_ll = [float(line[18:]) for line in llfile] except: lgf_ll = None elt_ll = pl.asarray(elt_ll) lbd_ll = pl.asarray(lbd_ll) lgf_ll = pl.asarray(lgf_ll) crit = pl.logical_and(lbd_ll <= lbdmax, lbd_ll >= lbdmin) elt_ll_set = pl.compress(crit, elt_ll) lbd_ll_set = pl.compress(crit, lbd_ll) try: lgf_ll_set = pl.compress(crit, lgf_ll) except: lgf_ll_set = None nid = lbd_ll_set.size dlbd_ll_set = lbd_ll_set[1:] - lbd_ll_set[0:nid - 1] dlbd_mean = pl.mean(dlbd_ll_set) if bla: print "" print " Number of line identification in the selected range:", nid print " Number of identification/Å:", (nid / lbdrange).__format__('7.2f') print " Mean interval [Å]: ", dlbd_mean.__format__('7.2f') loggfmin = -1 while nid > 100: crit = lgf_ll_set > loggfmin if not crit: break elt_ll_set = pl.compress(crit, elt_ll_set) lbd_ll_set = pl.compress(crit, lbd_ll_set) lgf_ll_set = pl.compress(crit, lgf_ll_set) nid = lbd_ll_set.size dlbd_ll_set = lbd_ll_set[1:] - lbd_ll_set[0:nid - 1] dlbd_mean = pl.mean(dlbd_ll_set) if bla: print "Number of line with log gf >", loggfmin, "to display:", nid print "Number of identification/Å:", nid / lbdrange print "Mean interval [Å]:", dlbd_mean loggfmin = loggfmin + 0.2 return elt_ll_set, lbd_ll_set, dlbd_mean
def select_data(i, data, lbdmin, lbdmax, obslist, lbdunitlist, hshiftlist): '''Selection of spectrum in a given wavelength range ''' bla = False global p # Case where wavelength are not in Angstrom but in nm if lbdunitlist[i] in ['a', 'Å', 'A', '', None]: pass elif lbdunitlist[i] == 'nm': data[:, 0] = data[:, 0] * 10 else: print "Wavelength unit unknown. Try Å|A|a or nm." quit(1) if hshiftlist[i] in [None, '']: hshift = 0.0 else: hshift = float(hshiftlist[i]) data[:, 0] = data[:, 0] + hshift crit = pl.logical_and(data[:, 0] <= lbdmax, data[:, 0] >= lbdmin) if not crit.any(): # Case of 1 spectrum only without any idea of wavelength unit #if len(obslist) == 1: data[:, 0] = data[:, 0] * 10 crit = pl.logical_and(data[:, 0] <= lbdmax, data[:, 0] >= lbdmin) print "wavelenght of the spectrum are outside the selection range." print "Maybe wavelengths are in nm ... convert wavelengths in Å and try again..." if not crit.any(): # print "Wavelength range outside the spectrum." # quit(1) # Skip this spectrum #else: print "Required (or default) wavelength range outside of this spectrum." print " Number of wavelength points:", len( data[:, 0]).__format__('11g') print " Lambda min: ", min(data[:, 0] / 10).__format__('9.3f') print " Lambda max: ", max(data[:, 0] / 10).__format__('9.3f') return None, None, p if bla: print " Number of wavelength points:", len(data[:, 0]).__format__('11g') print " Lambda min: ", min(data[:, 0]).__format__('9.3f') print " Lambda max: ", max(data[:, 0]).__format__('9.3f') x = pl.compress(crit, data[:, 0]) y = pl.compress(crit, data[:, 1]) # Case where there is a third column in input ASCII data considered as the absolute flux if abs_flux: try: y = pl.compress(crit, data[:, 2]) except: pass if bla: print " Number of selected points: ", len(x).__format__('11g') print " Selected lambda min: ", min(x).__format__('9.3f') print " Selected lambda max: ", max(x).__format__('9.3f') if hshift != 0.0: print " Including a shift of: ", hshift.__format__('9.3f') #Flag to plot spectra p = True return x, y, p
def select_ll(llfile, lbdmin, lbdmax, lbdrange): '''Select lines to show on the plot by vertical lines ''' bla = False try: llfile = pl.loadtxt(llname, dtype='str', comments='#', delimiter='\n') except IOError: print "Linelist file does not exist." quit(1) #return None, None, None elt_ll = [line[0:9] for line in llfile] lbd_ll = [float(line[9:18]) for line in llfile] try: lgf_ll = [float(line[18:]) for line in llfile] except: lgf_ll = None elt_ll = pl.asarray(elt_ll) lbd_ll = pl.asarray(lbd_ll) lgf_ll = pl.asarray(lgf_ll) crit = pl.logical_and(lbd_ll <= lbdmax, lbd_ll >= lbdmin) elt_ll_set = pl.compress(crit, elt_ll) lbd_ll_set = pl.compress(crit, lbd_ll) try: lgf_ll_set = pl.compress(crit, lgf_ll) except: lgf_ll_set = None nid = lbd_ll_set.size dlbd_ll_set = lbd_ll_set[1:] - lbd_ll_set[0:nid-1] dlbd_mean = pl.mean(dlbd_ll_set) if bla: print "" print " Number of line identification in the selected range:", nid print " Number of identification/Å:", (nid/lbdrange).__format__('7.2f') print " Mean interval [Å]: ", dlbd_mean.__format__('7.2f') loggfmin = -1 while nid > 100: crit = lgf_ll_set > loggfmin if not crit: break elt_ll_set = pl.compress(crit, elt_ll_set) lbd_ll_set = pl.compress(crit, lbd_ll_set) lgf_ll_set = pl.compress(crit, lgf_ll_set) nid = lbd_ll_set.size dlbd_ll_set = lbd_ll_set[1:] - lbd_ll_set[0:nid-1] dlbd_mean = pl.mean(dlbd_ll_set) if bla: print "Number of line with log gf >", loggfmin, "to display:", nid print "Number of identification/Å:", nid/lbdrange print "Mean interval [Å]:", dlbd_mean loggfmin = loggfmin + 0.2 return elt_ll_set, lbd_ll_set, dlbd_mean
def fit(psp_shape, time, voltage, error_estimate, maxcall=1000, maximal_red_chi2=2.0, fail_on_negative_cov=None): """ psp_shape : object PSPShape instance time : numpy.ndarray of floats numpy array of data acquisition times voltage : numpy.ndarray numpy array of voltage values error_estimate : float estimate for the standard deviation of an individual data point. maxcall : int maximal number of calls to the fit routine fail_on_negative_cov : list of int returns : tuple (fit_results error_estimates chi2_per_dof success) """ assert len(time) == len(voltage) initial_values = psp_shape.initial_fit_values(time, voltage) result = optimize.leastsq( lambda param: (psp_shape(time, *param) - voltage), [initial_values[key] for key in psp_shape.parameter_names()], full_output=1, maxfev=maxcall) resultparams, cov_x, _, _, ier = result ndof = len(time) - len(psp_shape.parameter_names()) fit_voltage = psp_shape(time, *result[0]) red_chi2 = sum(((fit_voltage - voltage)) ** 2) \ / (error_estimate ** 2 * ndof) fail_neg = p.any(p.diag(cov_x) < 0) if fail_on_negative_cov is not None: fail_neg = p.any(p.logical_and( p.diag(cov_x) < 0, fail_on_negative_cov)) cov_x *= error_estimate**2 success = ((not fail_neg) and (ier in [1, 2, 3, 4]) and (red_chi2 <= maximal_red_chi2)) processed, processed_cov = psp_shape.process_fit_results( resultparams, cov_x) return processed, processed_cov, red_chi2, success
# resample zwift power onto edge CrossPlotFig = plt.figure() sc = plt.scatter(edge_power_x, zwift_power_r_r, s=5, c=base_t, \ cmap=plt.get_cmap('brg'), edgecolors='face' ) plt.colorbar(orientation='horizontal') plt.title('Infocrank Vs PowerTap P1 Over Time (sec)\n(delay removed)') plt.xlabel('PowerTap P1 (w)') plt.ylabel('Infocrank via Zwift (w)') plt.grid(b=True, which='major', axis='both') a = plt.axis() plt.axis([0, a[1], 0, a[3]]) plt.show() # # linear regression # from pylab import polyfit, average, ones, where, logical_and, nonzero ii = nonzero( logical_and( base_t>=0, \ logical_and(edge_power_x>50, \ edge_power_x<1000) )) x = edge_power_x[ii] y = zwift_power_r_r[ii] coef = polyfit(x, y, deg=1) slope = coef[0] offset = coef[1] print 'slope = %5.3f, offset = %i' % (slope, offset) y_fit = slope * x + offset color = average(edge_t[ii]) * ones(len(edge_t[ii])) plt.plot(x, y_fit, 'k-') plt.show()