Example #1
0
def loadFile(path, sr=16000, time=3.0):
    spectrogram = Spectrogram(input_shape=(int(time * sr), 1), sr=sr)
    X = []
    curr = 0  # indicates 1st and last file of speaker
    speaker_dict = {}

    print('Total speakers:', len(os.listdir(path)))
    c = 0

    for speaker in os.listdir(path):
        if c % 10 == 0:
            print('Speakers done:', c)
        c += 1
        print('Loading speaker: ' + speaker)
        speaker_path = os.path.join(path, speaker)
        speaker_dict[speaker] = [curr, None]
        X_speaker = []

        for file_path in os.listdir(speaker_path):
            files = os.path.join(speaker_path, file_path)
            audio, rate = librosa.load(files, sr=sr)
            audio = np.expand_dims(audio, axis=1)

            spec = spectrogram.spectrogram(audio)
            X_speaker.append(spec)
            curr += 1

        speaker_dict[speaker][1] = curr - 1
        X.append(X_speaker)

    return X, speaker_dict
def saveBaselineIntensityImages(files,
                                saveLoc: str = None,
                                imageExt: str = "png"):
    """
    Save a image file of the baseline as a function of time to the folder specified by saveLoc.
    """
    if saveLoc == None:
        saveLoc = r"../baselineIntensityMaps"

    for i in tqdm.trange(len(files)):
        filename = f"../dig/{files[i]}"
        MySpect = Spectrogram(filename)
        peaks, _, heights = baselines_by_squash(MySpect)

        plt.plot(
            np.array(MySpect.time) * 1e6,
            MySpect.intensity[MySpect._velocity_to_index(peaks[0])])
        plt.xlabel("Time ($\mu$s)")
        plt.ylabel("Intensity (db)")
        plt.title(f"{MySpect.data.filename}")
        path = os.path.join(
            saveLoc,
            files[i].replace(".dig", "") + f"_baselineIntensity.{imageExt}")
        if not os.path.exists(os.path.split(path)[0]):
            os.makedirs(os.path.split(path)[0])

        plt.savefig(path)

        # Reset the state for the next data file.
        plt.clf()
        del MySpect
Example #3
0
    def create_plot(self):
        if self.plot is not None:
            self.plot.close()

        self.toolbar.set_auto(True)

        if self.settings.display == Display.PLOT:
            self.plot = Plotter(self.notify, self.figure, self.settings)
        elif self.settings.display == Display.SPECT:
            self.plot = Spectrogram(self.notify, self.figure, self.settings)
        else:
            self.plot = Plotter3d(self.notify, self.figure, self.settings)

        self.set_fonts()

        self.toolbar.set_plot(self.plot)
        self.toolbar.set_type(self.settings.display)
        self.measureTable.set_type(self.settings.display)

        self.set_plot_title()
        self.figure.tight_layout()
        self.figure.subplots_adjust(top=0.85)
        self.redraw_plot()
        self.plot.scale_plot(True)
        self.mouseZoom = MouseZoom(self.plot, self.toolbar, self.hide_overlay)
        self.mouseSelect = MouseSelect(self.plot, self.on_select,
                                       self.on_selected)
        self.measureTable.show(self.settings.showMeasure)
        self.panel.SetFocus()
def createGallery(digsToLookAt: list = None,
                  colormap="blue-orange-div",
                  fileext="jpeg",
                  transformData=False):
    if type(digsToLookAt) == type(None):
        digsToLookAt = DigFile.inventory(justSegments=True)['file']

    digDir = DigFile.dig_dir()
    imageDir, _ = os.path.split(digDir)
    imageFold = "ImageGallery"
    imageDir = os.path.join(imageDir, imageFold)
    print(digsToLookAt)
    for i in range(len(digsToLookAt)):
        filename = digsToLookAt[i]
        print(filename)
        spec = Spectrogram(os.path.join(digDir, filename))
        pcms, lastAxes = spec.plot(transformData, cmap=colormap)
        fileloc, filename = os.path.split(
            os.path.splitext(os.path.join(imageDir, filename))[0])
        fileloc = os.path.join(fileloc, filename)

        for key in pcms.keys():
            if "complex" in key:
                continue  # There is not a graph with a name that contains complex.
            if not os.path.exists(fileloc):
                os.makedirs(fileloc)

            plt.figure(num=key)  # Get to the appropriate figure.
            plt.savefig(
                os.path.join(fileloc, filename) +
                f' {key} spectrogram.{fileext}')
            plt.clf()
        del spec
def baselineTracking(spectrogram: Spectrogram,
                     baselineVel,
                     changeThreshold,
                     skipUntilTime: float = 12e-6):
    """
        Return the first time in microseconds that baseline's intensity value changes outside the changeThreshold.
        Use the average baseline value as the estimate
    """

    baselineInd = spectrogram._velocity_to_index(baselineVel)
    runningAverage = spectrogram.intensity[baselineInd][
        spectrogram._time_to_index(spectrogram.t_start + skipUntilTime)]

    startInd = spectrogram._time_to_index(spectrogram.t_start + skipUntilTime)
    for ind in range(startInd, spectrogram.intensity.shape[-1]):
        currentIntensity = spectrogram.intensity[baselineInd][ind]
        if (np.abs(currentIntensity) >= np.abs(
            (1 + changeThreshold) * runningAverage)) or (
                np.abs(currentIntensity) <= np.abs(
                    (1 - changeThreshold) * runningAverage)):
            # print(f"{changeThreshold}: {spectrogram.time[ind]*1e6}")
            return spectrogram.time[ind] * 1e6
        runningAverage += 1 / (ind + 1 - startInd) * \
            (currentIntensity - runningAverage)

    return spectrogram.time[ind] * 1e6
Example #6
0
    def __init__(self, weight_file, params_file):
        """Performs detection on an audio file.
        The structure of the network is hard coded to a network with 2
        convolution layers with pooling, 1 or 2 fully connected layers, and a
        final softmax layer.

        weight_file is the path to the numpy weights to the network
        params_file is the path to the network parameters
        """

        self.weights = np.load(weight_file, encoding='bytes', allow_pickle=True)
        if not all([weight.dtype == np.float32 for weight in self.weights]):
            for i in range(self.weights.shape[0]):
                self.weights[i] = self.weights[i].astype(np.float32)

        with open(params_file, 'rb') as fp:
            params = pickle.load(fp)

        self.chunk_size = 4.0  # seconds
        self.win_size = params['win_size']
        self.max_freq = params['max_freq']
        self.min_freq = params['min_freq']
        self.slice_scale = params['slice_scale']
        self.overlap = params['overlap']
        self.crop_spec = params['crop_spec']
        self.denoise = params['denoise']
        self.smooth_spec = params['smooth_spec']
        self.nms_win_size = int(params['nms_win_size'])
        self.smooth_op_prediction_sigma = params['smooth_op_prediction_sigma']
        self.sp = Spectrogram()
Example #7
0
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()

        self.sp = Spectrogram()

        self.file_pth = file_pth
        self.train = train
        self.random_state = None

        self.train = self.read_data()
Example #8
0
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()

        self.sp = Spectrogram()

        self.file_pth = file_pth
        self.train = train
        self.random_state = None

        self.data = self.read_data()
        datasets = train_test_split(self.data)
        if train:
            self.train = datasets[0]
        else:
            # actually this is test dataset
            self.train = datasets[1]
