Spaces:
Sleeping
Sleeping
umyuu
commited on
Commit
•
afb0c77
1
Parent(s):
d0f7ec1
Stopwatchクラスのリファクタリング
Browse files- app.py +1 -1
- src/__init__.py +2 -2
- src/myapp.py +7 -6
- src/reporter.py +1 -0
- src/saliency.py +1 -1
- src/utils.py +26 -11
app.py
CHANGED
@@ -5,12 +5,12 @@
|
|
5 |
from argparse import ArgumentParser, BooleanOptionalAction
|
6 |
|
7 |
from src import PROGRAM_NAME, get_package_version
|
8 |
-
from src.myapp import run_app
|
9 |
|
10 |
__version__ = get_package_version()
|
11 |
|
12 |
|
13 |
def main():
|
|
|
14 |
"""
|
15 |
エントリーポイント
|
16 |
1, コマンドライン引数の解析を行います
|
|
|
5 |
from argparse import ArgumentParser, BooleanOptionalAction
|
6 |
|
7 |
from src import PROGRAM_NAME, get_package_version
|
|
|
8 |
|
9 |
__version__ = get_package_version()
|
10 |
|
11 |
|
12 |
def main():
|
13 |
+
from src.myapp import run_app
|
14 |
"""
|
15 |
エントリーポイント
|
16 |
1, コマンドライン引数の解析を行います
|
src/__init__.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""src Module"""
|
3 |
from datetime import datetime
|
4 |
-
import logging
|
5 |
import re
|
6 |
from zoneinfo import ZoneInfo
|
7 |
|
@@ -35,7 +35,7 @@ class LocalTimeFormatter(logging.Formatter):
|
|
35 |
|
36 |
t = dt.strftime(re.sub(self.pattern, "", datefmt))
|
37 |
match = re.search(self.pattern, datefmt)
|
38 |
-
if
|
39 |
return t
|
40 |
|
41 |
groups = match.groups()
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
"""src Module"""
|
3 |
from datetime import datetime
|
4 |
+
import logging
|
5 |
import re
|
6 |
from zoneinfo import ZoneInfo
|
7 |
|
|
|
35 |
|
36 |
t = dt.strftime(re.sub(self.pattern, "", datefmt))
|
37 |
match = re.search(self.pattern, datefmt)
|
38 |
+
if match is None:
|
39 |
return t
|
40 |
|
41 |
groups = match.groups()
|
src/myapp.py
CHANGED
@@ -56,23 +56,24 @@ def submit_clicked(image: np.ndarray, algorithm: Literal["SpectralResidual", "Fi
|
|
56 |
np.ndarray: JET画像
|
57 |
np.ndarray: HOT画像
|
58 |
"""
|
59 |
-
|
60 |
-
|
61 |
#
|
62 |
saliency = SaliencyMap(algorithm)
|
|
|
63 |
success, saliency_map = saliency.compute(image)
|
64 |
-
|
65 |
|
66 |
if not success:
|
67 |
return image, image # エラーが発生した場合は入力画像を返します。
|
68 |
|
69 |
-
|
70 |
jet = convert_colormap(image, saliency_map, "jet")
|
71 |
# jet = None
|
72 |
-
|
73 |
hot = convert_colormap(image, saliency_map, "hot")
|
74 |
saliency = None
|
75 |
-
log.info(f"#
|
76 |
return jet, hot
|
77 |
|
78 |
|
|
|
56 |
np.ndarray: JET画像
|
57 |
np.ndarray: HOT画像
|
58 |
"""
|
59 |
+
sw = Stopwatch.start_new()
|
60 |
+
log.info(f"#submit_clicked({sw.elapsed:.3f}s)")
|
61 |
#
|
62 |
saliency = SaliencyMap(algorithm)
|
63 |
+
log.info(f"#SaliencyMap({sw.elapsed:.3f}s)")
|
64 |
success, saliency_map = saliency.compute(image)
|
65 |
+
log.info(f"#compute({sw.elapsed:.3f}s)")
|
66 |
|
67 |
if not success:
|
68 |
return image, image # エラーが発生した場合は入力画像を返します。
|
69 |
|
70 |
+
log.info(f"#jet({sw.elapsed:.3f}s)")
|
71 |
jet = convert_colormap(image, saliency_map, "jet")
|
72 |
# jet = None
|
73 |
+
log.info(f"#hot({sw.elapsed:.3f}s)")
|
74 |
hot = convert_colormap(image, saliency_map, "hot")
|
75 |
saliency = None
|
76 |
+
log.info(f"#submit_clicked({sw.elapsed:.3f}s)")
|
77 |
return jet, hot
|
78 |
|
79 |
|
src/reporter.py
CHANGED
@@ -11,6 +11,7 @@
|
|
11 |
import json
|
12 |
from logging import Logger, getLogger
|
13 |
import logging.config
|
|
|
14 |
from typing import Optional
|
15 |
|
16 |
from . import PROGRAM_NAME
|
|
|
11 |
import json
|
12 |
from logging import Logger, getLogger
|
13 |
import logging.config
|
14 |
+
|
15 |
from typing import Optional
|
16 |
|
17 |
from . import PROGRAM_NAME
|
src/saliency.py
CHANGED
@@ -10,7 +10,7 @@ class SaliencyMap:
|
|
10 |
"""
|
11 |
顕著性マップを計算するクラス。
|
12 |
Example:
|
13 |
-
from
|
14 |
|
15 |
saliency = SaliencyMap("SpectralResidual")
|
16 |
success, saliencyMap = saliency.compute(image)
|
|
|
10 |
"""
|
11 |
顕著性マップを計算するクラス。
|
12 |
Example:
|
13 |
+
from src.saliency import SaliencyMap
|
14 |
|
15 |
saliency = SaliencyMap("SpectralResidual")
|
16 |
success, saliencyMap = saliency.compute(image)
|
src/utils.py
CHANGED
@@ -7,29 +7,34 @@ def get_package_version() -> str:
|
|
7 |
"""
|
8 |
バージョン情報
|
9 |
"""
|
10 |
-
return '0.0.
|
11 |
|
12 |
|
13 |
class Stopwatch:
|
14 |
"""
|
15 |
-
|
16 |
Example:
|
17 |
from src.utils import Stopwatch
|
18 |
|
19 |
watch = Stopwatch.start_new()
|
20 |
-
|
21 |
-
print(f"{watch.
|
22 |
"""
|
23 |
|
24 |
def __init__(self):
|
25 |
-
self._start_time = 0
|
26 |
-
self._elapsed = 0
|
|
|
27 |
|
28 |
@property
|
29 |
-
def elapsed(self):
|
30 |
"""
|
31 |
-
|
32 |
"""
|
|
|
|
|
|
|
|
|
33 |
return self._elapsed
|
34 |
|
35 |
def start(self) -> None:
|
@@ -38,6 +43,7 @@ class Stopwatch:
|
|
38 |
"""
|
39 |
self._start_time = time.perf_counter()
|
40 |
self._elapsed = 0
|
|
|
41 |
|
42 |
@classmethod
|
43 |
def start_new(cls):
|
@@ -48,10 +54,19 @@ class Stopwatch:
|
|
48 |
stopwatch.start()
|
49 |
return stopwatch
|
50 |
|
51 |
-
def stop(self):
|
52 |
"""
|
53 |
計測を終了します。
|
54 |
"""
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
return self._elapsed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
"""
|
8 |
バージョン情報
|
9 |
"""
|
10 |
+
return '0.0.7'
|
11 |
|
12 |
|
13 |
class Stopwatch:
|
14 |
"""
|
15 |
+
経過時間を計測するためのクラス。
|
16 |
Example:
|
17 |
from src.utils import Stopwatch
|
18 |
|
19 |
watch = Stopwatch.start_new()
|
20 |
+
### 計測する処理
|
21 |
+
print(f"{watch.elapsed:.3f}")
|
22 |
"""
|
23 |
|
24 |
def __init__(self):
|
25 |
+
self._start_time: float = 0
|
26 |
+
self._elapsed: float = 0
|
27 |
+
self._is_running: bool = False
|
28 |
|
29 |
@property
|
30 |
+
def elapsed(self) -> float:
|
31 |
"""
|
32 |
+
経過時間を取得します。
|
33 |
"""
|
34 |
+
if self._is_running:
|
35 |
+
end_time = time.perf_counter()
|
36 |
+
self._elapsed = end_time - self._start_time
|
37 |
+
|
38 |
return self._elapsed
|
39 |
|
40 |
def start(self) -> None:
|
|
|
43 |
"""
|
44 |
self._start_time = time.perf_counter()
|
45 |
self._elapsed = 0
|
46 |
+
self._is_running = True
|
47 |
|
48 |
@classmethod
|
49 |
def start_new(cls):
|
|
|
54 |
stopwatch.start()
|
55 |
return stopwatch
|
56 |
|
57 |
+
def stop(self) -> float:
|
58 |
"""
|
59 |
計測を終了します。
|
60 |
"""
|
61 |
+
if self._is_running:
|
62 |
+
end_time = time.perf_counter()
|
63 |
+
self._elapsed = end_time - self._start_time
|
64 |
+
self._is_running = False
|
65 |
return self._elapsed
|
66 |
+
|
67 |
+
@property
|
68 |
+
def is_running(self) -> bool:
|
69 |
+
"""
|
70 |
+
実行中かどうかを取得します。
|
71 |
+
"""
|
72 |
+
return self._is_running
|