From 2e906aadc0fc5dc64ba92cdd803bbb72a9332984 Mon Sep 17 00:00:00 2001 From: bozzlab <41643434+bozzlab@users.noreply.github.com> Date: Mon, 18 Apr 2022 10:02:15 +0700 Subject: [PATCH] Feature: Rename point -> index, evaluate -> calculate (#1) --- README.md | 2 +- pyacoustics_stc/constant.py | 36 ++++++++-------- pyacoustics_stc/sound_tranmission_class.py | 48 +++++++++++----------- tests/test_sound_transmission_class.py | 10 ++--- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index bfd29cf..38f77a0 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ stl = { stc = SoundTransmissionClass(stl=stl) -stc.point +stc.index # 29 stc.deficiency # 25.579 diff --git a/pyacoustics_stc/constant.py b/pyacoustics_stc/constant.py index 4e6739d..883aed0 100644 --- a/pyacoustics_stc/constant.py +++ b/pyacoustics_stc/constant.py @@ -6,25 +6,25 @@ MAX_DIGIT = 5 STC_CONTOURS: Dict[int, Dict[int, float]] = { - stc_point: { - 125: stc_point - 16, - 160: stc_point - 13, - 200: stc_point - 10, - 250: stc_point - 7, - 315: stc_point - 4, - 400: stc_point - 1, - 500: stc_point + 0, - 630: stc_point + 1, - 800: stc_point + 2, - 1000: stc_point + 3, - 1250: stc_point + 4, - 1600: stc_point + 4, - 2000: stc_point + 4, - 2500: stc_point + 4, - 3150: stc_point + 4, - 4000: stc_point + 4, + stc_index: { + 125: stc_index - 16, + 160: stc_index - 13, + 200: stc_index - 10, + 250: stc_index - 7, + 315: stc_index - 4, + 400: stc_index - 1, + 500: stc_index + 0, + 630: stc_index + 1, + 800: stc_index + 2, + 1000: stc_index + 3, + 1250: stc_index + 4, + 1600: stc_index + 4, + 2000: stc_index + 4, + 2500: stc_index + 4, + 3150: stc_index + 4, + 4000: stc_index + 4, } - for stc_point in range(150) # STC range (0 -> 149) + for stc_index in range(150) # STC range (0 -> 149) } FREQUENCY_BAND: List[int] = list(STC_CONTOURS[0].keys()) diff --git a/pyacoustics_stc/sound_tranmission_class.py b/pyacoustics_stc/sound_tranmission_class.py index c3337c7..08d3dd4 100644 --- a/pyacoustics_stc/sound_tranmission_class.py +++ b/pyacoustics_stc/sound_tranmission_class.py @@ -15,22 +15,22 @@ class SoundTransmissionClass: def __init__(self, stl: Dict[int, float]): self.stl = stl - self._stc_point: int = None + self._stc_index: int = None self._stc_contour: Dict[int, float] = {} self._deficiency: int = None self._delta: Dict[int, float] = {} self._stl_stc_delta_contours: Dict[int, float] = {} - self._evaluate() + self._calculate() @property def contour(self) -> Dict[int, float]: """ - STC contour of STC point. + STC contour of STC index. example: - STC Point = 20 + STC index = 20 return: { @@ -58,11 +58,11 @@ def delta(self) -> Dict[int, float]: return self._delta @property - def point(self) -> int: + def index(self) -> int: """ - STC Point in range 0 -> 149. + STC index in range 0 -> 149. """ - return self._stc_point + return self._stc_index def _plot(self) -> plt: x_axis_frequency = [] @@ -78,7 +78,7 @@ def _plot(self) -> plt: plt.figure(figsize=(18, 8)) # plot values - plt.plot(y_axis_stc, "r--", label=f"STC {self._stc_point}") + plt.plot(y_axis_stc, "r--", label=f"STC {self._stc_index}") plt.plot(y_axis_stl, "b", label="STL") # config @@ -114,35 +114,35 @@ def export_graph_result(self, filename: str): plt = self._plot() plt.savefig(f"{filename}") - def _evaluate(self): + def _calculate(self): stl_stc_delta_contours = self._build_stl_stc_delta_contours() filtered_delta_contours = self._filter_delta_contours(stl_stc_delta_contours) - self._stc_point = self._get_stc_point(filtered_delta_contours) + self._stc_index = self._get_stc_index(filtered_delta_contours) self._delta = { freq: round(value, MAX_DIGIT) - for freq, value in filtered_delta_contours[self._stc_point].items() + for freq, value in filtered_delta_contours[self._stc_index].items() } self._deficiency = round( sum( [ stc_value - for stc_value in filtered_delta_contours[self._stc_point].values() + for stc_value in filtered_delta_contours[self._stc_index].values() ] ), MAX_DIGIT, ) - self._stc_contour = STC_CONTOURS[self._stc_point] + self._stc_contour = STC_CONTOURS[self._stc_index] def _build_stl_stc_delta_contours(self) -> Dict[int, float]: """ Building STL and STC delta contours. - Calculate delta between STL and STC value for each STC point and frequency. + Calculate delta between STL and STC value for each STC index and frequency. Structure data of `stl_stc_delta_contours`: { - stc_point (int): { + str_raint (int): { Freq (int): delta_value (float) } } @@ -167,15 +167,15 @@ def _build_stl_stc_delta_contours(self) -> Dict[int, float]: """ stl_stc_delta_contours: Dict[int, float] = {} - for stc_point, stc_contour in STC_CONTOURS.items(): - stl_stc_delta_contours[stc_point] = {} + for _stc_index, stc_contour in STC_CONTOURS.items(): + stl_stc_delta_contours[_stc_index] = {} for freq, value in stc_contour.items(): if self.stl[freq] < value: - stl_stc_delta_contours[stc_point][freq] = abs( + stl_stc_delta_contours[_stc_index][freq] = abs( self.stl[freq] - value ) else: - stl_stc_delta_contours[stc_point][freq] = 0 + stl_stc_delta_contours[_stc_index][freq] = 0 return stl_stc_delta_contours @@ -190,21 +190,21 @@ def _filter_delta_contours( filtered_delta_contours: Dict[int, float] = {} filtered_sum_delta_contours: Dict[int, float] = { - stc_point: stc_contour - for stc_point, stc_contour in stl_stc_delta_contours.items() + _stc_index: stc_contour + for _stc_index, stc_contour in stl_stc_delta_contours.items() if sum([stc_value for stc_value in stc_contour.values()]) <= MAX_SUM_CONTOUR } - for stc_point, stc_contour in filtered_sum_delta_contours.items(): + for _stc_index, stc_contour in filtered_sum_delta_contours.items(): filtered_values = [ value for value in stc_contour.values() if value < MAX_DELTA_PER_FREQUENCY ] if len(filtered_values) == len(FREQUENCY_BAND): - filtered_delta_contours[stc_point] = stc_contour + filtered_delta_contours[_stc_index] = stc_contour return filtered_delta_contours - def _get_stc_point(self, filtered_delta_contours: Dict[int, float]) -> int: + def _get_stc_index(self, filtered_delta_contours: Dict[int, float]) -> int: return max(filtered_delta_contours.keys()) diff --git a/tests/test_sound_transmission_class.py b/tests/test_sound_transmission_class.py index 43cb83e..b3033fd 100644 --- a/tests/test_sound_transmission_class.py +++ b/tests/test_sound_transmission_class.py @@ -45,9 +45,9 @@ ] # Expect value -stc_point = 29 +stc_index = 29 stc_deficiency = 25.579 -stc_contour = STC_CONTOURS[stc_point] +stc_contour = STC_CONTOURS[stc_index] @pytest.fixture @@ -60,9 +60,9 @@ def frequency_stl_map(): return build_frequency_stl_map(stl_without_key) -def test_stc_point(sound_tranmission_class): - assert isinstance(stc_point, int) - assert stc_point == sound_tranmission_class.point +def test_stc_index(sound_tranmission_class): + assert isinstance(stc_index, int) + assert stc_index == sound_tranmission_class.index def test_stc_contour(sound_tranmission_class):