Example #9
0
def make_spectrogram(pipe: PNSPipe, **kwargs):
    """
    Compute a spectrogram using the passed kwargs or
    built-in defaults and set the pipe.spectrogram field.
    Default values are:
      wavelength = 1.55e-6
      points_per_spectrum = 4096
      overlap = 7/8
      window_function = None
      form = 'power'
    """
    from spectrogram import Spectrogram

    defaults = dict(
        t_start=kwargs.get('t_start'),
        ending=kwargs.get('ending'),
        wavelength=kwargs.get('wavelength', 1.55e-6),
        points_per_spectrum=kwargs.get('points_per_spectrum', 4096),
        overlap=kwargs.get('overlap', 7 / 8),
        window_function=kwargs.get('window_function'),
        form=kwargs.get('form', 'power'),
    )
    allargs = dict({**defaults, **kwargs})
    # list the arguments we use in the log
    for k, v in allargs.items():
        pipe.log(f"+ {k} = {v}")

    pipe.spectrogram = Spectrogram(pipe.df, **allargs)
def mainTest(SpectrogramObject:Spectrogram, startTime:int, stopTime:int, signalJump:int=50, bottomIndex:int=0, topIndex:int=None, vStartInd: int=None):
    widths = [1,3,5,11,21]
    orders = [1,2,10]
    seam = []

    velocities = SpectrogramObject.velocity
    time = SpectrogramObject.time
    for width in widths:
        for order in orders:
            signal, p_table, dp_table, botVel, topVel = seamExtraction(SpectrogramObject, startTime, stopTime, width, signalJump, bottomIndex, topIndex, order)

            plt.plot(velocities[botVel:topVel+1], dp_table[0])
            plt.title("Total Minkowski" + str(order) +" Order Cost diagram with a window size of " +str(width) +" raw")
            plt.xlabel("Starting velocity of the trace (m/s)")
            plt.ylabel("Minimum value of the sum (|p_i - p_{i-1}|^" + str(order) + ") along the path")
            plt.show()

            print("Here is a graph of the signal trace across time")       

            plt.figure(figsize=(10, 6))
            myplot = SpectrogramObject.plot()
            plt.xlim((SpectrogramObject.time[startTime], SpectrogramObject.time[stopTime]))

            myplot.plot(time[startTime: stopTime+1], velocities[signal], 'b-', alpha = 0.4, label="Minimum Cost")
            if vStartInd != None:
                # Compute the signal seam for assuming this is the start point.
                seam = reconstruction(p_table, vStartInd-botVel, botVel)
                myplot.plot(time[startTime: stopTime+1], velocities[seam], 'r--', alpha = 0.4, label="Expected Start Point")
            myplot.title("Velocity as a function of time for the minimum cost seam with Minkowski" + str(order)+ " and a window size of " + str(width) + " raw")
            myplot.legend()
            myplot.show()
 def calc_spectrogram(self, **kwargs):
     if not hasattr(self, '_spectrogram'):
         self._spectrogram = Spectrogram.from_waveform(
             self.signal,
             **kwargs
         )
     return self._spectrogram
Example #12
0
class SampleDataset(Dataset):
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()

        self.sp = Spectrogram()

        self.file_pth = file_pth
        self.train = train
        self.random_state = None

        self.train = self.read_data()

    def __getitem__(self, index):

        # datasets[0][index][0]
        stage_layer, degc_layer = self.feature_layer(self.train[index][2])
        img = self.sp.spec_array(self.train[index][0])
        img = np.concatenate((img, stage_layer), axis=0)
        img = np.concatenate((img, degc_layer), axis=0)
        img = torch.tensor(img, dtype=torch.float32)
        target = self.train[index][1]
        target = torch.tensor(target)
        return img, target

    def __len__(self):
        return len(self.train)

    def feature_layer(self, features):

        stage = int(re.findall("\d+", features[0])[0])
        degc = float(features[1])

        stage_layer = np.full((224, 224), stage, dtype=np.int8)
        degc_layer = np.full((224, 224), degc, dtype=np.float)

        stage_layer = stage_layer[np.newaxis, :, :]
        degc_layer = degc_layer[np.newaxis, :, :]

        return stage_layer, degc_layer

    def read_data(self):
        datas = []
        #self.file = "./sample_data.txt"
        with open(self.file_pth, "r") as f:
            header = f.readline()
            while 1:
                line = f.readline()
                if not line:
                    break
                tmp = line.strip().split('\t')
                freq = list(map(float, tmp[4:]))
                features = tmp[2:4]
                # freq = list(map(float, tmp[1:]))
                # print(freq[-1])
                label = int(tmp[0])

                datas.append([freq, label, features])

        return datas
Example #13
0
def generate_graphs(directory):
    for file in os.listdir(directory):
        print(directory + file)
        if file.endswith(".dig"):
            # Then we have the right file.

            Spectrogram_Object = Spectrogram(directory + file)
            sampleSize = 256
            full_length_spectra = Spectrogram_Object.spectrogram(
                0, Spectrogram_Object.samples, sampleSize)

            colormaps = ["gist_stern", "tab20c", "tab20b", "terrain"]

            for colormap in colormaps:
                Spectrogram_Object.plot(full_length_spectra,
                                        sample_window=sampleSize,
                                        cmap=colormap)
def signal_finder(spectrogram: Spectrogram,
                  baseline: np.ndarray,
                  t_start: float,
                  dt: int = 2,
                  min_separation=200):  # m/s
    """
    Look above the baseline for a signal corresponding to a surface velocity.
    """
    from scipy.signal import find_peaks

    t_index = spectrogram._time_to_index(t_start)
    spectra = np.sum(spectrogram.intensity[:, t_index - dt:t_index + dt],
                     axis=1)

    try:
        # limit the region we look at to points above the baseline
        # get the index of the baseline
        blo = spectrogram._velocity_to_index(baseline[0]) + 5
        if len(baseline) > 1:
            bhi = spectrogram._velocity_to_index(baseline[1]) - 5
        else:
            bhi = len(spectrogram.velocity) - 1
        velocity = spectrogram.velocity[blo:bhi]
        spectrum = spectra[blo:bhi]
        smax = spectrum.max()
        min_sep = int(min_separation / spectrogram.dv)
        peaks, properties = find_peaks(spectrum,
                                       height=0.05 * smax,
                                       distance=min_sep)
        heights = properties['peak_heights']
        # produce an ordering of the peaks from high to low
        ordering = np.flip(np.argsort(heights))
        peak_index = peaks[ordering]
        peaks = velocity[peak_index]
        hts = heights[ordering]

        # normalize to largest peak of 1 (e.g., the baseline)
        hts = hts / hts[0]

        return peaks[0]
    except Exception as eeps:
        print(eeps)
        return None
def analyze_region(spectrogram: Spectrogram, roi: dict):
    """

    """
    time, velocity, intensity = spectrogram.slice(roi['time'], roi['velocity'])
    # Do we want to convert to power?
    power = spectrogram.power(intensity)
    # let's look at the peak power at each time
    peaks = np.max(power, axis=0)
    speaks = sorted(peaks)
    threshold = speaks[int(len(peaks) * 0.04)]
    amax = np.argmax(power, axis=0)
    power[power < threshold] = 0.0

    span = 30
    nmax = len(velocity) - 1
    times, centers, widths, amps = [], [], [], []

    def peg(x):
        if x < 0:
            return 0
        if x > nmax:
            return nmax
        return x

    for t in range(len(time) - 1):
        if power[amax[t], t] > threshold:
            acenter = amax[t]
            vfrom, vto = peg(acenter - span), peg(acenter + span)
            gus = Gaussian(velocity[vfrom:vto], power[vfrom:vto, t])
            if gus.valid:
                times.append(time[t])
                centers.append(gus.center)
                widths.append(gus.width)
                amps.append(gus.amplitude)
            else:
                print(f"[{t}] {gus.error}")

    #power[power>=threshold] = 1
    plt.pcolormesh(time * 1e6, velocity, power)
    plt.plot(np.array(times) * 1e6, centers, 'r.', alpha=0.5)
    plt.show()
