Skip to content

Commit

Permalink
Merge pull request #1339 from zm711/f-strings
Browse files Browse the repository at this point in the history
Switch Neo specific Code to F strings
  • Loading branch information
samuelgarcia authored Nov 17, 2023
2 parents 5fe556a + 14a1424 commit b8989a1
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 91 deletions.
15 changes: 6 additions & 9 deletions neo/core/analogsignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,9 @@ def _repr_pretty_(self, pp, cycle):
'''
Handle pretty-printing the :class:`AnalogSignal`.
'''
pp.text("{cls} with {channels} channels of length {length}; "
"units {units}; datatype {dtype} ".format(cls=self.__class__.__name__,
channels=self.shape[1],
length=self.shape[0],
units=self.units.dimensionality.string,
dtype=self.dtype))
pp.text(f"{self.__class__.__name__} with {self.shape[1]} channels of length "
f"{self.shape[0]}; units {self.units.dimesionality.string}; datatype "
f"{self.dtype}")
if self._has_repr_pretty_attrs_():
pp.breakable()
self._repr_pretty_attrs_(pp, cycle)
Expand All @@ -431,8 +428,8 @@ def _pp(line):
with pp.group(indent=1):
pp.text(line)

_pp("sampling rate: {}".format(self.sampling_rate))
_pp("time: {} to {}".format(self.t_start, self.t_stop))
_pp(f"sampling rate: {self.sampling_rate}")
_pp(f"time: {self.t_start} to {self.t_stop}")

def time_index(self, t):
"""Return the array index (or indices) corresponding to the time (or times) `t`"""
Expand Down Expand Up @@ -716,7 +713,7 @@ def concatenate(self, *signals, overwrite=False, padding=False):
raise MergeError(f'Signals are not continuous. Can not concatenate signals with gaps. '
f'Please provide a padding value.')
if padding is not False:
logger.warning('Signals will be padded using {}.'.format(padding))
logger.warning(f'Signals will be padded using {padding}.')
if padding is True:
padding = np.nan * units
if isinstance(padding, pq.Quantity):
Expand Down
24 changes: 11 additions & 13 deletions neo/core/baseneo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ def _check_annotations(value):
"""
if isinstance(value, np.ndarray):
if not issubclass(value.dtype.type, ALLOWED_ANNOTATION_TYPES):
raise ValueError("Invalid annotation. NumPy arrays with dtype %s"
"are not allowed" % value.dtype.type)
raise ValueError(f"Invalid annotation. NumPy arrays with dtype {value.dtype.type}"
f"are not allowed")
elif isinstance(value, dict):
for element in value.values():
_check_annotations(element)
elif isinstance(value, (list, tuple)):
for element in value:
_check_annotations(element)
elif not isinstance(value, ALLOWED_ANNOTATION_TYPES):
raise ValueError("Invalid annotation. Annotations of type %s are not"
"allowed" % type(value))
raise ValueError(f"Invalid annotation. Annotations of type {type(value)} are not"
f"allowed")


def merge_annotation(a, b):
Expand All @@ -58,8 +58,7 @@ def merge_annotation(a, b):
For strings: concatenate with ';'
Otherwise: fail if the annotations are not equal
"""
assert type(a) == type(b), 'type({}) {} != type({}) {}'.format(a, type(a),
b, type(b))
assert type(a) == type(b), f'type({a})) {type(a)} != type({b}) {type(b)}'
if isinstance(a, dict):
return merge_annotations(a, b)
elif isinstance(a, np.ndarray): # concatenate b to a
Expand All @@ -72,7 +71,7 @@ def merge_annotation(a, b):
else:
return a + ";" + b
else:
assert a == b, '{} != {}'.format(a, b)
assert a == b, f'{a} != {b}'
return a


