|
| 1 | +""" |
| 2 | + Pipeline for running all the modules in order |
| 3 | +""" |
| 4 | +# pylint: disable=wrong-import-position |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import inspect |
| 8 | +CURRENT_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 9 | +PARENT_DIR = os.path.dirname(CURRENT_DIR) |
| 10 | +sys.path.insert(0, PARENT_DIR) |
| 11 | +from timeit import default_timer as timer |
| 12 | +import filterbank.filterbank |
| 13 | +import timeseries.timeseries |
| 14 | +import clipping |
| 15 | +import dedisperse |
| 16 | +import fourier |
| 17 | + |
| 18 | +# pylint: disable=too-many-locals |
| 19 | +# pylint: disable=too-many-arguments |
| 20 | +# pylint: disable=invalid-name |
| 21 | +# pylint: disable=no-self-use |
| 22 | + |
| 23 | +class Pipeline: |
| 24 | + """ |
| 25 | + The Pipeline combines the functionality of all modules |
| 26 | + in the library. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, filename=None, as_stream=False, DM=230, scale=3, n=None, size=None): |
| 30 | + """ |
| 31 | + Initialize Pipeline object |
| 32 | +
|
| 33 | + Args: |
| 34 | + as_stream, read the filterbank data as stream |
| 35 | + """ |
| 36 | + if as_stream: |
| 37 | + if n: |
| 38 | + result = self.read_n_rows(n, filename, DM, scale) |
| 39 | + file = open("n_rows_filterbank.txt", "a+") |
| 40 | + else: |
| 41 | + result = self.read_rows(filename) |
| 42 | + file = open("rows_filterbank.txt", "a+") |
| 43 | + else: |
| 44 | + result = self.read_static(filename, DM, scale, size) |
| 45 | + file = open("static_filterbank.txt", "a+") |
| 46 | + file.write(str(result) + ",") |
| 47 | + file.close() |
| 48 | + |
| 49 | + |
| 50 | + def read_rows(self, filename): |
| 51 | + """ |
| 52 | + Read the filterbank data as stream |
| 53 | + and measure the time |
| 54 | + """ |
| 55 | + # init filterbank as stream |
| 56 | + fil = filterbank.Filterbank(filename) |
| 57 | + time_start = timer() |
| 58 | + while True: |
| 59 | + fil_data = fil.next_row() |
| 60 | + if isinstance(fil_data, bool): |
| 61 | + break |
| 62 | + time_stop = timer() - time_start |
| 63 | + return time_stop |
| 64 | + |
| 65 | + |
| 66 | + def read_n_rows(self, n, filename, DM, scale): |
| 67 | + """ |
| 68 | + Read the filterbank data as stream |
| 69 | + and measure the time |
| 70 | + """ |
| 71 | + fil = filterbank.Filterbank(filename) |
| 72 | + stopwatch_list = list() |
| 73 | + while True: |
| 74 | + stopwatch = dict.fromkeys(['time_read', 'time_select', 'time_clipping', 'time_dedisp', |
| 75 | + 'time_t_series', 'time_downsample', 'time_fft_vect', |
| 76 | + 'time_dft', 'time_ifft', 'time_fft_freq']) |
| 77 | + time_start = timer() |
| 78 | + fil_data = fil.next_n_rows(n) |
| 79 | + # break if EOF |
| 80 | + if isinstance(fil_data, bool): |
| 81 | + break |
| 82 | + stopwatch['time_read'] = timer() - time_start |
| 83 | + # run methods |
| 84 | + stopwatch = self.measure_methods(stopwatch, fil_data, fil.freqs, DM, scale) |
| 85 | + stopwatch_list.append(stopwatch) |
| 86 | + return stopwatch_list |
| 87 | + |
| 88 | + |
| 89 | + def read_static(self, filename, DM, scale, size): |
| 90 | + """ |
| 91 | + Read the filterbank data at once |
| 92 | + and measure the time per function/class |
| 93 | + """ |
| 94 | + stopwatch = dict.fromkeys(['time_read', 'time_select', 'time_clipping', 'time_dedisp', |
| 95 | + 'time_t_series', 'time_downsample', 'time_fft_vect', 'time_dft', |
| 96 | + 'time_ifft', 'time_fft_freq']) |
| 97 | + time_start = timer() |
| 98 | + # init filterbank |
| 99 | + fil = filterbank.Filterbank(filename, read_all=True, time_range=(0, size)) |
| 100 | + stopwatch['time_read'] = timer() - time_start |
| 101 | + # select data |
| 102 | + time_select = timer() |
| 103 | + freqs, fil_data = fil.select_data() |
| 104 | + stopwatch['time_select'] = timer() - time_select |
| 105 | + # run methods |
| 106 | + stopwatch = self.measure_methods(stopwatch, fil_data, freqs, DM, scale) |
| 107 | + return stopwatch |
| 108 | + |
| 109 | + |
| 110 | + def measure_methods(self, stopwatch, fil_data, freqs, DM, scale): |
| 111 | + """ |
| 112 | + Run and time all methods/modules |
| 113 | + """ |
| 114 | + # clipping |
| 115 | + time_clipping = timer() |
| 116 | + _, _ = clipping.clipping(freqs, fil_data) |
| 117 | + stopwatch['time_clipping'] = timer() - time_clipping |
| 118 | + # dedisperse |
| 119 | + time_dedisp = timer() |
| 120 | + fil_data = dedisperse.dedisperse(fil_data, DM) |
| 121 | + stopwatch['time_dedisp'] = timer() - time_dedisp |
| 122 | + # timeseries |
| 123 | + time_t_series = timer() |
| 124 | + time_series = timeseries.Timeseries(fil_data) |
| 125 | + stopwatch['time_t_series'] = timer() - time_t_series |
| 126 | + # downsample |
| 127 | + time_downsamp = timer() |
| 128 | + time_series = time_series.downsample(scale) |
| 129 | + stopwatch['time_downsample'] = timer() - time_downsamp |
| 130 | + # fft vect |
| 131 | + time_fft_vect = timer() |
| 132 | + fourier.fft_vectorized(time_series) |
| 133 | + stopwatch['time_fft_vect'] = timer() - time_fft_vect |
| 134 | + # dft |
| 135 | + time_dft = timer() |
| 136 | + fourier.dft_slow(time_series) |
| 137 | + stopwatch['time_dft'] = timer() - time_dft |
| 138 | + # ifft |
| 139 | + time_ifft = timer() |
| 140 | + fourier.ifft(time_series) |
| 141 | + stopwatch['time_ifft'] = timer() - time_ifft |
| 142 | + # fft freq |
| 143 | + time_fft_freq = timer() |
| 144 | + fourier.fft_freq(10) |
| 145 | + stopwatch['time_fft_freq'] = timer() - time_fft_freq |
| 146 | + return stopwatch |
0 commit comments