def split(self):

        if hasattr(self, 'elmdf'):
            return self.elmdf

        self.elms = self.elm_loc()
        elm_cycles = {}
        for elm_no, elm_time in enumerate(self.elms[:-1]):

            if self.elms[elm_no + 1] - self.elms[elm_no] <= self.min_elm_window:  # default min_elm_window = 50
                continue

            start_ielm = index_match(self.arr[0], elm_time)
            stop_ielm = index_match(self.arr[0], self.elms[elm_no + 1])

            for ielm_time in self.arr[0][start_ielm:stop_ielm]:
                '''MAX: Uncomment the lines below to get % of ELM'''
                ielm_index = np.argwhere(self.arr[0] == ielm_time)[0][0]
                elm_cycles[(elm_no, ielm_index, ielm_time, self.arr[0][stop_ielm] - ielm_time)] = self.arr[1].T[ielm_index]
                # elm_cycles[(elm_no, ielm_index, ielm_time, (ielm_time-self.arr[0][start_ielm])/(self.arr[0][stop_ielm]-self.arr[0][start_ielm]))] = self.arr[1].T[ielm_index]
        index = pd.MultiIndex.from_tuples(elm_cycles.keys(), names=['ELM_No', 'Index', 'Time (ms)', 'T - ELM (ms)'])
        # index = pd.MultiIndex.from_tuples(elm_cycles.keys(), names=['ELM_No', 'Index', 'Time (ms)', '% ELM'])
        self.elmdf = pd.DataFrame(elm_cycles.values(), index=index)

        return self.elmdf
예제 #2
0
 def slice1d(self, time, smooth=False, ax=None):
     '''
     This plots the 1D slice in plot slice. Not super necessary as an extra function, but I kinda wanted to see if
     I could move matplotlib objects between functions.
     '''
     if ax is None:
         ax = plt.gca()
     index = index_match(self.arr[0],
                         time)  # finds index of time entered. Kind of slow?
     if len(self.arr[0]) < index < 0:
         raise ValueError(
             'Selected time is out of bounds of run time for shot.')
     slce = self.arr[1][:, index]
     ax.plot(self.arr[2], slce)
     ax.set_xlabel('Frequency (kHz)')
     if smooth:
         smooth_arr = gaussian_filter1d(slce, 10)
         peaks, _ = find_peaks(smooth_arr,
                               prominence=(np.mean(abs(smooth_arr)), None),
                               distance=75)
         ax.plot(self.arr[2], smooth_arr, 'y')
         ax.plot(self.arr[2][peaks], smooth_arr[peaks], 'ro')
     else:
         peaks, _ = find_peaks(slce,
                               prominence=(np.mean(abs(slce)), None),
                               distance=75)
         ax.plot(self.arr[2][peaks], slce[peaks], 'ro')
     return peaks
    def split_from_spec(self, plot=False):
        '''
        split returns a dataframe of the original array which excludes all arrays belonging to ELM's.
        It also returns a hot array with 0 for all intra-elm indices and 1 for all elm indices (the ones that were excluded
        from the dataframe.
        '''

        _, pm = self.get_peaks()
        stop = self.stop_height  # default 1500
        pm_norm = (pm[:, stop:] - np.amin(pm[:, stop:])) / (np.amax(pm[:, stop:]) - np.amin(pm[:, stop:]))
        sums = np.sum(pm_norm, axis=1)

        # peaks, props = find_peaks(sums, distance=100, prominence=(1, None), width=(None, None), rel_height=1.0)
        # l_elms = props['left_ips']
        # r_elms = props['right_ips']

        elms = self.elm_loc()
        for i, elm in enumerate(elms):
            i_elm = index_match(self.arr[0], elm)
            # l_elms[i] = i_l_elm
            l_elm = np.argmax(np.gradient(sums[i_elm - 50:i_elm + 50])) + i_elm - 50
            r_elm = np.argmin(np.gradient(sums[i_elm - 50:i_elm + 50])) + i_elm - 50
            l_elms[i] = l_elm
            r_elms[i] = r_elm
        #
        # for i, r_elm in enumerate(r_elms):
        #     i_r_elm = index_match(self.arr[0], r_elm)
        #     r_elms[i] = i_r_elm
        # hot = np.zeros_like(self.arr[0])
        # for i in np.column_stack((l_elms, r_elms)):
        #     hot[i[0]:i[1]] = 1
        r_elms = r_elms.astype(int)
        l_elms = l_elms.astype(int)
        if plot:
            fig, ax = plt.subplots(1, 1)
            self.heatmap2d(ax=ax)
            # for i, num in enumerate(hot):
            #     if num == 1:
            #         ax.axvline(self.arr[0][i], c='orange')
            for i in l_elms:
                ax.axvline(self.arr[0][i], ymin=stop / self.arr[1].shape[0], c='red')
            for i in r_elms:
                ax.axvline(self.arr[0][i], ymin=stop / self.arr[1].shape[0], c='green')
            # for elm in elms:
            #     i_elm = index_match(self.arr[0], elm)
            #     ax.axvline(self.arr[0][i_elm], c='blue')
            plt.show()
        # make dict with keys, values, times
        elm_cycles = {}
        for i in range(len(r_elms) - 1):
            for j in self.arr[0][r_elms[i]:l_elms[i + 1]]:
                k = np.argwhere(self.arr[0] == j)[0][0]
                elm_cycles[(i, j, k)] = self.arr[1].T[np.argwhere(self.arr[0] == j)][0][0]

        index = pd.MultiIndex.from_tuples(elm_cycles.keys(), names=['ELM_No', 'Time(ms)', 'Index'])
        elmdf = pd.DataFrame(elm_cycles.values(), index=index)

        return elmdf
