def read_timeseries(self, input_path) -> Timeseries: timeseries = Timeseries() with open(input_path, 'r') as file: reader = csv.reader(file, delimiter=',') line_count = 0 for row in reader: if line_count > 0: timeseries.push(*row) line_count += 1 return timeseries
def write_timeseries(self, timeseries: Timeseries, output_path: str): data = list(timeseries.get_zip()) field_names = timeseries.get_attributes() with open(output_path, 'wt') as file: writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow(field_names) for ts in data: writer.writerow(ts)
def average(self, raster_paths: List[str]) -> Timeseries: timeseries = Timeseries() raster_paths.sort() for raster_path in raster_paths: data = self.tif_reader.read_tif(raster_path) avg = np.nanmean(data) date_str = get_date_from_filename( get_filename_from_path(raster_path)) timeseries.push(date_str, avg) return timeseries
def get_all_reference_timeseries(self, build: bool = False, plot: bool = False) -> List[Timeseries]: return [ Timeseries(['2019-01', '2019-02', '2019-03'], [254. / 255, 253. / 255, 255. / 255], 'broadleaf', 'VH'), Timeseries(['2019-01', '2019-02', '2019-03'], [252. / 255, 251. / 255, 253. / 255], 'broadleaf', 'VV'), Timeseries(['2019-01', '2019-02', '2019-03'], [0. / 255, 1. / 255, 5. / 255], 'coniferous', 'VH'), Timeseries(['2019-01', '2019-02', '2019-03'], [2. / 255, 3. / 255, 4. / 255], 'coniferous', 'VV'), ]
def plot_multiple_timeseries(self, timeseries: List[Timeseries], figsize: Tuple[int, int] = None, save_path: str = None): figsize = figsize if figsize else (10, 10) num_ts = len(timeseries) x: np.array = None ts: Timeseries = Timeseries() plt.figure(figsize=figsize) for idx, ts in enumerate(timeseries): ts_size = ts.get_size() x = np.arange(ts_size) # TODO ylim should be same for all plots plt.subplot(num_ts, 1, idx + 1) plt.ylabel('Mean sig0 [dB]') plt.title(ts.get_description()) plt.xticks([], []) plt.plot(x, ts.sig0s) plt.xticks(x[::3], ts.dates[::3], rotation=20) plt.xlabel('Date') if save_path: plt.savefig(save_path) else: plt.show()
def get_rmsd(self, reference_timeseries: Timeseries, actual_paths: List[str]) -> np.array: rmsd_cubes = [] raster_segmenter = RasterSegmenter() ts_size = reference_timeseries.get_size() print(f'Timeseries: {reference_timeseries.get_description()}') counter = 1 cube = raster_segmenter.get_next_cube(actual_paths) timeseries_array = np.array(reference_timeseries.sig0s) while cube: print(f'RMSD Segment Counter: {counter}') cube.data = np.square(cube.data - timeseries_array[None, None, :]) # per pixel cube.data = np.nansum(cube.data, axis=2) # sums all nan values to 0 cube.data[cube.data == 0] = np.nan # set nan sums to nan again cube.data = np.sqrt(cube.data / ts_size) # combining pixel rmsd_cubes.append(cube) counter += 1 cube = raster_segmenter.get_next_cube(actual_paths) raster = raster_segmenter.get_rmsd_from_cubes(rmsd_cubes) del rmsd_cubes return raster
def test_csv_read_write(): # Paths output_path = get_filepath(test_folder, 'csv_read_write.csv') # timeseries dates = ['201901', '201902', '201801', '201807'] sig0 = [12345, 3452, 12345, 9875] input_timeseries = Timeseries(dates, sig0) csv_read_writer = CsvReaderWriter() csv_read_writer.write_timeseries(input_timeseries, output_path) output_timeseries = csv_read_writer.read_timeseries(output_path) assert input_timeseries == output_timeseries