Expand Down Expand Up @@ -100,7 +99,7 @@ def merge_annotations(A, *Bs):
# exc.args += ('key %s' % name,)
# raise
merged[name] = "MERGE CONFLICT" # temporary hack
logger.debug("Merging annotations: A=%s Bs=%s merged=%s", A, Bs, merged)
logger.debug(f"Merging annotations: A={A} Bs={Bs} merged={merged}")
return merged


Expand All @@ -122,8 +121,7 @@ def intersect_annotations(A, B):

for key in set(A.keys()) & set(B.keys()):
v1, v2 = A[key], B[key]
assert type(v1) == type(v2), 'type({}) {} != type({}) {}'.format(v1, type(v1),
v2, type(v2))
assert type(v1) == type(v2), f'type({v1}) {type(v1)} != type({v2}) {type(v2)}'
if isinstance(v1, dict) and v1 == v2:
result[key] = deepcopy(v1)
elif isinstance(v1, str) and v1 == v2:
Expand Down Expand Up @@ -299,7 +297,7 @@ def _repr_pretty_attrs_(self, pp, cycle):
else:
pp.breakable()
with pp.group(indent=1):
pp.text("{}: ".format(key))
pp.text(f"{key}: ")
pp.pretty(value)

def _repr_pretty_(self, pp, cycle):
Expand Down Expand Up @@ -366,8 +364,8 @@ def set_parent(self, obj):
according to the type of "obj"
"""
if obj.__class__.__name__ not in self._parent_objects:
raise TypeError("{} can only have parents of type {}, not {}".format(
self.__class__.__name__, self._parent_objects, obj.__class__.__name__))
raise TypeError((f"{self.__class__.__name__} can only have parents of "
f"type {self._parwents_objects}, not {obj.__class__.__name__}"))
loc = self._parent_objects.index(obj.__class__.__name__)
parent_attr = self._parent_attrs[loc]
setattr(self, parent_attr, obj)
2 changes: 1 addition & 1 deletion neo/core/basesignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def merge(self, other):
if attr_self == attr_other:
kwargs[name] = attr_self
else:
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
kwargs[name] = f"merge({attr_self}, {attr_other})"
merged_annotations = merge_annotations(self.annotations, other.annotations)
kwargs.update(merged_annotations)

Expand Down
4 changes: 2 additions & 2 deletions neo/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def _repr_pretty_(self, pp, cycle):
for container in self._child_containers:
objs = getattr(self, container)
if objs:
vals.append('{} {}'.format(len(objs), container))
vals.append(f'{objs} {container}')
pp.text(', '.join(vals))

if self._has_repr_pretty_attrs_():
Expand All @@ -573,7 +573,7 @@ def _repr_pretty_(self, pp, cycle):
for container in self._repr_pretty_containers:
pp.breakable()
objs = getattr(self, container)
pp.text("# {} (N={})".format(container, len(objs)))
pp.text(f"# {container} (N={objs})")
for (i, obj) in enumerate(objs):
pp.breakable()
pp.text("%s: " % i)
Expand Down
10 changes: 5 additions & 5 deletions neo/core/dataobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _normalize_array_annotations(value, length):

if not own_length == val_length:
raise ValueError(
"Incorrect length of array annotation: {} != {}".format(val_length, own_length))
f"Incorrect length of array annotation: {val_length} != {own_length}")

# Local function used to check single elements of a list or an array
# They must not be lists or arrays and fit the usual annotation data types
Expand Down Expand Up @@ -264,10 +264,10 @@ def _merge_array_annotations(self, other):

# Warn if keys were omitted
if omitted_keys_other or omitted_keys_self:
warnings.warn("The following array annotations were omitted, because they were only "
"present in one of the merged objects: {} from the one that was merged "
"into and {} from the one that was merged into the other"
"".format(omitted_keys_self, omitted_keys_other), UserWarning)
warnings.warn(f"The following array annotations were omitted, because they were only "
f"present in one of the merged objects: {omitted_keys_self} from the "
f"one that was merged into and {omitted_keys_other} from the one that "
f"was merged into the other", UserWarning)

# Return the merged array_annotations
return merged_array_annotations
Expand Down
10 changes: 5 additions & 5 deletions neo/core/epoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def merge(self, other):
if attr_self == attr_other:
kwargs[name] = attr_self
else:
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
kwargs[name] = f"merge({attr_self}, {attr_other})"

merged_annotations = merge_annotations(self.annotations, other.annotations)
kwargs.update(merged_annotations)
Expand Down Expand Up @@ -354,8 +354,8 @@ def time_shift(self, t_shift):

def set_labels(self, labels):
if self.labels is not None and self.labels.size > 0 and len(labels) != self.size:
raise ValueError("Labels array has different length to times ({} != {})"
.format(len(labels), self.size))
raise ValueError(f"Labels array has different length to times "
f"({len(labels)} != {self.size})")
self._labels = np.array(labels)

def get_labels(self):
Expand All @@ -365,8 +365,8 @@ def get_labels(self):

def set_durations(self, durations):
if self.durations is not None and self.durations.size > 0 and len(durations) != self.size:
raise ValueError("Durations array has different length to times ({} != {})"
.format(len(durations), self.size))
raise ValueError("Durations array has different length to times "
f"({len(durations)} != {self.size})")
self._durations = durations

def get_durations(self):
Expand Down
12 changes: 6 additions & 6 deletions neo/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __new__(cls, times=None, labels=None, units=None, name=None, description=Non
# reference dimensionality
if (len(dim) != 1 or list(dim.values())[0] != 1 or not isinstance(list(dim.keys())[0],
pq.UnitTime)):
ValueError("Unit {} has dimensions {}, not [time]".format(units, dim.simplified))
ValueError(f"Unit {units} has dimensions {dim.simplified}, not [time].")

obj = pq.Quantity(times, units=dim).view(cls)
obj._labels = labels
Expand Down Expand Up @@ -204,7 +204,7 @@ def merge(self, other):
if attr_self == attr_other:
kwargs[name] = attr_self
else:
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
kwargs[name] = f"merge({attr_self}, {attr_other})"

print('Event: merge annotations')
merged_annotations = merge_annotations(self.annotations, other.annotations)
Expand Down Expand Up @@ -244,8 +244,8 @@ def __getitem__(self, i):

def set_labels(self, labels):
if self.labels is not None and self.labels.size > 0 and len(labels) != self.size:
raise ValueError("Labels array has different length to times ({} != {})"
.format(len(labels), self.size))
raise ValueError(f"Labels array has different length to times "
f"({len(labels)} != {self.size})")
self._labels = np.array(labels)

def get_labels(self):
Expand Down Expand Up @@ -353,13 +353,13 @@ def to_epoch(self, pairwise=False, durations=None):
times = self.times[::2]
durations = self.times[1::2] - times
labels = np.array(
["{}-{}".format(a, b) for a, b in zip(self.labels[::2], self.labels[1::2])])
[f"{a}-{b}" for a, b in zip(self.labels[::2], self.labels[1::2])])
elif durations is None:
# Mode 1
times = self.times[:-1]
durations = np.diff(self.times)
labels = np.array(
["{}-{}".format(a, b) for a, b in zip(self.labels[:-1], self.labels[1:])])
[f"{a}-{b}" for a, b in zip(self.labels[:-1], self.labels[1:])])
else:
# Mode 3
times = self.times
Expand Down
4 changes: 2 additions & 2 deletions neo/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def add(self, *objects):
"""Add a new Neo object to the Group"""
for obj in objects:
if self.allowed_types and not isinstance(obj, self.allowed_types):
raise TypeError("This Group can only contain {}, but not {}"
"".format(self.allowed_types, type(obj)))
raise TypeError(f"This Group can only contain {self.allowed_types}, "
f"but not {type(obj)}")
super().add(*objects)

def walk(self):
Expand Down
16 changes: 5 additions & 11 deletions neo/core/imagesequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,19 @@ def _repr_pretty_(self, pp, cycle):
Handle pretty-printing the :class:`ImageSequence`.
"""
pp.text(
"{cls} {nframe} frames with width {width} px and height {height} px; "
"units {units}; datatype {dtype} ".format(
cls=self.__class__.__name__,
nframe=self.shape[0],
height=self.shape[1],
width=self.shape[2],
units=self.units.dimensionality.string,
dtype=self.dtype,
f"{self.__class__.__name__} {self.shape[0]} frames with "
f"width {self.shape[2]} px and height {self.shape[1]} px; "
f"units {self.units.dimensionality.string}; datatype {self.dtype} "
)
)