Example #16
0
 def calc_spectrogram(self,
                      step_size=None,
                      fft_resolution=None,
                      scale_factors=None):
     self._spectrogram = Spectrogram.from_waveform(
         self.signal,
         step_size=step_size,
         fft_resolution=fft_resolution,
     )
     if scale_factors is not None:
         self._spectrogram.data = self._spectrogram.scale(scale_factors)
     return self._spectrogram
Example #17
0
    def __init__(self,
                 spectrogram: Spectrogram,
                 start_point: tuple,
                 span: int = 80,
                 rotate: bool = False,
                 weightingVector: dict = {},
                 debug=False):
        """
            Input:
                spectrogram: The object that we would like to trace a signal in.
                start_point: (t, v) The actual time and velocity values (not the indices)
                span: The number of velocity values up and down that you will check at the next time step.
                rotate: whether or not you want to use linear extrapolation and rotation at each time step.
                weightingVector: the weighting vector dictionary used to combine the available data into one matrix.
                    Values must sum to one. The default is {intensity: 1} meaning to use only the intensity data. 
                    It will be in the same order as the availableData.
                    If all the modes have been computed availableData will be
                        ['magnitude', 'angle', 'phase', 'real', 'imaginary', 'intensity']
                debug: Do you want to print debugging statements?
        """
        self.spectrogram = spectrogram
        self.t_start, self.v_start = start_point
        self.span = span
        self.rotate = rotate
        self.weightingVector = weightingVector if (
            weightingVector != {}) else {
                "intensity": 1
            }
        self.debug = debug
        if self.rotate:
            raise NotImplementedError(
                "The rotate capacity for peak follower is not implemented.")

        # Now establish storage for intermediate results and
        # state. time_index is the index into the spectrogram.intensity
        # along the time axis.
        self.time_index = spectrogram._time_to_index(self.t_start)

        self.results = dict(
            velocity_index_spans=[],  # the range of points used for the fit
            times=[],  # the times of the fits
            time_index=[],  # the corresponding point index in the time dimension
            velocities=[],  # the fitted center velocities
            intensities=[]  # the peak intensity
        )
        # for convenience, install pointers to useful fields in spectrogram
        self.velocity = self.spectrogram.velocity
        self.time = self.spectrogram.time
        self.intensity = self.spectrogram.intensity

        self.dV = self.velocity[1] - self.velocity[0]
        self.dT = self.time[1] - self.time[0]
    def get_data(self):

        print('Reading Data...')

        file_list = os.listdir(self.params['data_path'])
        file_list = [
            os.path.join(self.params['data_path'], i) for i in file_list[:10]
        ]

        x_data = Spectrogram(filenames=file_list)

        self.x_train, self.x_test = train_test_split(x_data.spectrogram,
                                                     test_size=0.3)
Example #19
0
    def __init__(self, user_dir, contains_vocals, n_samples):
        os.chdir(user_dir)
        # The line below makes a list of all the filenames in the specified directory,
        # while excluding names of subdirectories/folders
        self.filename_list = [name for name in os.listdir('.') if os.path.isfile(name)]
        print('Processing songs from:' + user_dir)
        # For test set, n_samples = 24505
        # For training set, n_samples = 46200
        freq_size = 513
        time_size = 23
        self.n_samples = n_samples
        # These are dimensions of the matrix [X, Y]

        self.X = np.zeros((self.n_samples, freq_size, time_size,1), dtype=int)
        self.Y = np.zeros((self.n_samples,1), dtype=int)
        # This will ensure we are indexing correctly to put the x for our sample into our X matrix of all songs' data
        n_samples_so_far = 0
        for i in range(len(self.filename_list)):
            filename = self.filename_list[i]  # Iterates through each file name in the list
            print(filename)

            self.contains_vocals = contains_vocals
            spect = Spectrogram(filename, contains_vocals)
            x_is = spect.get_X()
            y_is = spect.get_Y()
            #print("x_is"+str(x_is.shape))
            #print("y_is"+str(y_is.shape))
            #print("samples so far "+str(n_samples_so_far))
            
            n_song_samples = np.shape(y_is)[0] # I.e., how many samples did we make from the song
                                               # (song length (sec)*2, since 500ms segments used)

            upper_index = n_samples_so_far + n_song_samples  # Each sample is a row in our array/matrix
            #print("upper index "+str(upper_index))
            self.X[n_samples_so_far:upper_index, :, :,:] = x_is
            self.Y[n_samples_so_far:upper_index,:] = y_is

            n_samples_so_far = n_samples_so_far + n_song_samples  # Updates how many samples we've done so far
Example #20
0
File: main.py Project: wsy8029/nvh
class TestDataset(Dataset):
    
    def __init__(self, file_pth: str, train=True, random_state=None):
        super(Dataset, self).__init__()
        
        self.sp = Spectrogram()
        
        self.file_pth = file_pth
        self.train = train
        self.random_state = None
        
        self.data = self.read_data()
        datasets = train_test_split(self.data)
        if train:
            self.train = datasets[0]
        else:
            # actually this is test dataset
            self.train = datasets[1]
    
    def __getitem__(self, index):
        
        # datasets[0][index][0]
        img = self.sp.spec_array(self.train[index][0])
        img = torch.tensor(img, dtype=torch.float32)
        target = self.train[index][1]
        target = torch.tensor(target)
        return img, target
    
    def __len__(self):
        return len(self.train)
    
    def read_data(self):
        datas = []
        #self.file = "./sample_data.txt"
        
        with open(self.file_pth, "r") as f:
            header = f.readline()
            while 1:
                line = f.readline()
                if not line:
                    break
                tmp = line.strip().split('\t')
                # freq = list(map(float, tmp[4:]))
                freq = list(map(float, tmp[1:]))
                # print(freq[-1])
                label = int(tmp[0])
                
                datas.append([freq,label])
                
        return datas
    def test(self, filenames):
        filenames = filenames[5:15]
        for filename in filenames:
            print(filename)
            test_data = Spectrogram(filenames=[filename])
            decoded_spectrogram = self.network.model.predict(
                test_data.spectrogram)
            #print_summary(self.network.model, line_length=80)
            test_data.spectrogram_to_wav(
                spectrogram=copy.deepcopy(decoded_spectrogram),
                filename=filename.split("/")[-1])

            test_data.visualize(filename=filename,
                                spectrogram=decoded_spectrogram)
 def __init__(
         self,
         spectrogram: Spectrogram,
         start_point,  # either a tuple or a list of tuples
         max_acceleration=10000,
         smoothing=4,  # average this many points on each side
         max_hop=500,  # require peaks at successive time steps
         # to be within this many m/s
     direction=1,  # go forward in time (1), backward (-1)
 ):
     span = max_acceleration * spectrogram.dt * 1e6 / spectrogram.dv
     super().__init__(spectrogram, start_point, int(span))
     self.smoothing = smoothing
     self.max_hop = max_hop
     assert direction in (-1, 1), "Direction must be +1 or -1"
     self.direction = direction
     peaks, dv, heights = bline(spectrogram)
     # Let's take only the peaks greater than 10% of the strongest peak
     self.baselines = peaks[heights > 0.1]
     self.noise_level = spectrogram.noise_level() * 3
