Skip to content

Commit a8573d4

Browse files
committed
Add hydra example.
1 parent 33e650d commit a8573d4

5 files changed

Lines changed: 57 additions & 18 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
project_env/
22
.DS_Store
33

4+
# from hydra
5+
outputs/
6+
47
# Byte-compiled / optimized / DLL files
58
__pycache__/
69
*.py[cod]

‎examples/configs/defaults.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
hydra:
2+
job:
3+
chdir: True # change to output folder
4+
job_logging:
5+
formatters:
6+
simple:
7+
format: '[%(levelname)s] - %(message)s'
8+
9+
seed: 0
10+
11+
signal_len: 1000
12+
filter_len: 10

‎examples/configs/exp1.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# python examples/real_convolve.py -cn exp1
2+
defaults:
3+
- defaults
4+
- _self_
5+
6+
filter_len: 25

‎examples/real_convolve.py‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
33
from pydevtips.fftconvolve import RFFTConvolve
4+
import hydra
5+
import os
46

57

6-
# Create a signal
7-
signal = np.random.randn(1000)
8+
@hydra.main(version_base=None, config_path="configs", config_name="defaults")
9+
def main(config):
810

9-
# Create a moving average filter (low pass)
10-
n = 10
11-
filter = np.ones(n) / n
11+
output_dir = os.getcwd()
12+
seed = config.seed
13+
np.random.seed(seed)
1214

13-
# initialize the convolver
14-
convolver = RFFTConvolve(filter=filter, length=len(signal))
15+
# Create a signal
16+
signal = np.random.randn(config.signal_len)
1517

16-
# convolve
17-
convolved_signal = convolver(signal)
18+
# Create a moving average filter (low pass)
19+
n = config.filter_len
20+
filter = np.ones(n) / n
1821

19-
# plot with three subplots
20-
fig, ax = plt.subplots(3, 1, figsize=(10, 10))
21-
ax[0].plot(signal)
22-
ax[0].set_title("Signal")
23-
ax[1].plot(filter)
24-
ax[1].set_title("Filter")
25-
ax[2].plot(convolved_signal)
26-
ax[2].set_title("Convolved Signal")
22+
# initialize the convolver
23+
convolver = RFFTConvolve(filter=filter, length=len(signal))
2724

28-
plt.show()
25+
# convolve
26+
convolved_signal = convolver(signal)
27+
28+
# plot with three subplots
29+
_, ax = plt.subplots(3, 1, figsize=(10, 10))
30+
ax[0].plot(signal)
31+
ax[0].set_title("Signal")
32+
ax[1].plot(filter)
33+
ax[1].set_title("Filter")
34+
ax[2].plot(convolved_signal)
35+
ax[2].set_title("Convolved Signal")
36+
37+
plt.savefig(os.path.join(output_dir, "convolved_signal.png"))
38+
output_fp = os.path.join(output_dir, "convolved_signal.png")
39+
print(f"Saved convolved signal to {output_fp}")
40+
41+
plt.show()
42+
43+
44+
if __name__ == "__main__":
45+
main()

‎setup.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
install_requires=[
2222
"numpy",
2323
"matplotlib",
24+
"hydra-core",
2425
],
2526
include_package_data=True,
2627
)

0 commit comments

Comments
 (0)