Skip to content

Commit

Permalink
Add method to normalise locally
Browse files Browse the repository at this point in the history
Signed-off-by: Theodore Chang <[email protected]>
  • Loading branch information
TLCFEM committed Dec 14, 2024
1 parent df817bf commit a26aa9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/mb/app/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,32 @@ 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
self.waveform = apply_filter(
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.")
Expand Down
16 changes: 9 additions & 7 deletions src/mb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
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):
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()
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit a26aa9b

Please sign in to comment.