Exemplo n.º 1
0
def randomize_time(tree):
    measure = tt.first_or_none(tree, "measure")
    n = tt.first_or_none(tree, "staff").getAttribute("n")
    scoreDef = tt.first_or_none(tree, "scoreDef")
    if not scoreDef:
        scoreDef = tt.create_element_node("scoreDef")
        tree.insertBefore(scoreDef, measure)

    staffGrp = tt.first_or_none(scoreDef, "staffGrp")
    if not staffGrp:
        staffGrp = tt.create_element_node("staffGrp")
        scoreDef.appendChild(staffGrp)

    staffDef = tt.first_or_none(staffGrp, "staffDef",
                                lambda e: e.getAttribute("n") == n)
    if not staffDef:
        staffDef = tt.create_element_node("staffDef", {"n": n})
        staffGrp.appendChild(staffDef)

    choice = rnd.choice([{
        "meter.sym": "true"
    }, {
        "meter.count": f"{1 + rnd.randint(4)}",
        "meter.unit": f"{1 + rnd.randint(4)}"
    }, {}])

    set_attributes(staffDef, choice)

    return tree.toxml()
Exemplo n.º 2
0
def randomize_key(tree):
    measure = tt.first_or_none(tree, "measure")
    n = tt.first_or_none(tree, "staff").getAttribute("n")
    scoreDef = tt.first_or_none(tree, "scoreDef")
    if not scoreDef:
        scoreDef = tt.create_element_node("scoreDef")
        tree.insertBefore(scoreDef, measure)

    staffGrp = tt.first_or_none(scoreDef, "staffGrp")
    if not staffGrp:
        staffGrp = tt.create_element_node("staffGrp")
        scoreDef.appendChild(staffGrp)

    staffDef = tt.first_or_none(staffGrp, "staffDef",
                                lambda e: e.getAttribute("n") == n)
    if not staffDef:
        staffDef = tt.create_element_node("staffDef", {"n": n})
        staffGrp.appendChild(staffDef)

    choice = rnd.choice([{
        "key.sig.show": "true",
        "key.sig": f"{rnd.randint(5)}f"
    }, {
        "key.sig.show": "true",
        "key.sig": f"{rnd.randint(5)}s"
    }, {
        "key.sig.show": "true",
        "key.sig": f"{rnd.randint(5)}0"
    }, {}])

    set_attributes(staffDef, choice)

    return tree.toxml()
Exemplo n.º 3
0
 def test_nodes_different_attributes(self):
     node1 = tt.create_element_node("test", {
         "val1": 1,
         "val2": 2,
         "val3": 3
     })
     node2 = tt.create_element_node("test", {"val1": 1, "val2": 2})
     self.check(4, ta.node_distance(node1, node2))
Exemplo n.º 4
0
 def test_nodes_overlapping_and_value_mismatch(self):
     node1 = tt.create_element_node("test", {"val1": 1, "val2": 2})
     node2 = tt.create_element_node("test1", {
         "val1": 1,
         "val2": 4,
         "val3": 5,
         "val4": 22
     })
     self.check(20 + 2 * 4 + 1 * 2, ta.node_distance(node1, node2))
