Skip to content

Commit

Permalink
Update code to be PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
javerbukh committed Nov 6, 2024
1 parent a56b26e commit 18634ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
24 changes: 12 additions & 12 deletions jdaviz/configs/cubeviz/plugins/cube_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def suppress_stderr():
with open(os.devnull, "w") as devnull:
old_stderr = sys.stderr
sys.stderr = devnull
try:
try:
yield
finally:
sys.stderr = old_stderr
Expand All @@ -42,15 +42,15 @@ def audify_spectrum(spec, duration, overlap=0.05, system='mono', srate=44100, fm
'equal_loudness_normalisation': eln})

data = {'spectrum': [spec], 'pitch': [1]}

# again, use maximal range for the mapped parameters
lims = {'spectrum': ('0', '100')}

# set up source
sources = Events(data.keys())
sources.fromdict(data)
sources.apply_mapping_functions(map_lims=lims)

# render and play sonification!
soni = Sonification(score, sources, generator, system, samprate=srate)
soni.render()
Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self, cube, wlens, samplerate=44100, duration=1, overlap=0.05, buff
self.wl_bounds = wl_bounds
self.wl_unit = wl_unit
self.wlens = wlens

# control fades
fade = np.linspace(0, 1, buffsize+1)
self.ifade = fade[:-1]
Expand All @@ -92,16 +92,16 @@ def __init__(self, cube, wlens, samplerate=44100, duration=1, overlap=0.05, buff

# do we normalise for equal loudness?
self.eln = eln

self.idx1 = 0
self.idx2 = 0
self.cbuff = False
self.cursig = np.zeros(self.siglen, dtype='int16')
self.newsig = np.zeros(self.siglen, dtype='int16')
if self.cursig.nbytes * pow(1024,-3) > 2:

if self.cursig.nbytes * pow(1024, -3) > 2:
raise Exception("Cube projected to be > 2Gb!")

self.sigcube = np.zeros((*self.cube.shape[:2], self.siglen), dtype='int16')

def set_wl_bounds(self, w1, w2):
Expand All @@ -110,7 +110,7 @@ def set_wl_bounds(self, w1, w2):
"""
wsrt = np.sort([w1, w2])
self.wl_bounds = tuple(wsrt)

def audify_cube(self):
"""
Iterate through the cube, convert each spectrum to a signal, and store
Expand All @@ -129,7 +129,7 @@ def audify_cube(self):
with suppress_stderr():
if self.cube[i, j, lo2hi].any():
sig = audify_spectrum(self.cube[i, j, lo2hi], self.dur,
srate=self.srate,
srate=self.srate,
fmin=self.audfrqmin,
fmax=self.audfrqmax,
eln=self.eln)
Expand All @@ -146,7 +146,7 @@ def player_callback(self, outdata, frames, time, status):
cur = self.cursig
new = self.newsig
sdx = int(time.outputBufferDacTime*self.srate)
dxs = np.arange(sdx, sdx+frames).astype(int) % self.sigcube.shape[-1]
dxs = np.arange(sdx, sdx+frames).astype(int) % self.sigcube.shape[-1]
if self.cbuff:
outdata[:, 0] = (cur[dxs] * self.ofade).astype('int16')
outdata[:, 0] += (new[dxs] * self.ifade).astype('int16')
Expand Down
5 changes: 3 additions & 2 deletions jdaviz/configs/cubeviz/plugins/sonify_data/sonify_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, *args, **kwargs):
self.flux_viewer = self.app.get_viewer('flux-viewer')
self.spec_viewer.state.add_callback("x_min", self._update_x_values)
self.spec_viewer.state.add_callback("x_max", self._update_x_values)