def runAgainstTestSet(testSetFilename,
                      guessAtOptimalThres,
                      guessAtOptimalSkipUntil,
                      errorPower: int = 1):

    data = pd.read_excel(testSetFilename)

    # Ignore the samples that we do not have the full data for.
    data = data.dropna()
    data.reset_index()  # Reset so that it can be easily indexed.

    files = data["Filename"].to_list()

    bestGuessTimes = (data[data.columns[1]] * 1e6).to_list()
    # This will be the same times for the probe destruction dataset. It is generally ignored currently.
    bestGuessVels = data[data.columns[-1]].to_list()

    d = {"Filename": files}

    d["ProbeDestructionEstimateTime"] = np.zeros(len(files))
    d["Error Versus Label"] = np.zeros(len(files))

    for i in tqdm.trange(len(files)):
        filename = os.path.join(os.path.join("..", "dig"), f"{files[i]}")
        MySpect = Spectrogram(filename, overlap=0)
        peaks, _, heights = baselines_by_squash(MySpect, min_percent=0.75)
        timeEstimate = baselineTracking(MySpect, peaks[0], guessAtOptimalThres,
                                        guessAtOptimalSkipUntil)
        d["ProbeDestructionEstimateTime"][i] = timeEstimate
        d["Error Versus Label"][i] = np.power(bestGuessTimes[i] - timeEstimate,
                                              errorPower)

    errors = d["Error Versus Label"]

    print(f"The statistics for the test set specified by {testSetFilename}\n")
    print(
        f"Avg: {np.mean(errors)} Std: {np.std(errors)} Median: {np.median(errors)} Max: {np.max(errors)} Min: {np.min(errors)}"
    )

    return d
 def __init__(self, spec: Spectrogram, time_chunk: int = 16, velocity_chunk: int = 16):
     self.spectrogram = spec
     self.time_chunk = time_chunk
     self.velocity_chunk = velocity_chunk
     powers = spec.power(spec.intensity)
     nrows, ncols = powers.shape
     nrows = nrows // velocity_chunk
     ncols = ncols // time_chunk
     self.intensity = ar = np.zeros((nrows, ncols))
     rows = np.linspace(0, nrows * velocity_chunk,
                        num=nrows + 1, dtype=np.uint32)
     cols = np.linspace(0, ncols * time_chunk,
                        num=ncols + 1, dtype=np.uint32)
     for row in range(nrows):
         rfrom, rto = rows[row], rows[row + 1]
         for col in range(ncols):
             ar[row, col] = np.mean(
                 powers[rfrom:rto, cols[col]:cols[col + 1]])
     self.time = spec.time[0:len(spec.time):time_chunk]
     self.velocity = spec.velocity[0:len(spec.velocity):velocity_chunk]
     vals = ar.flatten()
     vals.sort()
     self.spower = vals
     self.threshold = vals[int(len(vals) * 0.8)]
def setupForDocumentation():
    # Set up
    filename = "../dig/CH_4_009.dig"
    t1 = 14.2389/1e6
    t2 = 31.796/1e6
    MySpect = Spectrogram(filename, mode = "complex")
    sTime = MySpect._time_to_index(t1)
    eTime = MySpect._time_to_index(t2)

    bottomVel = 1906.38
    botInd = MySpect._velocity_to_index(bottomVel)

    topVel = 5000
    topVelInd = MySpect._velocity_to_index(topVel)

    visualSignalStart = 2652.21
    visSigIndex = MySpect._velocity_to_index(visualSignalStart)

    return MySpect, sTime, eTime, botInd, topVelInd, visSigIndex
def setupForDocumentation():
    # Set up
    filename = "../See_if_this_is_enough_to_get_started/CH_4_009.dig"
    t1 = 14.2389/1e6
    t2 = 31.796/1e6
    MySpect = Spectrogram(filename)
    sTime = MySpect._time_to_index(t1)
    eTime = MySpect._time_to_index(t2)

    bottomVel = 1906.38
    botInd = MySpect._velocity_to_index(bottomVel)

    topVel = 5000
    topVelInd = MySpect._velocity_to_index(topVel)

    signalJump = 25

    visualSignalStart = 2652.21
    visSigIndex = MySpect._velocity_to_index(visualSignalStart)

    return MySpect, sTime, eTime, signalJump, botInd, topVelInd, visSigIndex
Example #27
0
 def plot_trace(self, spectrogram, trace_info, clim = [0.5, 1.5], color = 'r'):
     ''' plot traced coordinates in the dynamic spectrum
     '''
     
     base0 = Spectrogram()
     base0.spectrogram = spectrogram
     fig0, ax0 = base0.plot_spectrogram(figsize = [12,8], subplot = 211, clim = clim, cmap = 'Greys_r', aspect = 0.2)
     base1 = Spectrogram()
     base1.spectrogram = spectrogram
     fig1, ax1 = base1.plot_spectrogram(fig = fig0, subplot = 212, clim = clim, cmap = 'Greys_r', aspect = 0.2)
     trace_info = self.idx2map(spectrogram, trace_info)
     idplt = trace_info["ID"]
     xplt= trace_info["indy"]
     yplt = trace_info["frequency"]
     # overplot traced drift bursts
     for i in np.arange(idplt.min(), idplt.max()+1):
         idx = np.where(idplt == i)[0]
         if len(idx) > 1:
             plt.plot(xplt[idx],yplt[idx], color = color)
     ylim = ax0.get_ylim()
     ax1.set_ylim(ylim)
     plt.show()
Example #28
0
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 02 23:35:05 2016

