8000 Use sfs.default object instead of sfs.defs submodule · sfstoolbox/sfs-python@9ff63ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ff63ae

Browse files
committed
Use sfs.default object instead of sfs.defs submodule
Closes #32.
1 parent d13ff58 commit 9ff63ae

File tree

11 files changed

+71
-38
lines changed

11 files changed

+71
-38
lines changed

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ API Documentation
1212
time
1313
plot
1414
util
15+
default

doc/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import os
1818
from subprocess import check_output
1919

20+
import sphinx
21+
2022
# If extensions (or modules to document with autodoc) are in another directory,
2123
# add these directories to sys.path here. If the directory is relative to the
2224
# documentation root, use os.path.abspath to make it absolute, like shown here.
@@ -48,6 +50,9 @@
4850
"--InlineBackend.rc={'figure.dpi': 96}",
4951
]
5052

53+
# Tell autodoc that the documentation is being generated
54+
sphinx.SFS_DOCS_ARE_BEING_BUILT = True
55+
5156
autoclass_content = 'init'
5257
autodoc_member_order = 'bysource'
5358
autodoc_default_flags = ['members', 'undoc-members']

sfs/__init__.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,47 @@
55
"""
66
__version__ = "0.4.0"
77

8+
9+
class default:
10+
"""Get/set defaults for the *sfs* module.
11+
12+
For example, when you want to change the default speed of sound::
13+
14+
import sfs
15+
sfs.default.c = 330
16+
17+
"""
18+
19+
c = 343
20+
"""Speed of sound."""
21+
22+
rho0 = 1.2250
23+
"""Static density of air."""
24+
25+
selection_tolerance = 1e-6
26+
"""Tolerance used for secondary source selection."""
27+
28+
def __setattr__(self, name, value):
29+
"""Only allow setting existing attributes."""
30+
if name in dir(self) and name != 'reset':
31+
super().__setattr__(name, value)
32+
else:
33+
raise AttributeError(
34+
'"default" object has no attribute ' + repr(name))
35+
36+
def reset(self):
37+
"""Reset all attributes to their "factory default"."""
38+
vars(self).clear()
39+
40+
41+
import sys as _sys
42+
if not getattr(_sys.modules.get('sphinx'), 'SFS_DOCS_ARE_BEING_BUILT', False):
43+
# This object shadows the 'default' class, except when the docs are built:
44+
default = default()
45+
846
from . import tapering
947
from . import array
1048
from . import util
11-
from . import defs
1249
try:
1350
from . import plot
1451
except ImportError:

sfs/defs.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

sfs/mono/drivingfunction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def plot(d, selected):
3636
from numpy.core.umath_tests import inner1d # element-wise inner product
3737
from scipy.special import jn, hankel2
3838
from .. import util
39-
from .. import defs
39+
from .. import default
4040

4141

4242
def wfs_2d_line(omega, x0, n0, xs, c=None):
@@ -291,7 +291,7 @@ def source_selection_plane(n0, n):
291291
"""
292292
n0 = util.asarray_of_rows(n0)
293293
n = util.normalize_vector(n)
294-
return np.inner(n, n0) >= defs.selection_tolerance
294+
return np.inner(n, n0) >= default.selection_tolerance
295295

296296

297297
def source_selection_point(n0, x0, xs):
@@ -304,7 +304,7 @@ def source_selection_point(n0, x0, xs):
304304
x0 = util.asarray_of_rows(x0)
305305
xs = util.asarray_1d(xs)
306306
ds = x0 - xs
307-
return inner1d(ds, n0) >= defs.selection_tolerance
307+
return inner1d(ds, n0) >= default.selection_tolerance
308308

309309

310310
def source_selection_line(n0, x0, xs):
@@ -326,7 +326,7 @@ def source_selection_focused(ns, x0, xs):
326326
xs = util.asarray_1d(xs)
327327
ns = util.normalize_vector(ns)
328328
ds = xs - x0
329-
return inner1d(ns, ds) >= defs.selection_tolerance
329+
return inner1d(ns, ds) >= default.selection_tolerance
330330

331331

332332
def source_selection_all(N):

sfs/mono/source.py

Lines changed: 11 additions & 11 deletions
< 741A td data-grid-cell-id="diff-803c8b7afeac3a040dd039895d156d2389bdfe27757626f0940fe38285128cac-131-131-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">131
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
normalization_point = 4 * np.pi
1818
normalization_line = \\
19-
np.sqrt(8 * np.pi * omega / sfs.defs.c) * np.exp(1j * np.pi / 4)
19+
np.sqrt(8 * np.pi * omega / sfs.default.c) * np.exp(1j * np.pi / 4)
2020
2121
grid = sfs.util.xyz_grid([-2, 3], [-1, 2], 0, spacing=0.02)
2222
@@ -29,7 +29,7 @@
2929
import numpy as np
3030
from scipy import special
3131
from .. import util
32-
from .. import defs
32+
from .. import default
3333

3434

