Skip to content

Commit

Permalink
fixed a wrong if statement and added some notes
Browse files Browse the repository at this point in the history
  • Loading branch information
beasteers authored Feb 6, 2020
1 parent 4f8953e commit a1ecf85
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions birdvoxpaint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,16 @@ def apply_indices(S, indices, segment_length, n_jobs=1):
S_blocks = (spec_frame(spec, segment_length) for spec in S) # yields: block, freq, time

# allow user to pass a custom indices function - or as a list of functions
# NOTE: we use `binder` here so that index functions can have a state dict
# if they want to, but omit it if they don't
if callable(indices):
calc_indices = binder(indices, state={})
else:
indices = [binder(f, state={}) for f in indices]
calc_indices = lambda x: np.stack([f(x) for f in indices], axis=-1)

# build job to calculate indices
if n_jobs == 1:
if n_jobs > 1:
jobs = joblib.Parallel(n_jobs=n_jobs)
calc_indices = joblib.delayed(calc_indices)
else:
Expand All @@ -202,6 +204,7 @@ def _shape(X):
print(x.shape)
yield x

# TODO: cache this instead of pre-calling it in `transform`
def freq_slice(fmin, fmax, sr, n_fft):
'''Calculate the slice needed to select a frequency band.
Expand Down Expand Up @@ -286,6 +289,17 @@ def example_transform(funcs=[my_pcen_index, my_max_index], ...):
if i_want_to_reset_the_state:
for f in funcs:
f.cfg_['state'] = {}
NOTE: As I've matured, maybe a simpler solution would be to do:
def my_pcen_index():
state = {}
def calc(S):
state['z'] = ...
calc.state = state
return calc
transform(..., indices=[my_max_index, my_pcen_index()])
'''

def outer(func):
Expand All @@ -298,15 +312,11 @@ def outer(func):
cfg.update({k: __cfg__[k] for k in avail_args & set(__cfg__)})
if kw:
cfg.update({k: kw[k] for k in avail_args & set(kw)})

func.cfg_ = cfg
# TODO: allow mutations from the original bound dictionary `config` to
# propagate to function. Which would also allow functions
# to share a state.


@wraps(func)
def inner(*a, **kw):
return func(*a, **dict(func.cfg_, **kw))
return func(*a, **dict(cfg, **kw))
inner.cfg_ = cfg
return inner

return outer(__func__) if __func__ is not None else outer

0 comments on commit a1ecf85

Please sign in to comment.