@author: Zhitao
"""

from trace import Trace
from spectrogram import Spectrogram
########################## Import raw data. ###################################
file_path = "C:/Users/Zhitao/Desktop/pywork/"           # modify YOUR_PATH here
sample = Spectrogram(file_path = file_path)
sample.read_spectrogram()
data = sample.spectrogram
########### Apply tracing over specific range in time and frequency. ##########
t = Trace()
trace_info = t.trace(data, mask = None)
# Visualize traced structures.
t.plot_trace(data, trace_info)
Example #29
0
from tables import *
import config
import utils
from spectrogram import Spectrogram

conf = config.get_config()
winsize = int(conf.get('Spectrogram', 'WindowSize'))
nframes = int(conf.get('Tracks', 'LengthInSeconds')) * \
    int(conf.get('Tracks', 'SampleRate'))
stepsize = int(conf.get('Spectrogram', 'StepSize'))
fftres = int(conf.get('Spectrogram', 'FFTResolution'))
audio_folder = os.path.expanduser(conf.get('Input', 'AudioFolder'))
numberofgenres = len(utils.list_subdirs(audio_folder))
wins = Spectrogram.wins(winsize, nframes, stepsize)
bins = Spectrogram.bins(fftres)
shape = Spectrogram.shape(wins, bins)


class Track(IsDescription):

    """Description of a track in HDF5"""
    idnumber = Int32Col()
    name = StringCol(64)
    path = StringCol(512)
    genre = StringCol(32)
    # target = BoolCol(shape=(numberofgenres,))
    target = Int8Col()
    spectrogram = Float32Col(dflt=0.0, shape=shape)
def seamExtraction(SpectrogramObject:Spectrogram, startTime:int, stopTime:int, width:int, bottomIndex:int=0, topIndex:int=None, order:int = None, verbose:bool = False, theta = None):
    """
        Input:
            intensityMatrix: a 2d array [velocity][time] and each cell 
                corresponds to the intensity at that point.
            startTime: index corresponding to the start time in the intensity 
                matrix of the signal being considered.
            stopTime: index corresponding to the stop time in the intensity
                matrix of the signal being considered.
            width:
                How wide of a window that is being considered at
                    each time step. The window will be split evenly up and down
            bottomIndex: Upper bound on the velocity. 
                Assumed to be a valid index for sgram's intensity array.
                Defaults to zero
            order: the order of the minkowski distance function that will be minimized 
                to determine the most well connected signal. 
                Defaults to 2:
                    minimizing the Euclidean distance between neighboring pixels.
            verbose: Boolean that indicates if you want there to be debugging print statements.
                Defaults to False:
            theta: The relative importance of the phase array to the amplitude array if applicable.
                It should be between 0 and 1.
                
    Output:
        list of indices that should be plotted as the signal as an array of indices
        indicating the intensity values that correspond to the most 
        connected signal as defined by the metric.
    """

    if stopTime <= startTime:
        raise ValueError("Stop Time is assumed to be greater than start time.")

    else:
        try:
            theta = float(theta)
        except TypeError:
            raise TypeError("Theta needs to be able to be cast to a float.")

        if 0 > theta:
            theta = np.exp(theta) # This will get between 0 and 1.
        if theta > 1:
            theta = np.exp(-1*theta) # To get between 0 and 1.
        if width % 2 == 1:
            width += 1

        tableHeight = stopTime - startTime + 1

        velocities = np.zeros(tableHeight, dtype = np.int64)
        # This will hold the answers that we are looking for to return. It is a list of indices.

        halfFan = width//2

        bottomDP = bottomIndex + 1 


        if topIndex == None:
            topIndex = SpectrogramObject.velocity.shape[0] # I just want the maximum velocity index
        elif topIndex <= bottomIndex:
            topIndex = SpectrogramObject.velocity.shape[0]



        t, vel, real_raw_vals, ovals  = SpectrogramObject.slice((SpectrogramObject.time[startTime], SpectrogramObject.time[stopTime]), (SpectrogramObject.velocity[bottomDP], SpectrogramObject.velocity[topIndex]))

        amplitudeArray = [] # This is will be the magnitude of the complex data.
        phaseArray = []   # This will be the phase of the complex data.

        if SpectrogramObject.computeMode == "complex":
            # We have the complex data.
            amplitudeArray = real_raw_vals # It will be the appropriate item in this case.
            phaseArray = np.arctan2(np.real(ovals)/np.imag(ovals))
        elif SpectrogramObject.computeMode == "psd" or SpectrogramObject.computeMode == "magnitude":
            amplitudeArray = real_raw_vals # This is all that we will have as we cannot get the phase info.
        elif SpectrogramObject.computeMode == "angle" or SpectrogramObject.computeMode == "phase":
            phaseArray = ovals

        if verbose:
            print("new time shape", t.shape, "vel shape:", vel.shape)
            print("Before the transpose: real_raw_vals.shape", real_raw_vals.shape)      
            print("real_raw_vals.shape = ",real_raw_vals.shape)
            print("bottomDP", bottomDP)
            print()
            print("t2-t1", stopTime-startTime)
            print("(t2-t1)*halfan", (stopTime-startTime)*halfFan)
            print()
            print(topIndex)
            print(bottomIndex+1)
            print()
        
        amplitudeArray = np.transpose(amplitudeArray)
        phaseArray = np.transpose(phaseArray)
        tableHeight, tableWidth = np.transpose(real_raw_vals).shape
        DPTable = np.zeros((tableHeight, tableWidth))

        parentTable = np.zeros(DPTable.shape, dtype = np.int64)

        for timeIndex in range(2, tableHeight + 1):
            dpTime = tableHeight - timeIndex
            for velocityIndex in range(tableWidth):
                bestSoFar = np.Infinity
                bestPointer = None
                for testIndex in range(-halfFan, halfFan+1): # The + 1 is so that you get a balanced window.
                    if velocityIndex + testIndex >= 0 and velocityIndex + testIndex < tableWidth:
                        # then we have a valid index to test from.
                        ampAddition = 0
                        phaseAddition = 0
                        if len(amplitudeArray) != 0: # Then it has been initialized.
                            ampAddition = np.power(np.abs(amplitudeArray[dpTime+1][testIndex+velocityIndex]-amplitudeArray[dpTime][velocityIndex]), order)
                        if len(phaseArray) != 0: # Then it has been initialized.
                            phaseAddition = np.power(np.abs(phaseArray[dpTime+1][testIndex+velocityIndex]-phaseArray[dpTime][velocityIndex]), order)

                        current = (1-theta)*ampAddition + theta*phaseAddition + DPTable[dpTime+1][velocityIndex+testIndex]
                        
                        if current < bestSoFar:
                            bestSoFar = current
                            bestPointer = velocityIndex + testIndex
                            if verbose:
                                print("The bestValue has been updated for time", t[dpTime], "and velocity", vel[velocityIndex], \
                                    "to", bestSoFar, "with a bestPointer of ", vel[velocityIndex+testIndex], "at the next timeslice.")                          
                DPTable[dpTime][velocityIndex] = bestSoFar
                parentTable[dpTime][velocityIndex] = bestPointer

        # Now for the reconstruction.
        currentPointer = np.argmin(DPTable[0])
        
        if verbose:
            print("The value of the current pointer is", currentPointer)
            print("The minimum cost is", DPTable[0][currentPointer])

        velocities = reconstruction(parentTable, currentPointer, bottomDP)

        return velocities, parentTable, DPTable, bottomDP, topIndex
    WHITE_CH4_SHOT/seg00.dig -- ??? opencv_long_start_pattern4 span=200

    BLUE_CH1_SHOT/seg00.dig -- opencv_long_start_pattern4 span=150
    BLUE_CH2_SHOT/seg00.dig -- ??? opencv_long_start_pattern3 span=200
    BLUE_CH3_SHOT/seg00.dig -- opencv_long_start_pattern4 span=200

    CH_1_009/seg00.dig -- opencv_long_start_pattern2 span=200
    CH_3_009/seg00.dig -- opencv_long_start_pattern2 span=200
    CH_4_009/seg01.dig -- opencv_long_start_pattern2 span=200
    CH_4_009/seg02.dig -- opencv_long_start_pattern4 span=200
    """
    from ProcessingAlgorithms.preprocess.digfile import DigFile

    path = "../dig/BLUE_CH1_SHOT/seg00.dig"
    df = DigFile(path)
    spec = Spectrogram(df, 0.0, 60.0e-6, overlap_shift_factor= 1/8, form='db')
    spec.availableData = ['intensity']

    # print(spec.time[200])

    # gives user the option to click, by default it searches from (0,0)
    template_matcher = TemplateMatcher(spec,template=Templates.opencv_long_start_pattern5.value,
                                            span=200,
                                            k=20,
                                            methods=['cv2.TM_CCOEFF_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'])


    # masks the baselines to try and avoid matching with baselines or echoed signals
    template_matcher.mask_baselines()

    # get the times and velocities from matching
