Skip to content

Commit

Permalink
Use f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
carlthome committed Aug 18, 2024
1 parent e67b63f commit 6f6bcff
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions mir_eval/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def separation(
sources = np.atleast_2d(sources)

if labels is None:
labels = ["Source {:d}".format(_) for _ in range(len(sources))]
labels = [f"Source {_:d}" for _ in range(len(sources))]

kwargs.setdefault("scaling", "spectrum")

Expand Down Expand Up @@ -947,8 +947,8 @@ def __ticker_midi_note(x, pos):
octave = int(x / 12) - 1

if cents == 0:
return "{:s}{:2d}".format(NOTES[idx], octave)
return "{:s}{:2d}{:+02d}".format(NOTES[idx], octave, int(cents * 100))
return f"{NOTES[idx]:s}{octave:2d}"
return f"{NOTES[idx]:s}{octave:2d}{int(cents * 100):+02d}"


def __ticker_midi_hz(x, pos):
Expand All @@ -957,7 +957,7 @@ def __ticker_midi_hz(x, pos):
Inputs x are interpreted as midi numbers, and converted
to Hz.
"""
return "{:g}".format(midi_to_hz(x))
return f"{midi_to_hz(x):g}"


def ticker_notes(ax=None):
Expand Down
10 changes: 5 additions & 5 deletions mir_eval/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _open(file_or_str, **kwargs):
with open(file_or_str, **kwargs) as file_desc:
yield file_desc
else:
raise IOError("Invalid file-or-str object: {}".format(file_or_str))
raise IOError(f"Invalid file-or-str object: {file_or_str}")


def load_delimited(filename, converters, delimiter=r"\s+", comment="#"):
Expand Down Expand Up @@ -77,7 +77,7 @@ def load_delimited(filename, converters, delimiter=r"\s+", comment="#"):
if comment is None:
commenter = None
else:
commenter = re.compile("^{}".format(comment))
commenter = re.compile(f"^{comment}")

# Note: we do io manually here for two reasons.
# 1. The csv module has difficulties with unicode, which may lead
Expand Down Expand Up @@ -524,7 +524,7 @@ def load_key(filename, delimiter=r"\s+", comment="#"):
raise ValueError("Key file should contain only one line.")
scale, mode = scale[0], mode[0]
# Join with a space
key_string = "{} {}".format(scale, mode)
key_string = f"{scale} {mode}"
# Validate them, but throw a warning in place of an error
try:
key.validate_key(key_string)
Expand Down Expand Up @@ -581,7 +581,7 @@ def load_tempo(filename, delimiter=r"\s+", comment="#"):
warnings.warn(error.args[0])

if not 0 <= weight <= 1:
raise ValueError("Invalid weight: {}".format(weight))
raise ValueError(f"Invalid weight: {weight}")

return tempi, weight

Expand Down Expand Up @@ -646,7 +646,7 @@ def load_ragged_time_series(
if comment is None:
commenter = None
else:
commenter = re.compile("^{}".format(comment))
commenter = re.compile(f"^{comment}")

if header:
start_row = 1
Expand Down
2 changes: 1 addition & 1 deletion mir_eval/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def validate_key(key):
)
if mode not in ["major", "minor", "other"]:
raise ValueError(
"Mode '{}' is invalid; must be 'major', 'minor' or 'other'".format(mode)
f"Mode '{mode}' is invalid; must be 'major', 'minor' or 'other'"
)


Expand Down
2 changes: 1 addition & 1 deletion mir_eval/tempo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def validate_tempi(tempi, reference=True):
raise ValueError("tempi must have exactly two values")

if not np.all(np.isfinite(tempi)) or np.any(tempi < 0):
raise ValueError("tempi={} must be non-negative numbers".format(tempi))
raise ValueError(f"tempi={tempi} must be non-negative numbers")

if reference and np.all(tempi == 0):
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion mir_eval/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def generate_labels(items, prefix="__"):
Synthetically generated labels
"""
return ["{}{}".format(prefix, n) for n in range(len(items))]
return [f"{prefix}{n}" for n in range(len(items))]


def intervals_to_samples(intervals, labels, offset=0, sample_size=0.1, fill_value=None):
Expand Down
2 changes: 1 addition & 1 deletion tests/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def load_transcription_velocity(filename):
tasks["key"] = (mir_eval.key, mir_eval.io.load_key, "data/key/{}*.txt")
# Get task keys from argv
for task in sys.argv[1:]:
print("Generating data for {}".format(task))
print(f"Generating data for {task}")
submodule, loader, data_glob = tasks[task]
ref_files = sorted(glob.glob(data_glob.format("ref")))
est_files = sorted(glob.glob(data_glob.format("est")))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def test_bipartite_match():
#
G = collections.defaultdict(list)

u_set = ["u{:d}".format(_) for _ in range(10)]
v_set = ["v{:d}".format(_) for _ in range(len(u_set) + 1)]
u_set = [f"u{_:d}" for _ in range(10)]
v_set = [f"v{_:d}" for _ in range(len(u_set) + 1)]
for i, u in enumerate(u_set):
for v in v_set[: -i - 1]:
G[v].append(u)
Expand Down

0 comments on commit 6f6bcff

Please sign in to comment.