@with_spinner()
def vue_sonify_cube(self, *args):
# Get index of selected device
Expand All @@ -79,7 +79,8 @@ def _update_x_values(self, event):
def update_viewer_range(self, event):
with delay_callback(self.spec_viewer.state, 'x_min', 'x_max'):
self.spec_viewer.state.x_min, self.spec_viewer.state.x_max = self.wavemin, self.wavemax
self.flux_viewer.update_listener_wls(self.wavemin, self.wavemax, self.spec_viewer.state.x_display_unit)
self.flux_viewer.update_listener_wls(self.wavemin, self.wavemax,
self.spec_viewer.state.x_display_unit)

@observe('volume')
def update_volume_level(self, event):
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/cubeviz/plugins/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def on_mouse_move(self, data):
# Use the selected layer from coords_info as long as it's 3D
coords_dataset = self.viewer.session.application._tools['g-coords-info'].dataset.selected
if coords_dataset == 'auto':
cube_data = self.viewer.active_image_layer.layer
cube_data = self.viewer.active_image_layer.layer
elif coords_dataset == 'none':
if len(self.viewer.layers):
cube_data = self.viewer.layers[0].layer
Expand Down
18 changes: 10 additions & 8 deletions jdaviz/configs/cubeviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

__all__ = ['CubevizImageView', 'CubevizProfileView']


@viewer_registry("cubeviz-image-viewer", label="Image 2D (Cubeviz)")
class CubevizImageView(JdavizViewerMixin, WithSliceSelection, BqplotImageView):
# categories: zoom resets, (zoom, pan), subset, select tools, shortcuts
Expand All @@ -37,7 +38,7 @@ class CubevizImageView(JdavizViewerMixin, WithSliceSelection, BqplotImageView):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# provide reference from state back to viewer to use for zoom syncing
self.state._viewer = self

Expand All @@ -53,7 +54,7 @@ def __init__(self, *args, **kwargs):
self.audification_wl_unit = None
self.volume_level = None
self.stream_active = True

@property
def _default_spectrum_viewer_reference_name(self):
return self.jdaviz_helper._default_spectrum_viewer_reference_name
Expand Down Expand Up @@ -104,17 +105,18 @@ def stop_stream(self):
self.stream.stop()

def update_cube(self, x, y):
if not self.audified_cube or not hasattr(self.audified_cube, 'newsig') or not hasattr(self.audified_cube, 'sigcube'):
if (not self.audified_cube or not hasattr(self.audified_cube, 'newsig')
or not hasattr(self.audified_cube, 'sigcube')):
return
self.audified_cube.newsig = self.audified_cube.sigcube[x, y, :]
self.audified_cube.cbuff = True

def update_listener_wl_bounds(self, w1,w2):
def update_listener_wl_bounds(self, w1, w2):
if not self.audified_cube:
return
self.audified_cube.set_wl_bounds(w1, w2)

def update_listener_wls(self, w1,w2, wunit):
def update_listener_wls(self, w1, w2, wunit):
if not self.audified_cube:
return
self.audification_wl_bounds = (w1, w2)
Expand All @@ -137,20 +139,20 @@ def update_volume_level(self, level):

def get_sonified_cube(self, sample_rate, buffer_size, device, assidx, ssvidx,
pccut, audfrqmin, audfrqmax, eln):
spectrum = self.active_image_layer.layer.get_object(statistic=None)
spectrum = self.active_image_layer.layer.get_object(statistic=None)
wlens = spectrum.wavelength.to('m').value
flux = spectrum.flux.value
self.sample_rate = sample_rate
self.buffer_size = buffer_size

if self.audification_wl_bounds:
wl_unit = getattr(u, self.audification_wl_unit)
si_wl_bounds = (self.audification_wl_bounds * wl_unit).to('m')
wdx = np.logical_and(wlens >= si_wl_bounds[0].value,
wlens <= si_wl_bounds[1].value)
wlens = wlens[wdx]
flux = flux[:, :, wdx]

pc_cube = np.percentile(np.nan_to_num(flux), np.clip(pccut, 0, 99), axis=-1)

# clip zeros and remove NaNs
Expand Down

0 comments on commit 18634ea

Please sign in to comment.