Exemplo n.º 5
0
def populate_payload_xml(score, tree, task):
    task_type_name = task["type"]
    task_step_name = task["step"]
    measures = tree.getElementsByTagName("measure")
    first_measure_index = measures[0].getAttribute("n")

    # Scoredefs
    if task_type_name == "0_fix_scoredef" and scoredef_chance > rnd.random():
        node = tt.create_element_node(
            "scoreDef", {
                "key.sig": rnd.choice(key_sigs),
                "key.mode": rnd.choice(key_modes),
                "crmp_id": str(uuid.uuid4())
            })
        print(
            f"  Putting scoreDef {node.toxml()} before measure n={first_measure_index}"
        )
        tree.insertBefore(node, measures[0]).parentNode

    # Clef detection
    elif task_type_name == "1_detect_clefs" and task_step_name == "edit" and clef_chance > rnd.random(
    ):
        node = tt.create_element_node("clef", {"shape": "G", "line": "2"})
        staff = tree.getElementsByTagName("staff")[0]
        first_staff_index = staff.getAttribute('n')
        print(
            f"  Putting clef {node.toxml()} in measure n={first_measure_index}, staff n={first_staff_index}"
        )

        for layer in staff.getElementsByTagName("layer"):
            layer.appendChild(node.cloneNode(deep=True))
            break

    # Note editing
    elif task_type_name == "2_add_notes" and task_step_name == "edit":
        staff = tree.getElementsByTagName("staff")[0]
        staff_index = staff.getAttribute('n')
        for layer in staff.getElementsByTagName("layer"):
            for i in [_ for _ in range(4) if note_chance > rnd.random()]:
                note_attr = {
                    "dur": rnd.choice(note_dur),
                    "oct": rnd.choice(note_oct),
                    "pname": rnd.choice(note_pname)
                }
                node = tt.create_element_node("note", note_attr)
                print(
                    f"  Putting note {node.toxml()} in measure n={first_measure_index}, staff n={staff_index}"
                )
                layer.appendChild(node.cloneNode(deep=True))

    return tt.purge_non_element_nodes(tree).toxml()
def callback(channel, method, properties, body):

    message = json.loads(body)
    task_id = message["task_id"]
    task = db[cfg.col_task].find_one({"_id": ObjectId(task_id)})
    task_type_name = task["type"]
    task_type = db[cfg.col_task_type].find_one({"name": task_type_name})
    task_step_name = task["step"]
    threshold = task_type["steps"][task_step_name]["min_responses"]
    result_type = task_type["steps"][task_step_name]["result_type"]

    tree = xml.parseString(task['xml']).documentElement

    print(
        f"Processing task {task_id} of type {task_type} at step {task_step_name}; providing result type {result_type}"
    )

    clefs = [
        tt.create_element_node("clef", {
            "shape": "G",
            "line": "4"
        }),
        tt.create_element_node("clef", {
            "shape": "F",
            "line": "3"
        }),
        tt.create_element_node("clef", {
            "shape": "C",
            "line": "2"
        })
    ]

    for i in range(threshold):
        treeClone = tree.cloneNode(deep=True)
        node = clefs[i]
        layer = treeClone.getElementsByTagName("layer")[0]
        layer.appendChild(node.cloneNode(deep=True))
        payload = treeClone.toxml()

        requests.post(f"http://localhost:443/{task['_id']}", data=payload)
        for j in range(
                0):  # Putting this to >0 will simulate delay in response
            connection.process_data_events()
            time.sleep(0.2)

    print(
        f"  Passed through task with ID {task['_id']} of type:step {task['type']}:{task['step']} as result {threshold} times"
    )
    print(f"  - payload: {payload}")
    raise Exception("I've served my purpose...")
Exemplo n.º 7
0
def get_slice_context_and_xml(measure_slice):
    score_name = measure_slice["score"]
    start = measure_slice['start']
    end = measure_slice['end']
    staff_range = [str(x) for x in range(
        measure_slice['staff_start'] + 1,
        measure_slice['staff_end'] + 1
    )]

    score = db[cfg.col_score].find_one({"name": score_name})

    section = tt.create_element_node("section")

    for measure in score["measures"][start:end]:
        score_def = measure["score_def_before_measure"]
        if score_def:
            section.appendChild(xml.parseString(score_def).documentElement)
        section.appendChild(xml.parseString(measure["xml"]).documentElement)

    context = xml.parseString(measure["context"]).documentElement

    for node in list(section.childNodes) + context.getElementsByTagName('scoreDef'):
        staffs = list(node.getElementsByTagName("staff")) + list(node.getElementsByTagName("staffDef"))
        for staff in staffs:
            if staff.getAttribute("n") not in staff_range:
                tt.delete_node(staff)

    return section.toxml(), context.toxml()
Exemplo n.º 8
0
 def build_initial_context(self):
     context = self.mei.documentElement.cloneNode(deep=True)
     tt.delete_node(context.getElementsByTagName("facsimile")[0])
     section = context.getElementsByTagName("section")[0]
     tt.replace_child_nodes(section, [])
     section.appendChild(tt.create_element_node("PUT_TASK_XML_HERE"))
     return context
