Skip to content

Commit

Permalink
Feature: Rename point -> index, evaluate -> calculate (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bozzlab authored Apr 18, 2022
1 parent c51ddd9 commit 2e906aa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stl = {

stc = SoundTransmissionClass(stl=stl)

stc.point
stc.index
# 29
stc.deficiency
# 25.579
Expand Down
36 changes: 18 additions & 18 deletions pyacoustics_stc/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
48 changes: 24 additions & 24 deletions pyacoustics_stc/sound_tranmission_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down Expand Up @@ -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 = []
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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

Expand All @@ -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())
10 changes: 5 additions & 5 deletions tests/test_sound_transmission_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 2e906aa

Please sign in to comment.