3535
def point(omega, x0, n0, grid, c=None):
@@ -123,14 +123,14 @@ def point_velocity(omega, x0, n0, grid, c=None):
123123
124124
"""
125125
if c is None:
126-
c = defs.c
126+
c = default.c
127127
k = util.wavenumber(omega, c)
128128
x0 = util.asarray_1d(x0)
129129
grid = util.as_xyz_components(grid)
130130
offset = grid - x0
131
r = np.linalg.norm(offset)
132132
v = point(omega, x0, n0, grid, c=c)
133-
v *= (1+1j*k*r) / (defs.rho0 * c * 1j*k*r)
133+
v *= (1+1j*k*r) / (default.rho0 * c * 1j*k*r)
134134
return util.XyzComponents([v * o / r for o in offset])
135135

136136

@@ -160,7 +160,7 @@ def point_averaged_intensity(omega, x0, n0, grid, c=None):
160160
grid = util.as_xyz_components(grid)
161161
offset = grid - x0
162162
r = np.linalg.norm(offset)
163-
i = 1 / (2*defs.rho0 * defs.c)
163+
i = 1 / (2 * default.rho0 * default.c)
164164
return util.XyzComponents([i * o / r**2 for o in offset])
165165

166166

@@ -466,14 +466,14 @@ def line_velocity(omega, x0, n0, grid, c=None):
466466
467467
"""
468468
if c is None:
469-
c = defs.c
469+
c = default.c
470470
k = util.wavenumber(omega, c)
471471
x0 = util.asarray_1d(x0)[:2] # ignore z-component
472472
grid = util.as_xyz_components(grid)
473473

474474
offset = grid[:2] - x0
475475
r = np.linalg.norm(offset)
476-
v = -1/(4*c*defs.rho0) * special.hankel2(1, k * r)
476+
v = -1/(4*c*default.rho0) * special.hankel2(1, k * r)
477477
v = [v * o / r for o in offset]
478478

479479
assert v[0].shape == v[1].shape
@@ -662,8 +662,8 @@ def plane_velocity(omega, x0, n0, grid, c=None):
662662
663663
"""
664664
if c is None:
665-
c = defs.c
666-
v = plane(omega, x0, n0, grid, c=c) / (defs.rho0 * c)
665+
c = default.c
666+
v = plane(omega, x0, n0, grid, c=c) / (default.rho0 * c)
667667
return util.XyzComponents([v * n for n in n0])
668668

669669

@@ -697,8 +697,8 @@ def plane_averaged_intensity(omega, x0, n0, grid, c=None):
697697
698698
"""
699699
if c is None:
700-
c = defs.c
701-
i = 1 / (2 * defs.rho0 * c)
700+
c = default.c
701+
i = 1 / (2 * default.rho0 * c)
702702
return util.XyzComponents([i * n for n in n0])
703703

704704

sfs/plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mpl_toolkits.mplot3d import Axes3D
99
import numpy as np
1010
from . import util
11-
from . import defs
11+
from . import default
1212

1313

1414
def _register_cmap_clip(name, original_cmap, alpha):
@@ -385,7 +385,7 @@ def vectors(v, grid, cmap='blacktransparent', headlength=3, headaxislength=2.5,
385385
if ax is None:
386386
ax = plt.gca()
387387
if clim is None:
388-
v_ref = 1 / (defs.rho0 * defs.c) # reference particle velocity
388+
v_ref = 1 / (default.rho0 * default.c) # reference particle velocity
389389
clim = 0, 2 * v_ref
390390
return ax.quiver(X, Y, U, V, speed, cmap=cmap, pivot='mid', units='xy',
391391
angles='xy', headlength=headlength,

sfs/time/drivingfunction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
import numpy as np
77
from numpy.core.umath_tests import inner1d # element-wise inner product
8-
from .. import defs
8+
from .. import default
99
from .. import util
1010

1111

@@ -56,7 +56,7 @@ def wfs_25d_plane(x0, n0, n=[0, 1, 0], xref=[0, 0, 0], c=None):
5656
5757
"""
5858
if c is None:
59-
c = defs.c
59+
c = default.c
6060
x0 = util.asarray_of_rows(x0)
6161
n0 = util.asarray_of_rows(n0)
6262
n = util.normalize_vector(n)
@@ -116,7 +116,7 @@ def wfs_25d_point(x0, n0, xs, xref=[0, 0, 0], c=None):
116116
117117
"""
118118
if c is None:
119-
c = defs.c
119+
c = default.c
120120
x0 = util.asarray_of_rows(x0)
121121
n0 = util.asarray_of_rows(n0)
122122
xs = util.asarray_1d(xs)
@@ -179,7 +179,7 @@ def wfs_25d_focused(x0, n0, xs, xref=[0, 0, 0], c=None):
179179
180180
"""
181181
if c is None:
182-
c = defs.c
182+
c = default.c
183183
x0 = util.asarray_of_rows(x0)
184184
n0 = util.asarray_of_rows(n0)
185185
xs = util.asarray_1d(xs)

sfs/time/soundfield.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Compute sound field."""
22

33
from .. import util
4-
from .. import defs
4+
from .. import default
55
from .source import point
66

77

@@ -38,7 +38,7 @@ def p_array(x0, signals, weights, observation_time, grid, source=point,
3838
3939
"""
4040
if c is None:
41-
c = defs.c
41+
c = default.c
4242
x0 = util.asarray_of_rows(x0)
4343
data, samplerate, signal_offset = util.as_delayed_signal(signals)
4444
weights = util.asarray_1d(weights)

sfs/time/source.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010
from .. import util
11-
from .. import defs
11+
from .. import default
1212

1313

1414
def point(xs, signal, observation_time, grid, c=None):
@@ -51,7 +51,7 @@ def point(xs, signal, observation_time, grid, c=None):
5151
data = util.asarray_1d(data)
5252
grid = util.as_xyz_components(grid)
5353
if c is None:
54-
c = defs.c
54+
c = default.c
5555
r = np.linalg.norm(grid - xs)
5656
# evaluate g over grid
5757
weights = 1 / (4 * np.pi * r)

0 commit comments

Comments
 (0)
0