From a26aa9b98592d164695619103ce0cfda8efa6760 Mon Sep 17 00:00:00 2001 From: Theodore Chang Date: Sat, 14 Dec 2024 09:53:34 +0100 Subject: [PATCH] Add method to normalise locally Signed-off-by: Theodore Chang --- src/mb/app/response.py | 15 ++++++++++++++- src/mb/client.py | 16 +++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/mb/app/response.py b/src/mb/app/response.py index bc771c9..5000324 100644 --- a/src/mb/app/response.py +++ b/src/mb/app/response.py @@ -95,12 +95,23 @@ class RecordResponse(RawRecordResponse): @model_validator(mode="before") @classmethod - def normalise(cls, values): + def default_unit(cls, values): if values.get("processed_data_unit", None): values["processed_data_unit"] = values["raw_data_unit"] return values + def normalise(self): + if self.waveform is None: + raise RuntimeError("Cannot normalise the waveform.") + + wave_array = np.array(self.waveform) + self.waveform = (wave_array / np.max(np.abs(wave_array))).tolist() + + self.processed_data_unit = "1" + + return self + def filter(self, window, up_ratio: int = 1): self.time_interval /= up_ratio # noinspection PyTypeChecker @@ -108,6 +119,8 @@ def filter(self, window, up_ratio: int = 1): window * up_ratio, zero_stuff(up_ratio, self.waveform) ).tolist() + return self + def to_spectrum(self): if self.time_interval is None or self.waveform is None: raise RuntimeError("Cannot convert to spectrum.") diff --git a/src/mb/client.py b/src/mb/client.py index dd83f6a..e7cdd69 100644 --- a/src/mb/client.py +++ b/src/mb/client.py @@ -29,7 +29,7 @@ from rich.console import Console from rich.progress import track -from mb.app.response import QueryConfig, RecordResponse +from mb.app.response import QueryConfig, RecordResponse, PaginationConfig class MBRecord(RecordResponse): @@ -37,7 +37,7 @@ def plot_waveform(self, fig: Figure | None = None): if fig is None: fig = plt.figure() gca = fig.add_subplot(111) - gca.set_xlabel("Frequency (Hz)") + gca.set_xlabel("Time (s)") gca.set_ylabel(f"Acceleration Magnitude ({self.processed_data_unit})") else: gca = fig.gca() @@ -290,14 +290,16 @@ async def status(self): async def main(): - async with MBClient("http://170.64.176.26:8000") as client: - results = await client.search(QueryConfig()) + async with MBClient("http://170.64.176.26:8000", timeout=100) as client: + results = await client.search( + QueryConfig(pagination=PaginationConfig(page_size=100)) + ) fig: Figure = None # type: ignore for result in await client.download(results): - fig = result.plot_spectrum(fig) - fig.legend() + fig = result.normalise().plot_waveform(fig) + # fig.legend() fig.tight_layout() - fig.savefig("spectrum.png") + fig.savefig("waveform.png") if __name__ == "__main__":