def detect_edges(im, detection_threshold=0.6, do_dilation=False, overlay=True): edges = (pylab.absolute(image_derivative(im, 'x', False)) > detection_threshold) | \ (pylab.absolute(image_derivative(im, 'y', False)) > detection_threshold) if do_dilation: edges = ndimage.binary_dilation(edges) if overlay: # overlay edges in a color im2 = nxmx3(im) im2[edges] = [1, .2, .2] edges = im2 return edges
def _power(self): if not self._stft(): return False fp = self._check_feature_params() self.POWER = (P.absolute(self.STFT)**2).sum(0) self._have_power = True if self.verbosity: print("Extracted POWER") self.X = self.POWER return True
def rank_by_distance_bhatt(self, qkeys, ikeys, rkeys, dists): """ :: Reduce timbre-channel distances to ranks list by ground-truth key indices Bhattacharyya distance on timbre-channel probabilities and Kullback distances """ # timbre-channel search using pre-computed distances ranks_list = [] t_keys, t_lens = self.get_adb_lists(0) rdists = pylab.ones(len(t_keys)) * float('inf') qk = self._get_probs_tc(qkeys) for i in range(len(ikeys[0])): # number of include keys ikey = [] dk = pylab.zeros(self.timbre_channels) for t_chan in range(self.timbre_channels): # timbre channels ikey.append(ikeys[t_chan][i]) try: # find dist of key i for query i_idx = rkeys[t_chan].index( ikey[t_chan]) # dataset include-key match # the reduced distance function in include_keys order # distance is Bhattacharyya distance on probs and dists dk[t_chan] = dists[t_chan][i_idx] except: print("Key not found in result list: ", ikey, "for query:", qkeys[t_chan]) raise error.BregmanError() rk = self._get_probs_tc(ikey) a_idx = t_keys.index(ikey[0]) # audiodb include-key index rdists[a_idx] = distance.bhatt( pylab.sqrt(pylab.absolute(dk)), pylab.sqrt(pylab.absolute(qk * rk))) #search for the index of the relevant keys rdists = pylab.absolute(rdists) sort_idx = pylab.argsort(rdists) # Sort fields into database order for r in self.ground_truth: # relevant keys ranks_list.append(pylab.where( sort_idx == r)[0][0]) # Rank of the relevant key return ranks_list, rdists
def _cqft_intensified(self): """ :: Constant-Q Fourier transform using only max abs(STFT) value in each band """ if not self._have_stft: if not self._stft(): return False self._make_log_freq_map() r, b = self.Q.shape b, c = self.STFT.shape self.CQFT = P.zeros((r, c)) for i in P.arange(r): for j in P.arange(c): self.CQFT[i, j] = (self.Q[i, :] * P.absolute(self.STFT[:, j])).max() self._have_cqft = True self._is_intensified = True self.inverse = self.icqft self.X = self.CQFT return True
def _cqft(self): """ :: Constant-Q Fourier transform. """ if not self._power(): return False fp = self._check_feature_params() if self.intensify: self._cqft_intensified() else: self._make_log_freq_map() self.CQFT = P.sqrt( P.array(P.mat(self.Q) * P.mat(P.absolute(self.STFT)**2))) self._is_intensified = False self._have_cqft = True if self.verbosity: print("Extracted CQFT: intensified=%d" % self._is_intensified) self.inverse = self.icqft self.X = self.CQFT return True
def rank_by_distance_avg(self, qkeys, ikeys, rkeys, dists): """ :: Reduce timbre-channel distances to ranks list by ground-truth key indices Kullback distances """ # timbre-channel search using pre-computed distances ranks_list = [] t_keys, t_lens = self.get_adb_lists(0) rdists = pylab.ones(len(t_keys)) * float('inf') for t_chan in range(self.timbre_channels): # timbre channels t_keys, t_lens = self.get_adb_lists(t_chan) for i, ikey in enumerate(ikeys[t_chan]): # include keys, results try: # find dist of key i for query i_idx = rkeys[t_chan].index( ikey) # lower_bounded include-key index a_idx = t_keys.index(ikey) # audiodb include-key index # the reduced distance function in include_keys order # distance is the sum for now if t_chan: rdists[a_idx] += dists[t_chan][i_idx] else: rdists[a_idx] = dists[t_chan][i_idx] except: print("Key not found in result list: ", ikey, "for query:", qkeys[t_chan]) raise error.BregmanError() #search for the index of the relevant keys rdists = pylab.absolute(rdists) sort_idx = pylab.argsort(rdists) # Sort fields into database order for r in self.ground_truth: # relevant keys ranks_list.append(pylab.where( sort_idx == r)[0][0]) # Rank of the relevant key return ranks_list, rdists
def feature_plot(self, feature=None, normalize=False, dbscale=False, norm=False, interp='nearest', labels=True, nofig=False, **kwargs): """ :: Plot the given feature, default is self.feature, returns an error if feature not extracted Inputs: feature - the feature to plot self.feature features are extracted in the following hierarchy: stft->cqft->mfcc->[lcqft,hcqft]->chroma, if a later feature was extracted, then an earlier feature can be plotted normalize - column-wise normalization ['alse] dbscale - transform linear power to decibels: 20*log10(X) [False] norm - make columns unit norm [False] interp - how to interpolate values in the plot ['nearest'] labels - whether to plot labels nofig - whether to make new figure **kwargs - keyword arguments to imshow or plot """ feature = self._check_feature_params( )['feature'] if feature is None else feature # check plots if feature == 'stft': if not self._have_stft: print("Error: must extract STFT first") else: feature_plot(P.absolute(self.STFT), normalize, dbscale, norm, title_string="STFT", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) self._feature_plot_yticks( float(self.sample_rate) / (self.nfft)) P.xlabel('Time (secs)') P.ylabel('Frequency (Hz)') elif feature == 'power': if not self._have_power: print("Error: must extract POWER first") else: if not nofig: P.figure() P.plot(feature_scale(self.POWER, normalize, dbscale) / 20.0) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) P.title("Power") P.xlabel("Time (s)") P.ylabel("Power (dB)") elif feature == 'cqft': if not self._have_cqft: print("Error: must extract CQFT first") else: feature_plot(self.CQFT, normalize, dbscale, norm, title_string="CQFT", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) #self._feature_plot_yticks(1.) P.yticks(P.arange(0, self._cqtN, self.nbpo), (self.lo * 2**(P.arange(0, self._cqtN, self.nbpo) / self.nbpo)).round(1)) P.xlabel('Time (secs)') P.ylabel('Frequency (Hz)') elif feature == 'mfcc': if not self._have_mfcc: print("Error: must extract MFCC first") else: fp = self._check_feature_params() X = self.MFCC[self.lcoef:self.lcoef + self.ncoef, :] feature_plot(X, normalize, dbscale, norm, title_string="MFCC", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) P.xlabel('Time (secs)') P.ylabel('Cepstral coeffient') elif feature == 'lcqft': if not self._have_lcqft: print("Error: must extract LCQFT first") else: feature_plot(self.LCQFT, normalize, dbscale, norm, title_string="LCQFT", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) P.yticks(P.arange(0, self._cqtN, self.nbpo), (self.lo * 2**(P.arange(0, self._cqtN, self.nbpo) / self.nbpo)).round(1)) elif feature == 'hcqft': if not self._have_hcqft: print("Error: must extract HCQFT first") else: feature_plot(self.HCQFT, normalize, dbscale, norm, title_string="HCQFT", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) P.yticks(P.arange(0, self._cqtN, self.nbpo), (self.lo * 2**(P.arange(0, self._cqtN, self.nbpo) / self.nbpo)).round(1)) P.xlabel('Time (secs)') P.ylabel('Frequency (Hz)') elif feature == 'chroma' or feature == 'hchroma': if not self._have_chroma: print("Error: must extract CHROMA first") else: feature_plot(self.CHROMA, normalize, dbscale, norm, title_string="CHROMA", interp=interp, nofig=nofig, **kwargs) if labels: self._feature_plot_xticks( float(self.nhop) / float(self.sample_rate)) P.yticks(P.arange(0, self.nbpo, self.nbpo / 12.), [ 'C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B' ]) P.xlabel('Time (secs)') P.ylabel('Pitch Class') else: print("Unrecognized feature, skipping plot: ", feature)
def __get_data(caltable, xaxis, yaxis, iteration, poln, field, antenna, spw, timerange): antenna_list = __get_antennas(caltable) fields_list = __get_table(caltable, 'FIELD') spw_list = __get_table(caltable, 'SPECTRAL_WINDOW') caltype = __get_caltype(caltable) tb.open(caltable) timecol = tb.getcol('TIME') antcol = tb.getcol('ANTENNA1') spwcol = tb.getcol('SPECTRAL_WINDOW_ID') fieldcol = tb.getcol('FIELD_ID') column = 'CPARAM' if "CPARAM" in tb.colnames() else "FPARAM" paramcol = tb.getcol(column) tb.close() # Select data; would for loop + numba be preferred for large tables? rows = pl.where(pl.isin(fieldcol, field))[0] rows = rows[pl.where(pl.isin(spwcol[rows], spw))[0]] rows = rows[pl.where(pl.isin(antcol[rows], antenna))[0]] rows = rows[pl.where( pl.logical_and(timecol[rows] >= timerange[0], timecol[rows] <= timerange[1]))[0]] # Key and Label templates for each subfigure, iteration determines the number of subfigures # iteratation = field, antenna, or spw figtempl = "" fighdrs = [] iterset = set(it.upper() for it in iteration.split(',')) #print('iteration="{}", len(iterset)="{}", iterset="{}"'.format(iteration, len(iterset), iterset)) for it in iterset: if it == "": continue elif it == "ANTENNA": figtempl += "A{1}" fighdrs.append("Antenna = {1}") elif it == "SPW": figtempl += "S{2}" fighdrs.append("Spw = {2}") elif it == "FIELD": figtempl += "F{3}" fighdrs.append("Field = {3}") else: print("Error, invalid iteration parameter:", it) raise ValueError() fighdrtempl = ", ".join(fighdrs) # Label and key template for each plot #plotcols = {"TIME", "ANTENNA", "SPW", "FIELD"} - iterset plotcols = {"ANTENNA", "SPW", "FIELD"} - iterset if "TIME" in (xaxis, yaxis): plotcols -= {"TIME"} if "CHANNEL" in (xaxis, yaxis): plotcols -= {"SPW"} if "ANTENNA" in (xaxis, yaxis): plotcols -= {"ANTENNA"} plottempl = "" plothdrs = [] for pc in plotcols: if pc == "TIME": plottempl += "T{0}" plothdrs.append("{0}") elif pc == "ANTENNA": plottempl += "A{1}" plothdrs.append("{1}") elif pc == "SPW": plottempl += "S{2}" plothdrs.append("{2}") elif pc == "FIELD": plottempl += "F{3}" plothdrs.append("{3}") plothdrtempl = ", ".join(plothdrs) #field,spw,antenna,pol,time / val plotdata = {} for row in rows: r = (timecol[row], antcol[row], spwcol[row], fieldcol[row]) rlabel = (__casa2datetime(r[0]), antenna_list[r[1]], r[2], fields_list[r[3]]['NAME']) figkey = figtempl.format(*r) try: f = plotdata[figkey] except KeyError: label = fighdrtempl.format(*rlabel) f = {"label": label} plotdata[figkey] = f plotkey = plottempl.format(*r) try: p = f[plotkey] except KeyError: label = plothdrtempl.format(*rlabel) p = ([], [], label) # X, Y, Label f[plotkey] = p #print('figkey = \"{}\" ; plotkey = {}; label = {}'.format(figkey, plotkey, label)) for pol in poln: for i, ax in enumerate((xaxis, yaxis)): # Should be possible to do more efficient if ax == "TIME": p[i].append(rlabel[0]) elif ax == "ANTENNA": p[i].append(r[1]) elif ax == "CHAN": nchan = spw_list[r[2]]['NUM_CHAN'] p[i].extend([ch for ch in range(nchan)]) elif ax == "FREQ": p[i].extend(spw_list[r[2]]['CHAN_FREQ']) elif ax == "DELAY": if caltype == "FRINGE": p[i].append(paramcol[pol * 4 + 1, 0, row]) else: p[i].append(paramcol[pol * 4, 0, row]) elif ax == "RATE": if caltype == "FRINGE": p[i].append(paramcol[pol * 4 + 2, 0, row]) else: print( "Error: Rate is not available in calibration table" ) raise ValueError() elif ax == "DISP": if caltype == "FRINGE": p[i].append(paramcol[pol * 4 + 3, 0, row]) else: print( "Error: Dispersive delay is not available in calibration table" ) raise ValueError() elif ax == "AMP": p[i].extend(pl.absolute(paramcol[pol, :, row])) elif ax == "PHASE": if caltype == "FRINGE": v = paramcol[pol * 4, :, row] else: v = paramcol[pol, :, row] p[i].extend( pl.arctan2(pl.imag(v), pl.real(v)) * 180 / pl.pi) elif ax == "TSYS": if caltype == "EVLASWPOW": p[i].append(paramcol[pol * 4 + 1], 0, row) else: p[i].append(paramcol[pol, 0, row]) else: #print(len(spw_list)) #print(spw_list) #print(paramcol.shape) print("Error: unknown axis ", ax) raise ValueError() # Convert data to sorted numpy arrays for figkey, fig in plotdata.items(): for plotkey, data in fig.items(): if (plotkey == "label"): continue x = pl.array(data[0]) y = pl.array(data[1]) srt = x.argsort() fig[plotkey] = [x[srt], y[srt], data[2]] return plotdata