| 1 |
""" |
| 2 |
This is only a bridge for using Brian 1 hears with Brian 2. |
| 3 |
|
| 4 |
.. deprecated:: 2.2.2.2 |
| 5 |
Use the `brian2hears <https://brian2hears.readthedocs.io/>`_ package instead. |
| 6 |
|
| 7 |
NOTES: |
| 8 |
|
| 9 |
* Slicing sounds with Brian 2 units doesn't work, you need to either use Brian 1 units or replace calls to |
| 10 |
``sound[:20*ms]`` with ``sound.slice(None, 20*ms)``, etc. |
| 11 |
|
| 12 |
TODO: handle properties (e.g. sound.duration) |
| 13 |
|
| 14 |
Not working examples: |
| 15 |
|
| 16 |
* time_varying_filter1 (care with units) |
| 17 |
""" |
| 18 |
|
| 19 |
try: |
| 20 |
import brian as b1 |
| 21 |
import brian.hears as b1h |
| 22 |
except ImportError as ex: |
| 23 |
raise ImportError( |
| 24 |
"brian2.hears is deprecated and will be removed in a future release, please use" |
| 25 |
" the brian2hears package available at https://brian2hears.readthedocs.io/. If" |
| 26 |
" you really want to keep using it, note: brian2.hears is a bridge between" |
| 27 |
" Brian 2 and the version of Brian Hears from Brian 1, you need to have Brian 1" |
| 28 |
" installed to use it." |
| 29 |
) from ex |
| 30 |
|
| 31 |
from inspect import isclass, ismethod |
| 32 |
|
| 33 |
from numpy import asarray, ndarray |
| 34 |
|
| 35 |
from brian2.core.clocks import Clock |
| 36 |
from brian2.core.operations import network_operation |
| 37 |
from brian2.groups.neurongroup import NeuronGroup |
| 38 |
from brian2.units import second |
| 39 |
from brian2.units.fundamentalunits import Quantity |
| 40 |
from brian2.utils.logger import get_logger |
| 41 |
|
| 42 |
logger = get_logger(__name__) |
| 43 |
|
| 44 |
logger.warn( |
| 45 |
"brian2.hears is deprecated and will be removed in a future release, please use the" |
| 46 |
" brian2hears package available at https://brian2hears.readthedocs.io/. If you" |
| 47 |
" really want to keep using it, note that it is a bridge between Brian 2 and Brian" |
| 48 |
" Hears from Brian 1. This is not guaranteed to work in all cases that brian.hears" |
| 49 |
" works. See the limitations in the online documentation." |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
def convert_unit_b1_to_b2(val): |
| 54 |
return Quantity.with_dimensions(float(val), val.dim._dims) |
| 55 |
|
| 56 |
|
| 57 |
def convert_unit_b2_to_b1(val): |
| 58 |
return b1.Quantity.with_dimensions(float(val), val.dim._dims) |
| 59 |
|
| 60 |
|
| 61 |
def modify_arg(arg): |
| 62 |
""" |
| 63 |
Modify arguments to make them compatible with Brian 1. |
| 64 |
|
| 65 |
- Arrays of units are replaced with straight arrays |
| 66 |
- Single values are replaced with Brian 1 equivalents |
| 67 |
- Slices are handled so we can use e.g. sound[:20*ms] |
| 68 |
|
| 69 |
The second part was necessary because some functions/classes test if an object is an array or not to see if it |
| 70 |
is a sequence, but because brian2.Quantity derives from ndarray this was causing problems. |
| 71 |
""" |
| 72 |
if isinstance(arg, Quantity): |
| 73 |
if len(arg.shape) == 0: |
| 74 |
arg = b1.Quantity.with_dimensions(float(arg), arg.dim._dims) |
| 75 |
else: |
| 76 |
arg = asarray(arg) |
| 77 |
elif isinstance(arg, slice): |
| 78 |
arg = slice(modify_arg(arg.start), modify_arg(arg.stop), modify_arg(arg.step)) |
| 79 |
return arg |
| 80 |
|
| 81 |
|
| 82 |
def wrap_units(f): |
| 83 |
""" |
| 84 |
Wrap a function to convert units into a form that Brian 1 can handle. Also, check the output argument, if it is |
| 85 |
a ``b1h.Sound`` wrap it. |
| 86 |
""" |
| 87 |
|
| 88 |
def new_f(*args, **kwds): |
| 89 |
newargs = [] |
| 90 |
newkwds = {} |
| 91 |
for arg in args: |
| 92 |
newargs.append(modify_arg(arg)) |
| 93 |
for k, v in kwds.items(): |
| 94 |
newkwds[k] = modify_arg(v) |
| 95 |
rv = f(*newargs, **newkwds) |
| 96 |
if rv.__class__ == b1h.Sound: |
| 97 |
rv.__class__ = BridgeSound |
| 98 |
elif isinstance(rv, b1.Quantity): |
| 99 |
rv = Quantity.with_dimensions(float(rv), rv.dim._dims) |
| 100 |
return rv |
| 101 |
|
| 102 |
return new_f |
| 103 |
|
| 104 |
|
| 105 |
def wrap_units_property(p): |
| 106 |
fget = p.fget |
| 107 |
fset = p.fset |
| 108 |
fdel = p.fdel |
| 109 |
if fget is not None: |
| 110 |
fget = wrap_units(fget) |
| 111 |
if fset is not None: |
| 112 |
fset = wrap_units(fset) |
| 113 |
if fdel is not None: |
| 114 |
fdel = wrap_units(fdel) |
| 115 |
new_p = property(fget, fset, fdel) |
| 116 |
return new_p |
| 117 |
|
| 118 |
|
| 119 |
def wrap_units_class(_C): |
| 120 |
""" |
| 121 |
Wrap a class to convert units into a form that Brian 1 can handle in all methods |
| 122 |
""" |
| 123 |
|
| 124 |
class new_class(_C): |
| 125 |
for _k in _C.__dict__: |
| 126 |
_v = getattr(_C, _k) |
| 127 |
if hasattr(ndarray, _k) and getattr(ndarray, _k) is _v: |
| 128 |
continue |
| 129 |
if ismethod(_v): |
| 130 |
_v = wrap_units(_v) |
| 131 |
exec(f"{_k} = _v") |
| 132 |
elif isinstance(_v, property): |
| 133 |
_v = wrap_units_property(_v) |
| 134 |
exec(f"{_k} = _v") |
| 135 |
del _k |
| 136 |
del _v |
| 137 |
|
| 138 |
return new_class |
| 139 |
|
| 140 |
|
| 141 |
WrappedSound = wrap_units_class(b1h.Sound) |
| 142 |
|
| 143 |
|
| 144 |
class BridgeSound(WrappedSound): |
| 145 |
""" |
| 146 |
We add a new method slice because slicing with units can't work with Brian 2 units. |
| 147 |
""" |
| 148 |
|
| 149 |
def slice(self, *args): |
| 150 |
return self.__getitem__(slice(*args)) |
| 151 |
|
| 152 |
|
| 153 |
Sound = BridgeSound |
| 154 |
|
| 155 |
|
| 156 |
class FilterbankGroup(NeuronGroup): |
| 157 |
def __init__(self, filterbank, targetvar, *args, **kwds): |
| 158 |
self.targetvar = targetvar |
| 159 |
self.filterbank = filterbank |
| 160 |
self.buffer = None |
| 161 |
filterbank.buffer_init() |
| 162 |
|
| 163 |
# Sanitize the clock - does it have the right dt value? |
| 164 |
if "clock" in kwds: |
| 165 |
if int(1 / kwds["clock"].dt) != int(filterbank.samplerate): |
| 166 |
raise ValueError("Clock should have 1/dt=samplerate") |
| 167 |
kwds["clock"] = Clock(dt=float(kwds["clock"].dt) * second) |
| 168 |
else: |
| 169 |
kwds["clock"] = Clock(dt=1 * second / float(filterbank.samplerate)) |
| 170 |
|
| 171 |
buffersize = kwds.pop("buffersize", 32) |
| 172 |
if not isinstance(buffersize, int): |
| 173 |
buffersize = int(buffersize * self.samplerate) |
| 174 |
self.buffersize = buffersize |
| 175 |
self.buffer_pointer = buffersize |
| 176 |
self.buffer_start = -buffersize |
| 177 |
|
| 178 |
NeuronGroup.__init__(self, filterbank.nchannels, *args, **kwds) |
| 179 |
|
| 180 |
@network_operation(clock=self.clock, when="start") |
| 181 |
def apply_filterbank_output(): |
| 182 |
if self.buffer_pointer >= self.buffersize: |
| 183 |
self.buffer_pointer = 0 |
| 184 |
self.buffer_start += self.buffersize |
| 185 |
self.buffer = self.filterbank.buffer_fetch( |
| 186 |
self.buffer_start, self.buffer_start + self.buffersize |
| 187 |
) |
| 188 |
setattr(self, targetvar, self.buffer[self.buffer_pointer, :]) |
| 189 |
self.buffer_pointer += 1 |
| 190 |
|
| 191 |
self.contained_objects.append(apply_filterbank_output) |
| 192 |
|
| 193 |
def reinit(self): |
| 194 |
NeuronGroup.reinit(self) |
| 195 |
self.filterbank.buffer_init() |
| 196 |
self.buffer_pointer = self.buffersize |
| 197 |
self.buffer_start = -self.buffersize |
| 198 |
|
| 199 |
|
| 200 |
handled_explicitly = {"Sound", "FilterbankGroup"} |
| 201 |
|
| 202 |
__all__ = [k for k in b1h.__dict__ if not k.startswith("_")] |
| 203 |
for k in __all__: |
| 204 |
if k in handled_explicitly: |
| 205 |
continue |
| 206 |
curobj = getattr(b1h, k) |
| 207 |
if callable(curobj): |
| 208 |
if isclass(curobj): |
| 209 |
curobj = wrap_units_class(curobj) |
| 210 |
else: |
| 211 |
curobj = wrap_units(curobj) |
| 212 |
exec(f"{k} = curobj") |
| 213 |
|
| 214 |
__all__.extend( |
| 215 |
[ |
| 216 |
"convert_unit_b1_to_b2", |
| 217 |
"convert_unit_b2_to_b1", |
| 218 |
] |
| 219 |
) |