Exemplo n.º 9
0
    def update_score_def_with_score_def(self, score_def, new_score_def):
        attributes = ["key.mode", "key.sig", "meter.sym", "meter.unit", "meter.count", "clef.line", "clef.shape"]
        staff_defs = score_def.getElementsByTagName("staffDef")
        new_staff_defs = new_score_def.getElementsByTagName("staffDef")

        for attr in attributes:
            # Sometimes, the score def itself contains some properties, we should try to set those as well
            value = new_score_def.getAttribute(attr)
            if value:
                score_def.setAttribute(attr, value)

            for new_staff_def in new_staff_defs:
                value = new_staff_def.getAttribute(attr)
                n = new_staff_def.getAttribute("n")
                if value:
                    found = False
                    for old_staff_def in staff_defs:
                        if old_staff_def.getAttribute("n") == n:
                            old_staff_def.setAttribute(attr, value)
                            found = True
                            break
                    if not found:
                        score_def.appendChild(tt.create_element_node("staffDef", {"n": n, attr: value}))

            for old_staff_def, new_staff_def in tt.matching_pairs(staff_defs, new_staff_defs, ["n"]):
                value = new_staff_def.getAttribute(attr)
                if value:
                    old_staff_def.setAttribute(attr, value)
Exemplo n.º 10
0
def callback(ch, method, properties, body):
    data = json.loads(body)
    # We need:
    # - name: name of the current sheet
    # - partials: file names of the additional MEI files (including extension) (list).
    # These "partial" MEIs need to be in the "whole" folder of the sheet already, just like the skeleton.mei
    sheet_name = data['name']
    partial_file_names = data['partials']

    # Get sheet id (for status queue)
    sheet_id = str(db[cfg.col_sheet].find_one({"name": sheet_name})["_id"])

    whole_dir = fsm.get_sheet_whole_directory(sheet_name)
    skeleton_path = whole_dir / 'aligned.mei'
    partial_paths = [whole_dir / partial for partial in partial_file_names]

    # skeleton always has 1 section which just contains the measures and some additional tags
    skeleton_document = xml.parse(str(skeleton_path)).documentElement
    skeleton_section = skeleton_document.getElementsByTagName("section")[0]
    skeleton_section_xml = tt.purge_non_element_nodes(skeleton_section).toxml()
    partial_sections_xml = []
    for partial_path in partial_paths:
        if partial_path.is_file():
            partial = xml.parse(str(partial_path))
            # We have to extract the measures and put them under a "fake" section root to get a similar structure as the skeleton
            partial = tt.replace_child_nodes(
                tt.create_element_node("section"),
                partial.getElementsByTagName("measure"))
            partial = tt.purge_non_element_nodes(partial)
            partial_sections_xml.append(partial.toxml())

    # Perform the alignments and node picking
    aligned_trees = ta.align_trees_multiple([skeleton_section_xml] +
                                            partial_sections_xml)
    final_section_tree, _ = ta.build_consensus_tree(
        aligned_trees, consensus_method=ta.consensus_bnd_enrich_skeleton)

    # The final tree only aligned the section with measures, so we need to put the contents of that section back now
    tt.replace_child_nodes(skeleton_section, final_section_tree.childNodes)

    # Write the final tree to a file
    with open(whole_dir / 'aligned.mei', 'w') as aligned_mei_file:
        # We also purge everything that is not an element, to keep the tree clean and easily output a prettified XML file
        aligned_mei_file.write(
            tt.purge_non_element_nodes(skeleton_document).toprettyxml())

    # Update status
    status_update_msg = {
        '_id': sheet_id,
        'module': 'aligner',
        'status': 'complete',
        'name': sheet_name
    }

    global channel
    channel.queue_declare(queue=cfg.mq_omr_planner_status)
    channel.basic_publish(exchange="",
                          routing_key=cfg.mq_omr_planner_status,
                          body=json.dumps(status_update_msg))
Exemplo n.º 11
0
def randomize_note(tree):
    note_count = rnd.randint(8)
    layer = tt.first_or_none(tree, "layer")
    for i in range(note_count):
        element = tt.create_element_node(
            rnd.choice(["note", "rest"]),
            {"dur": rnd.choice(['1/32', '1/16', '1/8', '1/4', '1/2', '1'])})
        layer.appendChild(element)

    return tree.toxml()