def _pp(line):
pp.breakable()
with pp.group(indent=1):
pp.text(line)

for line in [
"sampling rate: {!s}".format(self.sampling_rate),
"spatial_scale: {!s}".format(self.spatial_scale),
f"sampling rate: {self.sampling_rate}",
f"spatial_scale: {self.spatial_scale}"
]:
_pp(line)

Expand Down
24 changes: 10 additions & 14 deletions neo/core/irregularlysampledsignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def __repr__(self):
'''
Returns a string representing the :class:`IrregularlySampledSignal`.
'''
return '<{}({} at times {})>'.format(
self.__class__.__name__, super().__repr__(), self.times)
return (f"<{self.__class__.__name__}({super().__repr__()} "
f"at times {self.times})>")

def __getitem__(self, i):
'''
Expand Down Expand Up @@ -286,16 +286,15 @@ def _check_consistency(self, other):
return
# dimensionality should match
if self.ndim != other.ndim:
raise ValueError('Dimensionality does not match: {} vs {}'.format(
self.ndim, other.ndim))
raise ValueError(f'Dimensionality does not match: {self.ndim} vs {other.ndim}')
# if if the other array does not have a times property,
# then it should be okay to add it directly
if not hasattr(other, 'times'):
return

# if there is a times property, the times need to be the same
if not (self.times == other.times).all():
raise ValueError('Times do not match: {} vs {}'.format(self.times, other.times))
raise ValueError(f'Times do not match: {self.times} vs {other.times}')

def __rsub__(self, other, *args):
'''
Expand All @@ -307,12 +306,9 @@ def _repr_pretty_(self, pp, cycle):
'''
Handle pretty-printing the :class:`IrregularlySampledSignal`.
'''
pp.text("{cls} with {channels} channels of length {length}; "
"units {units}; datatype {dtype} ".format(cls=self.__class__.__name__,
channels=self.shape[1],
length=self.shape[0],
units=self.units.dimensionality.string,
dtype=self.dtype))
pp.text(f"{self.__class__.__name__} with {self.shape[1]} channels of length "
f"{self.shape[0]}; units {self.units.dimensionality.string}; datatype "
f"{self.dtype}")
if self._has_repr_pretty_attrs_():
pp.breakable()
self._repr_pretty_attrs_(pp, cycle)
Expand All @@ -322,7 +318,7 @@ def _pp(line):
with pp.group(indent=1):
pp.text(line)

for line in ["sample times: {}".format(self.times)]:
for line in [f"sample times: {self.times}"]:
_pp(line)

@property
Expand Down Expand Up @@ -485,7 +481,7 @@ def merge(self, other):
if attr_self == attr_other:
kwargs[name] = attr_self
else:
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
kwargs[name] = f"merge({attr_self}, {attr_other})"
merged_annotations = merge_annotations(self.annotations, other.annotations)
kwargs.update(merged_annotations)

Expand Down Expand Up @@ -564,7 +560,7 @@ def concatenate(self, other, allow_overlap=False):
if attr_self == attr_other:
kwargs[name] = attr_self
else:
kwargs[name] = "merge({}, {})".format(attr_self, attr_other)
kwargs[name] = f"merge({attr_self}, {attr_other})"
merged_annotations = merge_annotations(self.annotations, other.annotations)
kwargs.update(merged_annotations)

Expand Down
Loading

0 comments on commit b8989a1

Please sign in to comment.