Skip to content

Commit

Permalink
Merge pull request #47 from harmonydata/load_instrument_from_list
Browse files Browse the repository at this point in the history
Load instrument from list
  • Loading branch information
woodthom2 committed Jun 21, 2024
2 parents 6392abc + 86c253b commit e8e7f3c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/harmony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

# TODO: make these configurable at package level
import os
from .schemas import *

from .examples import example_instruments
from .schemas import *
from .util.instrument_helper import create_instrument_from_list, import_instrument_into_harmony_web
from .util.model_downloader import download_models

if os.environ.get("HARMONY_NO_PARSING") is None or os.environ.get("HARMONY_NO_PARSING") == "":
Expand All @@ -43,7 +45,8 @@

if os.environ.get("HARMONY_NO_MATCHING") is None or os.environ.get("HARMONY_NO_MATCHING") == "":
from .matching.matcher import match_instruments_with_function

try:
from .matching.default_matcher import match_instruments
except ModuleNotFoundError:
print ("Warning: transformers not available. To use transformers, run pip install sentence-transformers")
print("Warning: transformers not available. To use transformers, run pip install sentence-transformers")
60 changes: 60 additions & 0 deletions src/harmony/util/instrument_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'''
MIT License
Copyright (c) 2023 Ulster University (https://www.ulster.ac.uk).
Project: Harmony (https://harmonydata.ac.uk)
Maintainer: Thomas Wood (https://fastdatascience.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import base64
import json

from harmony.schemas.requests.text import Instrument, Question


def create_instrument_from_list(question_texts: list, instrument_name: str = "My instrument") -> Instrument:
"""
Read a list of strings and create an Instrument object.
:return: Single Instrument.
"""

questions = []
for ctr, question_text in enumerate(question_texts):
questions.append(Question(question_text=question_text, question_no=str(ctr + 1)))

return Instrument(questions=questions, instrument_name=instrument_name)


def import_instrument_into_harmony_web(instrument: Instrument, harmony_fe_base_url="https://harmonydata.ac.uk") -> str:
"""
Import a single instrument into the Harmony web UI.
@param instrument: An instrument object created by Harmony
@param harmony_fe_base_url: The base URL of the React app front end, defaulting to the web Harmony front end at harmonydata.ac.uk
@return: a URL which you can click which will take you to the browser.
"""
instrument_serialised_as_json = json.dumps(instrument.dict())
instrument_json_b64_encoded_bytes = base64.b64encode(instrument_serialised_as_json.encode('utf-8'))
instrument_json_b64_encoded_str = instrument_json_b64_encoded_bytes.decode("utf-8")

url = f"{harmony_fe_base_url}/#/import/{instrument_json_b64_encoded_str}"

return url
54 changes: 54 additions & 0 deletions tests/test_create_instrument_from_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'''
MIT License
Copyright (c) 2023 Ulster University (https://www.ulster.ac.uk).
Project: Harmony (https://harmonydata.ac.uk)
Maintainer: Thomas Wood (https://fastdatascience.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import sys
import unittest

sys.path.append("../src")

from harmony import create_instrument_from_list, import_instrument_into_harmony_web


class TestCreateInstrument(unittest.TestCase):

def test_single_instrument_simple(self):
instrument = create_instrument_from_list(["question A", "question B"])
self.assertEqual(2, len(instrument.questions))

def test_single_instrument_simple_2(self):
instrument = create_instrument_from_list(["question A", "question B", "question C"], instrument_name="potato")
self.assertEqual(3, len(instrument.questions))
self.assertEqual("potato", instrument.instrument_name)

def test_single_instrument_send_to_web(self):
instrument = create_instrument_from_list(["question A", "question B"])
web_url = import_instrument_into_harmony_web(instrument)
self.assertIn("harmonydata.ac.uk", web_url)


if __name__ == '__main__':
unittest.main()

0 comments on commit e8e7f3c

Please sign in to comment.