def mainTest(SpectrogramObject:Spectrogram, startTime:int, stopTime:int, bottomIndex:int=0, topIndex:int=None, vStartInd: int=None,verbose:bool = False):
    widths = [1,3,5,11,21]
    orders = [1,2,10]
    precision = 10
    thetas = np.arange(precision+1)/precision # Get in the range of zero to one inclusive.
    seam = []

    velocities = SpectrogramObject.velocity
    time = SpectrogramObject.time

    signalData = time*1e6

    headers = ["Time ($\mu$s)"]

    basefilePath = "../DocumentationImages/DP/ComplexSpectra/"
    digfileUsed = SpectrogramObject.data.filename
    for width in widths:
        for order in orders:
            for theta in thetas:
                hyperParamNames =  "w " + str(width) + " ord " + str(order) + " Minkowski dist theta " + str(theta).replace(".", "_")
                signal, p_table, dp_table, botVel, topVel = seamExtraction(SpectrogramObject, startTime, stopTime, width, bottomIndex, topIndex, order, theta=theta)

                fname = "DP_Cost_Table time by vel " +hyperParamNames + ".csv"
                filename = basefilePath + fname
                np.savetxt(filename,dp_table,delimiter=",")

                fname = "Parent_Table time by vel " + hyperParamNames + ".csv"
                filename = basefilePath + fname
                np.savetxt(filename,p_table,delimiter=",")

                fig = plt.figure(num=1)
                plt.plot(velocities[botVel:topVel+1], dp_table[0])
                plt.title("Total Minkowski" + str(order) +" Order Cost diagram with a window size of " +str(width) +" PercPhase:" + str(theta))
                plt.xlabel("Starting velocity of the trace (m/s)")
                plt.ylabel("Minimum value of the sum ($\theta$|$\phi_i$ - $\phi_{i-1}$|^" + str(order) + "(1-$\theta$)|$Amp_i$ - $Amp_{i-1}$|^" + str(order) +") along the path")
                # manager = plt.get_current_fig_manager()
                # manager.window.Maximized()
                if verbose:
                    fig.show()
                    print("Here is a graph of the signal trace across time")       
                # fig.show()
                    
                extension = "svg"
                fname = "DP_Start_Cost " + hyperParamNames + extension
                filename = basefilePath + fname
                fig.savefig(filename, bbox_inches = "tight")

                fig2 = plt.figure(num = 2, figsize=(10, 6))
                ax = fig2.add_subplot(1,1,1)
                fig2Axes = fig2.axes[0]

                print(type(fig2Axes))
                print(fig2Axes)
                SpectrogramObject.plot(axes=fig2Axes)
                plt.xlim((SpectrogramObject.time[startTime], SpectrogramObject.time[stopTime]))

                fig2Axes.plot(time[startTime: stopTime+1], velocities[signal], 'b-', alpha = 0.4, label="Minimum Cost")
                if vStartInd != None:
                       # Compute the signal seam for assuming this is the start point.
                    seam = reconstruction(p_table, vStartInd-botVel, botVel)
                    fig2Axes.plot(time[startTime: stopTime+1], velocities[seam], 'r--', alpha = 0.4, label="Expected Start Point")
                fig2Axes.set_title("Velocity as a function of time for the minimum cost seam with Minkowski" + str(order)+ " and a window size of " + str(width) + " raw")
                fig2Axes.legend()

                # manager = plt.get_current_fig_manager()
                # manager.window.showMaximized()
                if verbose:
                    fig2.show()
                fname = "Overlay Spectra " + hyperParamNames + extension
                filename = basefilePath + fname
                plt.savefig(filename, bbox_inches = "tight")

                # Build the reconstruction of every possible starting velocity. Then, save them in a csv file.
                extension = "csv"
                fname = "Velocity Traces " + hyperParamNames + extension
                filename = basefilePath + fname

                for velInd in range(0, topVel-botVel+1):
                    trace = reconstruction(p_table, velInd, botVel)
                    header = "Starting Velocity " + str(velocities[velInd+botVel]) + "(m/s)"
                    headers.append(header) 
                    meaured = velocities[trace]
                    signalData = np.hstack((signalData, meaured))
                signalData = signalData.reshape((topVel-botVel + 2, len(time))).transpose() # I want each column to be a velocity trace.
                # np.savetxt(filename, signalData, delimiter=",")
                df = pd.DataFrame(data=signalData, index=time*1e6, columns=headers)
                df.to_csv(filename)

                print("Completed the documentation for ", hyperParamNames)
        tr = table.row
        i = 0
        for filename, genre in gt.ground_truth.iteritems():
            # Read file
            f = Sndfile(filename, mode='r')

            # Check against specs
            check_audio_file_specs(f, samplerate, encoding, channels)

            # Read
            logging.debug("Reading %s" % filename)
            frames = f.read_frames(lengthinseconds*samplerate)

            # Calculate Spectrogram
            logging.debug("Calculating spectrogram of %s" % filename)
            sg = Spectrogram.from_waveform(frames, windowsize, stepsize, windowtype, fftres)

            # Save in feature file
            tr['idnumber'] = i
            tr['name'] = os.path.basename(filename)
            tr['path'] = filename
            tr['genre'] = genre
            tr['target'] = gt.genres.index(genre)
            tr['spectrogram'] = sg.spectrogram
            logging.debug("Saving %s in HDF5 file." % filename)
            tr.append()
            i = i + 1
        h5file.close()
        logging.info("Feature Extraction: spectrograms saved in %s." % features_path)

    else:
        if x < 0:
            return 0
        if x > nmax:
            return nmax
        return x

    for t in range(len(time) - 1):
        if power[amax[t], t] > threshold:
            acenter = amax[t]
            vfrom, vto = peg(acenter - span), peg(acenter + span)
            gus = Gaussian(velocity[vfrom:vto], power[vfrom:vto, t])
            if gus.valid:
                times.append(time[t])
                centers.append(gus.center)
                widths.append(gus.width)
                amps.append(gus.amplitude)
            else:
                print(f"[{t}] {gus.error}")

    #power[power>=threshold] = 1
    plt.pcolormesh(time * 1e6, velocity, power)
    plt.plot(np.array(times) * 1e6, centers, 'r.', alpha=0.5)
    plt.show()


if __name__ == "__main__":
    sp = Spectrogram('../dig/sample')
    roi = dict(time=(13.6636e-6, 35.1402e-6), velocity=(2393.33, 4500.377))
    analyze_region(sp, roi)
    print("Hi!")
    def __init__(self, path, which_set,
                 feature="spectrogram",
                 space="conv2d",
                 axes=('b', 0, 1, 'c'),
                 preprocess=False,
                 seconds=None,
                 window_size=None,
                 window_type=None,
                 step_size=None,
                 tw_window_size=None,
                 tw_window_type=None,
                 tw_step_size=None,
                 fft_resolution=None,
                 seed=None,
                 n_folds=4,
                 run_n=0,
                 verbose=False,
                 print_params=True):
        super(AudioDataset, self).__init__()

        converter = space

        # signal is 1D
        if feature == "signal":
            space = "vector"
            converter = "signal"

        # inverting a spectrogram if the space is a vector doesn't make sense
        if space == "vector" and feature == "inv_spectrogram":
            feature == "spectrogram"

        feature_extractors = {
            "spectrogram": self.get_spectrogram_data,
            "inv_spectrogram": self.get_inv_spectrogram_data,
            "texture_window": self.get_texture_window_data,
            "signal": self.get_signal_data
        }

        spaces_converters = {
            "conv2d": AudioDataset.twod_to_conv2dspaces,
            "vector": AudioDataset.twod_to_vectorspaces,
            "signal": lambda x: x
        }

        index_converters = {
            "conv2d": lambda x: x,
            "vector": self.track_ids_to_frame_ids,
            "signal": lambda x: x
        }

        path = string_utils.preprocess(path)

        # init dynamic params
        tracks, genres = self.tracks_and_genres(path, seconds)
        samplerate = tracks[0].samplerate
        seconds = tracks[0].seconds

        if feature != "signal":
            spec_wins_per_track = len(
                Spectrogram.wins(
                    seconds * samplerate,
                    window_size,
                    step_size
                )
            )

            if feature == "texture_window":
                tw_wins_per_track = len(
                    TextureWindow.wins(
                        spec_wins_per_track,
                        tw_window_size,
                        tw_step_size
                    )
                )
                wins_per_track = tw_wins_per_track
            else:
                wins_per_track = spec_wins_per_track

            bins_per_track = Spectrogram.bins(fft_resolution)

        view_converters = {
            "conv2d": dense_design_matrix.DefaultViewConverter(
                (bins_per_track, wins_per_track, 1), axes
            ),
            "vector": None,
            "signal": None
        }

        view_converter = view_converters[converter]

        self.__dict__.update(locals())
        del self.self

        if print_params:
            print(self)
            add_row(next(loc), 0.9, 0.8, 0.5)  # to faded yellow
            add_row(next(loc), 0.5, 0, 0.5)
            add_row(next(loc), 0, 0.5, 0)
            add_row(next(loc), 1, 0.75, 0.2)
            add_row(next(loc), 0.2, 0.2, 0.2)
            add_row(next(loc), 0, 0, 0)
        except:
            pass

        add_row(1, 0, 0, 0)
        return cdict

    roids = calculate_centroids()
    bounds = normalize_bounds(roids)
    cdict = build_cdict(bounds)

    from matplotlib.colors import LinearSegmentedColormap
    COLORMAPS[name] = LinearSegmentedColormap(name, cdict)
    return dict(name=name, centroids=roids, cmap=COLORMAPS[name])