예제 #4
0
    def split(self):

        self.elms = self.elm_loc()
        elm_cycles = {}
        for elm_no, elm_time in enumerate(self.elms[:-1]):
            if self.elms[elm_no + 1] - self.elms[
                    elm_no] <= self.min_elm_window:  #default min_elm_window = 50
                continue

            start_ielm = index_match(self.arr[0], elm_time)
            stop_ielm = index_match(self.arr[0], self.elms[elm_no + 1])

            for ielm_time in self.arr[0][start_ielm:stop_ielm]:
                ielm_index = np.argwhere(self.arr[0] == ielm_time)[0][0]
                elm_cycles[(elm_no, ielm_time,
                            ielm_index)] = self.arr[1].T[ielm_index]
        index = pd.MultiIndex.from_tuples(
            elm_cycles.keys(), names=['ELM_No', 'Time(ms)', 'Index'])
        self.elmdf = pd.DataFrame(elm_cycles.values(), index=index)

        return self.elmdf
예제 #5
0
    def time_to_elm(self):

        if not hasattr(self, 'elmdf'):
            self.split()

        ielm_time = np.array([i[1] for i in self.elmdf.index])

        dict = {}
        prev_elm_index = 0
        for i, elm in enumerate(self.elms):
            next_elm_index = index_match(ielm_time, elm)
            for j, ielm in enumerate(ielm_time[prev_elm_index:next_elm_index]):
                dict[(i, ielm,
                      j + prev_elm_index)] = ielm_time[next_elm_index] - ielm
            prev_elm_index = next_elm_index

        index = pd.MultiIndex.from_tuples(
            dict.keys(), names=['ELM_No', 'Time (ms)', 'Index'])
        t_to_elm = pd.DataFrame(dict.values(),
                                index=index,
                                columns=['Time to Next ELM (ms)'])

        return t_to_elm
예제 #6
0
    def split(self, plot=False):
        '''
        split returns a dataframe of the original array which excludes all arrays belonging to ELM's.
        It also returns a hot array with 0 for all intra-elm indices and 1 for all elm indices (the ones that were excluded
        from the dataframe.
        '''

        if hasattr(self, 'elmdf'):
            return self.elmdf

        pm = self.arr[1].T
        stop = self.stop_height  # default 1500
        pm_norm = (pm[:, stop:] - np.amin(pm[:, stop:])) / (
            np.amax(pm[:, stop:]) - np.amin(pm[:, stop:]))
        sums = np.sum(pm_norm, axis=1)

        peaks, props = find_peaks(sums,
                                  distance=10,
                                  prominence=(0.5, None),
                                  width=(10, 50),
                                  rel_height=0.95)
        l_elms = props['left_ips']
        r_elms = props['right_ips']
        r_elms = np.asarray([*map(np.ceil, r_elms)]).astype(int)
        l_elms = np.asarray([*map(np.floor, l_elms)]).astype(int)
        l_elms_time = self.arr[0][l_elms]
        r_elms_time = self.arr[0][r_elms]

        if plot:
            fig, ax = plt.subplots(1, 1)
            self.heatmap2d(ax=ax)
            for t in l_elms_time:
                ax.axvline(t, ymin=stop / self.arr[1].shape[0], c='red')
            for t in r_elms_time:
                ax.axvline(t, ymin=stop / self.arr[1].shape[0], c='green')
            plt.show()

        # make dict with keys, values, times
        elm_cycles = {}
        elms = list(zip(l_elms_time, r_elms_time))
        for elm_no, _ in enumerate(elms[:-1]):
            if elms[elm_no + 1][0] - elms[elm_no][
                    1] <= self.min_elm_window:  # default min_elm_window = 50
                continue

            start_ielm = index_match(self.arr[0], elms[elm_no][1])
            stop_ielm = index_match(self.arr[0], elms[elm_no + 1][0])

            for ielm_time in self.arr[0][start_ielm:stop_ielm]:
                ielm_index = np.argwhere(self.arr[0] == ielm_time)[0][0]
                elm_cycles[(elm_no, ielm_index, ielm_time,
                            self.arr[0][stop_ielm] -
                            ielm_time)] = self.arr[1].T[ielm_index]
                # elm_cycles[(elm_no, ielm_index, ielm_time, (ielm_time - self.arr[0][start_ielm]) /
                #             (self.arr[0][stop_ielm] - self.arr[0][start_ielm]))] = self.arr[1].T[ielm_index]
        index = pd.MultiIndex.from_tuples(
            elm_cycles.keys(),
            names=['ELM_No', 'Index', 'Time (ms)', 'T - ELM (ms)'])
        # index = pd.MultiIndex.from_tuples(elm_cycles.keys(), names=['ELM_No', 'Index', 'Time (ms)', '% ELM'])
        self.elmdf = pd.DataFrame(elm_cycles.values(), index=index)

        return self.elmdf