Skip to content

Commit

Permalink
[MAINT] Clean up string formatting (#205)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McCloy <dan@mccloy.info>
  • Loading branch information
tsbinns and drammock committed Jun 21, 2024
1 parent 57a1271 commit 329e80b
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 221 deletions.
80 changes: 31 additions & 49 deletions mne_connectivity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ def _init_epochs(self, events, event_id, on_missing="warn") -> None:
if events is not None:
for key, val in self.event_id.items():
if val not in events[:, 2]:
msg = "No matching events found for %s " "(event id %i)" % (
key,
val,
)
msg = f"No matching events found for {key} (event id {val})"
_on_missing(on_missing, msg)

# ensure metadata matches original events size
Expand Down Expand Up @@ -95,9 +92,8 @@ def append(self, epoch_conn):
"""
if not isinstance(self, type(epoch_conn)):
raise ValueError(
f"The type of the epoch connectivity to append "
f"is {type(epoch_conn)}, which does not match "
f"{type(self)}."
f"The type of the epoch connectivity to append is {type(epoch_conn)}, "
f"which does not match {type(self)}."
)
if hasattr(self, "times"):
if not np.allclose(self.times, epoch_conn.times):
Expand Down Expand Up @@ -157,8 +153,8 @@ def combine(self, combine="mean"):

if not self.is_epoched:
raise RuntimeError(
"Combine only works over Epoched connectivity. "
f"It does not work with {self}"
"Combine only works over Epoched connectivity. It does not work with "
f"{self}"
)

fun = _check_combine(combine, valid=("mean", "median"))
Expand Down Expand Up @@ -247,16 +243,16 @@ def predict(self, data):
"""
if data.ndim < 2 or data.ndim > 3:
raise ValueError(
f"Data passed in must be either 2D or 3D. "
f"The data you passed in has {data.ndim} dims."
"Data passed in must be either 2D or 3D. The data you passed in has "
f"{data.ndim} dims."
)
if data.ndim == 2 and self.is_epoched:
raise RuntimeError(
"If there is a VAR model over epochs, " "one must pass in a 3D array."
"If there is a VAR model over epochs, one must pass in a 3D array."
)
if data.ndim == 3 and not self.is_epoched:
raise RuntimeError(
"If there is a single VAR model, " "one must pass in a 2D array."
"If there is a single VAR model, one must pass in a 2D array."
)

# make the data 3D
Expand Down Expand Up @@ -419,9 +415,7 @@ def __init__(
):
if isinstance(indices, str) and indices not in ["all", "symmetric"]:
raise ValueError(
f"Indices can only be "
f'"all", otherwise '
f"should be a list of tuples. "
'Indices can only be "all", "symmetric", or a list of tuples. '
f"It cannot be {indices}."
)

Expand Down Expand Up @@ -450,12 +444,12 @@ def __repr__(self) -> str:
if self.n_epochs is not None:
r += f"n_epochs : {self.n_epochs}, "
if "freqs" in self.dims:
r += "freq : [%f, %f], " % (self.freqs[0], self.freqs[-1]) # type: ignore
r += f"freq : [{self.freqs[0]}, {self.freqs[-1]}], " # type: ignore
if "times" in self.dims:
r += "time : [%f, %f], " % (self.times[0], self.times[-1]) # type: ignore
r += f"time : [{self.times[0]}, {self.times[-1]}], " # type: ignore
r += f", nave : {self.n_epochs_used}"
r += f", nodes, n_estimated : {self.n_nodes}, " f"{self.n_estimated_nodes}"
r += ", ~%s" % (sizeof_fmt(self._size),)
r += f", ~{sizeof_fmt(self._size)}"
r += ">"
return r

Expand Down Expand Up @@ -534,25 +528,21 @@ def _prepare_xarray(
def _check_data_consistency(self, data, indices, n_nodes):
"""Perform data input checks."""
if not isinstance(data, np.ndarray):
raise TypeError("Connectivity data must be passed in as a " "numpy array.")
raise TypeError("Connectivity data must be passed in as a numpy array.")

if self.is_epoched:
if data.ndim < 2 or data.ndim > 4:
raise RuntimeError(
f"Data using an epoched data "
f"structure should have at least "
f"2 dimensions and at most 4 "
f"dimensions. Your data was "
f"{data.shape} shape."
"Data using an epoched data structure should have at least 2 "
f"dimensions and at most 4 dimensions. Your data was {data.shape} "
"shape."
)
else:
if data.ndim > 3:
raise RuntimeError(
f"Data not using an epoched data "
f"structure should have at least "
f"1 dimensions and at most 3 "
f"dimensions. Your data was "
f"{data.shape} shape."
"Data not using an epoched data structure should have at least 1 "
f"dimensions and at most 3 dimensions. Your data was {data.shape} "
"shape."
)

# get the number of estimated nodes
Expand All @@ -566,30 +556,24 @@ def _check_data_consistency(self, data, indices, n_nodes):
# check that the indices passed in are of the same length
if len(indices[0]) != len(indices[1]):
raise ValueError(
f"If indices are passed in "
f"then they must be the same "
f"length. They are right now "
f"{len(indices[0])} and "
f"{len(indices[1])}."
"If indices are passed in then they must be the same length. They "
f"are right now {len(indices[0])} and {len(indices[1])}."
)
# indices length should match the data length
if len(indices[0]) != data_len:
raise ValueError(
f"The number of indices, {len(indices[0])} "
f"should match the raveled data length passed "
f"in of {data_len}."
f"The number of indices, {len(indices[0])} should match the "
f"raveled data length passed in of {data_len}."
)

elif indices == "symmetric":
expected_len = ((n_nodes + 1) * n_nodes) // 2
if data_len != expected_len:
raise ValueError(
f'If "indices" is "symmetric", then '
f"connectivity data should be the "
f"upper-triangular part of the matrix. There "
f"are {data_len} estimated connections. "
f"But there should be {expected_len} "
f"estimated connections."
'If "indices" is "symmetric", then '
f"connectivity data should be the upper-triangular part of the "
f"matrix. There are {data_len} estimated connections. But there "
f"should be {expected_len} estimated connections."
)

def copy(self):
Expand Down Expand Up @@ -731,7 +715,7 @@ def get_data(self, output="compact"):
# the matrix, and there could also be cases where multiple
# results correspond to the same entries in the matrix.
raise ValueError(
"cannot return multivariate connectivity " "data in a dense form"
"cannot return multivariate connectivity data in a dense form"
)

# get the new shape of the data array
Expand Down Expand Up @@ -794,17 +778,15 @@ def rename_nodes(self, mapping):
if any(missing):
raise ValueError(
"Name(s) in mapping missing from info: "
"%s" % np.array(orig_names)[np.array(missing)]
f"{np.array(orig_names)[np.array(missing)]}"
)
new_names = [
(names.index(name), new_name) for name, new_name in mapping.items()
]
elif callable(mapping):
new_names = [(ci, mapping(name)) for ci, name in enumerate(names)]
else:
raise ValueError(
"mapping must be callable or dict, not %s" % (type(mapping),)
)
raise ValueError(f"mapping must be callable or dict, not {type(mapping)}")

# check we got all strings out of the mapping
for new_name in new_names:
Expand Down
12 changes: 6 additions & 6 deletions mne_connectivity/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ def envelope_correlation(
epoch_data = epoch_data.data
if epoch_data.ndim != 2:
raise ValueError(
"Each entry in data must be 2D, got shape %s" % (epoch_data.shape,)
f"Each entry in data must be 2D, got shape {epoch_data.shape}"
)
n_nodes, n_times = epoch_data.shape
if ei > 0 and n_nodes != corrs[0].shape[0]:
raise ValueError(
"n_nodes mismatch between data[0] and data[%d], "
"got %s and %s" % (ei, n_nodes, corrs[0].shape[0])
f"n_nodes mismatch between data[0] and data[{ei}], got {n_nodes} and "
f"{corrs[0].shape[0]}"
)

# Get the complex envelope (allowing complex inputs allows people
Expand All @@ -136,7 +136,7 @@ def envelope_correlation(

if not np.iscomplexobj(epoch_data):
raise ValueError(
"data.dtype must be float or complex, got %s" % (epoch_data.dtype,)
f"data.dtype must be float or complex, got {epoch_data.dtype}"
)
data_mag = np.abs(epoch_data)
data_conj_scaled = epoch_data.conj()
Expand Down Expand Up @@ -280,8 +280,8 @@ def _gen_sym_orth(data, n_iter, tol):
n, m = Z.shape
if m < n:
raise RuntimeError(
f"Symmetric orth requires at least as many time points ({m}) "
f"as the number of time series ({n})"
f"Symmetric orth requires at least as many time points ({m}) as the "
f"number of time series ({n})"
)
logger.debug("Symmetric orth")
# "starting with D(1) = I_n"
Expand Down
Loading

0 comments on commit 329e80b

Please sign in to comment.