if __name__ == '__main__':
    sg = Spectrogram('../dig/CH_4_009.dig')
    roids = make_spectrogram_color_map(sg, 4, "Kitty")

    sg.plot(cmap = roids["cmap"])
    import matplotlib.pyplot as plt
    plt.xlim((0, 50))
    plt.ylim((1500, 5000))
    plt.show()
Example #37
0
class PanelGraph(wx.Panel):
    def __init__(self, panel, notify, settings, callbackMotion):
        self.panel = panel
        self.notify = notify
        self.plot = None
        self.settings = settings
        self.spectrum = None
        self.isLimited = None
        self.limit = None
        self.extent = None

        self.mouseSelect = None
        self.mouseZoom = None
        self.measureTable = None

        self.background = None

        self.selectStart = None
        self.selectEnd = None

        self.menuClearSelect = []

        self.measure = None
        self.show = None

        self.doDraw = False

        wx.Panel.__init__(self, panel)

        self.figure = matplotlib.figure.Figure(facecolor='white')
        self.canvas = FigureCanvas(self, -1, self.figure)

        self.measureTable = PanelMeasure(self)

        self.toolbar = NavigationToolbar(self.canvas, self, settings,
                                         self.hide_overlay)
        self.toolbar.Realize()

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.canvas, 1, wx.EXPAND)
        vbox.Add(self.measureTable, 0, wx.EXPAND)
        vbox.Add(self.toolbar, 0, wx.EXPAND)
        self.SetSizer(vbox)
        vbox.Fit(self)

        self.create_plot()

        self.canvas.mpl_connect('motion_notify_event', callbackMotion)
        self.canvas.mpl_connect('draw_event', self.on_draw)
        self.canvas.mpl_connect('idle_event', self.on_idle)
        self.Bind(wx.EVT_SIZE, self.on_size)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)

    def create_plot(self):
        if self.plot is not None:
            self.plot.close()

        self.toolbar.set_auto(True)

        if self.settings.display == Display.PLOT:
            self.plot = Plotter(self.notify, self.figure, self.settings)
        elif self.settings.display == Display.SPECT:
            self.plot = Spectrogram(self.notify, self.figure, self.settings)
        else:
            self.plot = Plotter3d(self.notify, self.figure, self.settings)

        self.set_fonts()

        self.toolbar.set_plot(self.plot)
        self.toolbar.set_type(self.settings.display)
        self.measureTable.set_type(self.settings.display)

        self.set_plot_title()
        self.figure.tight_layout()
        self.figure.subplots_adjust(top=0.85)
        self.redraw_plot()
        self.plot.scale_plot(True)
        self.mouseZoom = MouseZoom(self.plot, self.toolbar, self.hide_overlay)
        self.mouseSelect = MouseSelect(self.plot, self.on_select,
                                       self.on_selected)
        self.measureTable.show(self.settings.showMeasure)
        self.panel.SetFocus()

    def set_fonts(self):
        axes = self.plot.get_axes()
        axes.xaxis.label.set_size('small')
        axes.yaxis.label.set_size('small')
        if self.settings.display == Display.SURFACE:
            axes.zaxis.label.set_size('small')
        axes.tick_params(axis='both', which='major', labelsize='small')
        axes = self.plot.get_axes_bar()
        axes.tick_params(axis='both', which='major', labelsize='small')

    def add_menu_clear_select(self, menu):
        self.menuClearSelect.append(menu)
        menu.Enable(False)

    def enable_menu(self, state):
        for menu in self.menuClearSelect:
            menu.Enable(state)

    def on_size(self, event):
        ppi = wx.ScreenDC().GetPPI()
        size = [float(v) for v in self.canvas.GetSize()]
        width = size[0] / ppi[0]
        height = size[1] / ppi[1]
        self.figure.set_figwidth(width)
        self.figure.set_figheight(height)
        self.figure.set_dpi(ppi[0])
        event.Skip()

    def on_draw(self, _event):
        axes = self.plot.get_axes()
        self.background = self.canvas.copy_from_bbox(axes.bbox)
        self.draw_overlay()

    def on_select(self):
        self.hide_measure()

    def on_selected(self, start, end):
        self.enable_menu(True)
        self.selectStart = start
        self.selectEnd = end
        self.measureTable.set_selected(self.spectrum, start, end)

    def on_idle(self, _event):
        if self.doDraw and self.plot.get_plot_thread() is None:
            self.hide_overlay()
            self.canvas.draw()
            self.doDraw = False

    def on_timer(self, _event):
        self.timer.Stop()
        self.set_plot(None, None, None, None, self.annotate)

    def draw(self):
        self.doDraw = True

    def show_measureTable(self, show):
        self.measureTable.show(show)
        self.Layout()

    def set_plot(self, spectrum, isLimited, limit, extent, annotate=False):
        if spectrum is not None and extent is not None:
            if isLimited is not None and limit is not None:
                self.spectrum = copy.copy(spectrum)
                self.extent = extent
                self.annotate = annotate
                self.isLimited = isLimited
                self.limit = limit

        if self.plot.get_plot_thread() is None:
            self.timer.Stop()
            self.measureTable.set_selected(self.spectrum, self.selectStart,
                                           self.selectEnd)
            if isLimited:
                spectrum = reduce_points(spectrum, limit)
            self.plot.set_plot(self.spectrum, self.extent, annotate)

        else:
            self.timer.Start(200, oneShot=True)

    def set_plot_title(self):
        if len(self.settings.devices) > 0:
            gain = self.settings.devices[self.settings.index].gain
        else:
            gain = 0
        self.figure.suptitle("Frequency Spectrogram\n{0} - {1} MHz,"
                             " gain = {2}dB".format(self.settings.start,
                                                    self.settings.stop, gain))

    def redraw_plot(self):
        if self.spectrum is not None:
            self.set_plot(self.spectrum,
                          self.settings.pointsLimit,
                          self.settings.pointsMax,
                          self.extent, self.settings.annotate)

    def set_grid(self, on):
        self.plot.set_grid(on)

    def draw_overlay(self):
        if self.background is not None:
            self.canvas.restore_region(self.background)
            self.draw_select()
            self.draw_measure()
            self.canvas.blit(self.plot.get_axes().bbox)

    def draw_select(self):
        if self.selectStart is not None and self.selectEnd is not None:
            self.mouseSelect.draw(self.selectStart, self.selectEnd)

    def hide_overlay(self):
        if self.plot is not None:
            self.plot.hide_measure()
        self.hide_select()

    def hide_measure(self):
        if self.plot is not None:
            self.plot.hide_measure()

    def hide_select(self):
        if self.mouseSelect is not None:
            self.mouseSelect.hide()

    def draw_measure(self):
        if self.measure is not None and self.measure.is_valid():
            self.plot.draw_measure(self.measure, self.show)

    def update_measure(self, measure, show):
        self.measure = measure
        self.show = show
        self.draw_overlay()

    def get_figure(self):
        return self.figure

    def get_axes(self):
        return self.plot.get_axes()

    def get_canvas(self):
        return self.canvas

    def get_toolbar(self):
        return self.toolbar

    def scale_plot(self, force=False):
        self.plot.scale_plot(force)

    def clear_plots(self):
        self.plot.clear_plots()

    def clear_selection(self):
        self.measure = None
        self.measureTable.clear_measurement()
        self.selectStart = None
        self.selectEnd = None
        self.mouseSelect.clear()
        self.enable_menu(False)

    def close(self):
        close_modeless()