Exemplo n.º 12
0
def randomize_clef(tree):
    clef = tt.create_element_node("clef")
    choice = rnd.choice([{
        "shape": "G",
        "line": "2"
    }, {
        "shape": "F",
        "line": "4"
    }, {
        "shape": "C",
        "line": "3"
    }, {}])
    set_attributes(clef, choice)
    tt.first_or_none(tree, "layer").appendChild(clef)
    return tree.toxml()
Exemplo n.º 13
0
 def test_nodes_total_mismatch(self):
     node1 = tt.create_element_node("test", {"val1": 1, "val2": 2})
     node2 = tt.create_element_node("test1", {"val3": 5, "val4": 22})
     self.check(20 + 4 * 4, ta.node_distance(node1, node2))
Exemplo n.º 14
0
def create_gap_element():
    return tt.create_element_node(GAP_ELEMENT_NAME)
Exemplo n.º 15
0
 def test_nodes_equivalent(self):
     node1 = tt.create_element_node("test", {"val1": 1, "val2": 2})
     node2 = tt.create_element_node("test", {"val1": 1, "val2": 2})
     self.check(0, ta.node_distance(node1, node2))
Exemplo n.º 16
0
 def test_nodes_different_name(self):
     node1 = tt.create_element_node("test1", {"val1": 1, "val2": 2})
     node2 = tt.create_element_node("test2", {"val1": 1, "val2": 2})
     self.check(20, ta.node_distance(node1, node2))
Exemplo n.º 17
0
def callback(ch, method, properties, body):
    data = json.loads(body)
    sheet_name = data['name']
    post_processing_steps = data["steps"]
    task_id = data['task_id']

    # Get MEI file
    mei_path = fsm.get_sheet_whole_directory(sheet_name) / "aligned.mei"
    mei_xml_tree = tt.purge_non_element_nodes(xml.parse(str(mei_path)))
    mei_section = mei_xml_tree.getElementsByTagName("section")[0]

    if "clef" in post_processing_steps:
        print(f"Performing clef post-processing for sheet {sheet_name}")
        for layer in mei_xml_tree.getElementsByTagName("layer"):
            element = layer.firstChild

            if element != None and element.tagName=="clef":
                staff = layer.parentNode
                measure = staff.parentNode

                clef_line  = element.getAttribute("line")
                clef_shape = element.getAttribute("shape")
                layer.removeChild(element)

                prev = measure.previousSibling
                scoreDef = None

                while prev:
                    if prev.tagName == "measure":
                        break
                    if prev.tagName == "scoreDef":
                        scoreDef = prev
                        break
                    prev = prev.previousSibling

                # TODO: actually generalize this code
                if not scoreDef:
                    scoreDef = tt.create_element_node("scoreDef")
                    mei_section.insertBefore(scoreDef, measure)

                staffGrp = tt.first_or_none(scoreDef, "staffGrp")
                if not staffGrp:
                    staffGrp = tt.create_element_node("staffGrp")
                    scoreDef.appendChild(staffGrp)

                staffDef = tt.first_or_none(staffGrp, "staffDef", lambda e: e.getAttribute("n") == staff.getAttribute("n"))
                if not staffDef:
                    staffDef = tt.create_element_node("staffDef", {"n": staff.getAttribute("n")})
                    staffGrp.appendChild(staffDef)

                staffDef.setAttribute("clef.line", clef_line)
                staffDef.setAttribute("clef.shape", clef_shape)

    # Write MEI file if there were changes
    if post_processing_steps:
        with open(str(mei_path), 'w') as mei_file:
            mei_file.write(tt.purge_non_element_nodes(mei_xml_tree.documentElement).toprettyxml())

    status_update_msg = {
        '_id': task_id,
        'module': 'post_processing',
        'status': 'complete'
    }

    global channel
    channel.queue_declare(queue=cfg.mq_task_scheduler_status)
    channel.basic_publish(exchange="", routing_key=cfg.mq_task_scheduler_status, body=json.dumps(status_update_msg))