Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/laserjeyes/ms3
Browse files Browse the repository at this point in the history
  • Loading branch information
laserjeyes committed Nov 27, 2020
2 parents d4a4fa2 + 9bfd92d commit 0242186
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/ms3/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'C': logging.CRITICAL,
}

CURRENT_LEVEL = logging.WARNING
CURRENT_LEVEL = logging.INFO

class ContextAdapter(logging.LoggerAdapter):
""" This LoggerAdapter is designed to include the module and function that called the logger."""
Expand Down
8 changes: 4 additions & 4 deletions src/ms3/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,9 @@ def compare_labels(self, detached_key, new_color='ms3_darkgreen', old_color='ms3
ids = [id for id in ids if detached_key in self._parsed_mscx[id]._detached_annotations]
self.logger.info(f"{len(ids)} parsed scores include detached labels with the key '{detached_key}'.")
for id in ids:
self._parsed_mscx[id].compare_labels(detached_key=detached_key, new_color=new_color, old_color=old_color,
res = self._parsed_mscx[id].compare_labels(detached_key=detached_key, new_color=new_color, old_color=old_color,
detached_is_newer=detached_is_newer)
if store_with_suffix is not None:
if res and store_with_suffix is not None:
self.store_mscx(ids=ids, suffix=store_with_suffix, overwrite=True, simulate=self.simulate)


Expand Down Expand Up @@ -1363,8 +1363,8 @@ def store_mscx(self, keys=None, ids=None, root_dir=None, folder='.', suffix='',
for key, i in ids:
new_path = self._store_mscx(key=key, i=i, folder=folder, suffix=suffix, root_dir=root_dir, overwrite=overwrite, simulate=simulate)
if new_path in paths:
modus = 'would ' if simulate else ''
self.logger.warning(f"The score at {new_path} {modus}have been overwritten.")
modus = 'would have' if simulate else 'has'
self.logger.warning(f"The score at {new_path} {modus} been overwritten.")
else:
paths.append(new_path)
if simulate:
Expand Down
32 changes: 18 additions & 14 deletions src/ms3/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,32 @@ def attach_labels(self, key, staff=None, voice=None, check_for_clashes=True, rem
:obj:`int`
Number of labels that were to be attached.
"""
assert self._mscx is not None, "No score has been parsed yet."
assert key != 'annotations', "Labels with key 'annotations' are already attached."
if key not in self._detached_annotations:
self.logger.info(f"""Key '{key}' doesn't correspond to a detached set of annotations.
self.mscx.logger.info(f"""Key '{key}' doesn't correspond to a detached set of annotations.
Use one of the existing keys or load a new set with the method load_annotations().\nExisting keys: {list(self._detached_annotations.keys())}""")
return 0, 0

annotations = self._detached_annotations[key]
goal = len(annotations.df)
if goal == 0:
self.logger.warning(f"The Annotation object '{key}' does not contain any labels.")
self.mscx.logger.warning(f"The Annotation object '{key}' does not contain any labels.")
return 0, 0
df = annotations.prepare_for_attaching(staff=staff, voice=voice, check_for_clashes=check_for_clashes)
reached = len(df)
if reached == 0:
self.logger.error(f"No labels from '{key}' have been attached due to aforementioned errors.")
self.mscx.logger.error(f"No labels from '{key}' have been attached due to aforementioned errors.")
return reached, goal

prepared_annotations = Annotations(df=df, cols=annotations.cols, infer_types=annotations.regex_dict)
reached = self.mscx.add_labels(prepared_annotations)
if remove_detached:
if reached == goal:
del(self._detached_annotations[key])
self.logger.debug(f"Detached annotations '{key}' successfully attached and removed.")
self.mscx.logger.debug(f"Detached annotations '{key}' successfully attached and removed.")
else:
self.logger.info(f"Only {reached} of the {goal} targeted labels could be attached, so '{key}' was not removed.")
self.mscx.logger.info(f"Only {reached} of the {goal} targeted labels could be attached, so '{key}' was not removed.")
return reached, goal


Expand All @@ -317,7 +318,7 @@ def check_labels(self, keys='annotations', regex=None, label_type='dcml', **kwar
Labels not matching the regex.
"""
if keys == 'annotations' and not self.mscx.has_annotations:
self.logger.debug("Score contains no Annotations.")
self.mscx.logger.debug("Score contains no Annotations.")
return
if regex is None:
if label_type in self._label_regex:
Expand All @@ -336,7 +337,7 @@ def check_labels(self, keys='annotations', regex=None, label_type='dcml', **kwar
for k in keys:
(existing if k in self else missing).append(k)
if len(missing) > 0:
self.logger.warning(f"The following keys are not among the Annotations objects, which are: {list(self)}")
self.logger.warning(f"The keys {missing} are not among the Annotations objects, which are: {list(self)}")
if len(existing) == 0:
return pd.DataFrame()
labels_cfg = self.labels_cfg.copy()
Expand Down Expand Up @@ -388,8 +389,8 @@ def compare_labels(self, detached_key, new_color='ms3_darkgreen', old_color='ms3
changes_old = old_vals - unchanged
changes_new = new_vals - unchanged
if len(changes_new) == 0 and len(changes_old) == 0:
self.logger.info(f"Comparison yielded no changes.")
return
self.mscx.logger.info(f"Comparison yielded no changes.")
return False

new_rgba = color2rgba(new_color)
new_color_params = rgba2params(new_rgba)
Expand All @@ -416,7 +417,10 @@ def compare_labels(self, detached_key, new_color='ms3_darkgreen', old_color='ms3
self.mscx.changed = True
self.mscx.parsed.parse_measures()
self.mscx._update_annotations()
self.logger.info(f"{color_changes} attached labels changed to {change_to}, {added_changes} labels added in {added_color}.")
self.mscx.logger.info(f"{color_changes} attached labels changed to {change_to}, {added_changes} labels added in {added_color}.")
return True
return False



def detach_labels(self, key, staff=None, voice=None, label_type=None, delete=True):
Expand Down Expand Up @@ -446,18 +450,18 @@ def detach_labels(self, key, staff=None, voice=None, label_type=None, delete=Tru
of the annotations for storing them separately but without removing the labels from the score.
"""
if not self.mscx.has_annotations:
self.logger.info("No annotations present in score.")
self.mscx.logger.info("No annotations present in score.")
return
assert key != 'annotations', "The key 'annotations' is reserved, please choose a different one."
if not key.isidentifier():
self.logger.warning(
f"'{key}' can not be used as an identifier. The extracted labels need to be accessed via self._detached_annotations['{key}']")
f"'{key}' cannot be used as an identifier. The extracted labels need to be accessed via self._detached_annotations['{key}']")
df = self.annotations.get_labels(staff=staff, voice=voice, label_type=label_type, drop=delete)
if len(df) == 0:
self.logger.info(f"No labels found for staff {staff}, voice {voice}, label_type {label_type}.")
self.mscx.logger.info(f"No labels found for staff {staff}, voice {voice}, label_type {label_type}.")
return
logger_cfg = self.logger_cfg.copy()
logger_cfg['name'] += f"{self.logger_names['mscx']}:{key}"
logger_cfg['name'] += f"{self.mscx.logger.logger.name}:{key}"
self._detached_annotations[key] = Annotations(df=df, infer_types=self.get_infer_regex(), mscx_obj=self._mscx,
logger_cfg=logger_cfg)
if delete:
Expand Down
39 changes: 24 additions & 15 deletions tests/test_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ def test_init(self):

class TestParser:

test_folder, _ = os.path.split(os.path.realpath(__file__))
test_folder = os.path.basename(os.path.realpath(__file__))

def test_parse_and_write_back(self, score_object):
original_mscx = score_object.full_paths['mscx']
tmp_file = tempfile.NamedTemporaryFile(mode='r', suffix='.mscx', dir=self.test_folder)
if score_object.mscx.has_annotations:
score_object.detach_labels('labels')
score_object.attach_labels('labels')
score_object.store_mscx(tmp_file.name)
original = open(original_mscx).read()
after_parsing = tmp_file.read()
assert_all_lines_equal(original, after_parsing, original=original_mscx, tmp_file=tmp_file)
try:
tmp_file = tempfile.NamedTemporaryFile(mode='r', suffix='.mscx', dir=self.test_folder, encoding='utf-8', delete=False)
if score_object.mscx.has_annotations:
score_object.detach_labels('labels')
score_object.attach_labels('labels')
score_object.store_mscx(tmp_file.name)
original = open(original_mscx, encoding='utf-8').read()
after_parsing = tmp_file.read()
assert_all_lines_equal(original, after_parsing, original=original_mscx, tmp_file=tmp_file)
finally:
tmp_file.close()
os.remove(tmp_file.name)



def test_store_and_load_labels(self, score_object):
Expand All @@ -49,12 +54,16 @@ def test_store_and_load_labels(self, score_object):
score_object.load_annotations(fname, key='tsv')
score_object.detach_labels('labels')
score_object.attach_labels('tsv')
tmp_file = tempfile.NamedTemporaryFile(mode='r', suffix='.tsv', dir=self.test_folder)
score_object.store_mscx(tmp_file.name)
original_mscx = score_object.full_paths['mscx']
before = open(original_mscx).read()
after = tmp_file.read()
assert_all_lines_equal(before, after, original=original_mscx, tmp_file=tmp_file)
try:
tmp_file = tempfile.NamedTemporaryFile(mode='r', suffix='.tsv', dir=self.test_folder, encoding='utf-8', delete=False)
score_object.store_mscx(tmp_file.name)
original_mscx = score_object.full_paths['mscx']
before = open(original_mscx, encoding='utf-8').read()
after = tmp_file.read()
assert_all_lines_equal(before, after, original=original_mscx, tmp_file=tmp_file)
finally:
tmp_file.close()
os.remove(tmp_file.name)

def test_expanded_labels(self, score_object):
if score_object.mscx.has_annotations:
Expand Down

0 comments on commit 0242186

Please sign in to comment.