Example #38
0
    allDigs = DigFile.inventory()[
        "file"]  # Just the files that are not segments.

    saveLoc = os.path.join(
        os.path.split(digFolder)[0], "TotalIntensityMaps\\Median\\")
    if not os.path.exists(saveLoc):
        os.makedirs(saveLoc)

    fractions = np.arange(10)
    fractionsL = len(fractions)

    data = np.zeros((fractionsL, len(allDigs)))
    data[:, 0] = fractions
    for i in range(len(allDigs)):
        filename = allDigs[i]
        path = os.path.join(digFolder, filename)
        spec = Spectrogram(path)
        inTen = np.sum(spec.intensity, axis=0)
        # Offset the values so that everything is non negative.
        inTen = inTen - np.min(inTen)

        for j in range(fractionsL):
            frac = np.power(10.0, -1 * fractions[j])
            data[j, i] = len(threshold(inTen, frac))

        del spec

    name = os.path.join(saveLoc, "NumberOfLowValuesVsFracOfMax.txt")

    np.savetxt(name, data)
Example #39
0
class PanelGraph(wx.Panel):
    def __init__(self, panel, notify, settings, callbackMotion):
        self.panel = panel
        self.notify = notify
        self.plot = None
        self.settings = settings
        self.spectrum = None
        self.isLimited = None
        self.limit = None
        self.extent = None

        self.background = None

        self.selectStart = None
        self.selectEnd = None

        self.menuClearSelect = []

        self.measure = None
        self.show = None

        self.doDraw = False

        wx.Panel.__init__(self, panel)

        self.figure = matplotlib.figure.Figure(facecolor='white')
        self.canvas = FigureCanvas(self, -1, self.figure)

        self.measureTable = PanelMeasure(self)

        self.toolbar = NavigationToolbar(self.canvas, self, settings,
                                         self.on_nav_changed)
        self.toolbar.Realize()

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.canvas, 1, wx.EXPAND)
        vbox.Add(self.measureTable, 0, wx.EXPAND)
        vbox.Add(self.toolbar, 0, wx.EXPAND)
        self.SetSizer(vbox)
        vbox.Fit(self)

        self.create_plot()

        self.canvas.mpl_connect('motion_notify_event', callbackMotion)
        self.canvas.mpl_connect('draw_event', self.on_draw)
        self.canvas.mpl_connect('idle_event', self.on_idle)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)

    def create_plot(self):
        if self.plot is not None:
            self.plot.close()

        if self.settings.display == Display.PLOT:
            self.plot = Plotter(self.notify, self.figure, self.settings)
        elif self.settings.display == Display.SPECT:
            self.plot = Spectrogram(self.notify, self.figure, self.settings)
        else:
            self.plot = Plotter3d(self.notify, self.figure, self.settings)

        self.toolbar.set_plot(self.plot)
        self.toolbar.set_type(self.settings.display)
        self.measureTable.set_type(self.settings.display)

        self.set_plot_title()
        self.redraw_plot()
        self.plot.scale_plot(True)
        self.mouseZoom = MouseZoom(self.plot, self.toolbar, self.hide_measure,
                                   self.draw_measure)
        self.mouseSelect = MouseSelect(self.plot, self.on_select,
                                       self.on_selected)
        self.draw_select()
        self.measureTable.show(self.settings.showMeasure)
        self.panel.SetFocus()

    def add_menu_clear_select(self, menu):
        self.menuClearSelect.append(menu)
        menu.Enable(False)

    def enable_menu(self, state):
        for menu in self.menuClearSelect:
            menu.Enable(state)

    def on_draw(self, _event):
        axes = self.plot.get_axes()
        self.background = self.canvas.copy_from_bbox(axes.bbox)
        self.mouseSelect.set_background(self.background)
        self.draw_measure()

    def on_nav_changed(self, _event):
        self.draw_measure()

    def on_select(self):
        self.hide_measure()

    def on_selected(self, start, end):
        self.enable_menu(True)
        self.on_draw(None)
        self.selectStart = start
        self.selectEnd = end
        self.measureTable.set_selected(self.spectrum, start, end)
        self.draw_measure()

    def on_idle(self, _event):
        if self.doDraw and self.plot.get_plot_thread() is None:
            self.canvas.draw()
            self.doDraw = False

    def on_timer(self, _event):
        self.timer.Stop()
        self.set_plot(None, None, None, None, self.annotate)

    def draw(self):
        self.doDraw = True

    def show_measureTable(self, show):
        self.measureTable.show(show)
        self.Layout()

    def set_plot(self, spectrum, isLimited, limit, extent, annotate=False):
        if spectrum is not None and extent is not None:
            if isLimited is not None and limit is not None:
                self.spectrum = copy.copy(spectrum)
                self.extent = extent
                self.annotate = annotate
                self.isLimited = isLimited
                self.limit = limit

        if self.plot.get_plot_thread() is None:
            self.timer.Stop()
            self.measureTable.set_selected(spectrum, self.selectStart,
                                           self.selectEnd)
            if isLimited:
                spectrum = reduce_points(spectrum, limit)
            self.plot.set_plot(spectrum, extent, annotate)

            self.draw_select()
        else:
            self.timer.Start(200, oneShot=True)

    def set_plot_title(self):
        if len(self.settings.devices) > 0:
            gain = self.settings.devices[self.settings.index].gain
        else:
            gain = 0
        self.plot.set_title("Frequency Spectrogram\n{0} - {1} MHz,"
                            " gain = {2}dB".format(self.settings.start,
                                                   self.settings.stop, gain))

    def redraw_plot(self):
        if self.spectrum is not None:
            self.set_plot(self.spectrum,
                          self.settings.pointsLimit,
                          self.settings.pointsMax,
                          self.extent, self.settings.annotate)

    def draw_select(self):
        if self.selectStart is not None and self.selectEnd is not None:
            self.mouseSelect.draw(self.selectStart, self.selectEnd)

    def hide_measure(self):
        self.plot.hide_measure()

    def draw_measure(self):
        if self.measure is not None and self.background is not None:
            self.plot.draw_measure(self.background, self.measure, self.show)

    def update_measure(self, measure, show):
        self.measure = measure
        self.show = show
        self.draw_measure()

    def get_figure(self):
        return self.figure

    def get_axes(self):
        return self.plot.get_axes()

    def get_canvas(self):
        return self.canvas

    def get_toolbar(self):
        return self.toolbar

    def scale_plot(self, force=False):
        self.plot.scale_plot(force)

    def clear_plots(self):
        self.plot.clear_plots()

    def clear_selection(self):
        self.measure = None
        self.measureTable.clear_measurement()
        self.selectStart = None
        self.selectEnd = None
        self.mouseSelect.clear()
        self.enable_menu(False)

    def close(self):
        close_modeless()