Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#11 Expose option of turning on/off negation behaviour through the API #15

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion harmony_api/routers/text_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def match(
_model_is_available=Depends(dependencies.model_from_match_body_is_available),
include_catalogue_matches: bool = Query(default=False),
catalogue_sources: List[str] = Query(default=[]),
is_negate: bool = True
) -> MatchResponse:
"""
Match instruments.
Expand Down Expand Up @@ -270,6 +271,7 @@ def match(
mhc_embeddings=mhc_embeddings,
texts_cached_vectors=texts_cached_vectors,
vectorisation_function=vectorisation_function,
is_negate=is_negate
)

# Get catalogue matches
Expand Down Expand Up @@ -426,4 +428,3 @@ def search_instruments(
]

return SearchInstrumentsResponse(instruments=instruments)

12 changes: 7 additions & 5 deletions tests/generate_remote_test.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_parse_local.py > remote_tests/test_parse_remote.py
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_match_local.py > remote_tests/test_match_remote.py
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_match_mhc_local.py > remote_tests/test_match_mhc_remote.py
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_negate_local.py > remote_tests/test_negate_remote.py
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_different_frameworks_local.py > remote_tests/test_different_frameworks_remote.py
sed s#http://localhost:8000#https://api.harmonydata.ac.uk#g local_tests/test_match_local_bigger.py > remote_tests/test_match_remote_bigger.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_parse_local.py > staging_tests/test_parse_remote.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_local.py > staging_tests/test_match_remote.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_mhc_local.py > staging_tests/test_match_mhc_remote.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_different_frameworks_local.py > staging_tests/test_different_frameworks_remote.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_local_bigger.py > staging_tests/test_match_remote_bigger.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_parse_local.py > staging_tests/test_parse_staging.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_local.py > staging_tests/test_match_staging.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_mhc_local.py > staging_tests/test_match_mhc_staging.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_negate_local.py > staging_tests/test_negate_staging.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_different_frameworks_local.py > staging_tests/test_different_frameworks_staging.py
sed s#http://localhost:8000#https://harmonystagingtmp.azurewebsites.net#g local_tests/test_match_local_bigger.py > staging_tests/test_match_staging_bigger.py
sed s#http://localhost:3000#https://harmonydata.ac.uk#g selenium_tests/test_selenium_local.py > selenium_tests/test_selenium_remote.py
88 changes: 88 additions & 0 deletions tests/local_tests/test_negate_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'''
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 unittest

import requests

headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
}

json_data_to_match_negation = {
'instruments': [
{
'questions': [
{
'question_no': '1',
'question_text': 'I feel nervous',
},
{
'question_no': '2',
'question_text': 'I don\'t feel nervous',
},
],
}
]
}

endpoint = 'http://localhost:8000/text/match'

response = requests.post(endpoint, headers=headers, json=json_data_to_match_negation)

response_no_negation = requests.post(endpoint + "?is_negate=false", headers=headers, json=json_data_to_match_negation)


class TestMatch(unittest.TestCase):

def test_negation_instrument_correct_size_dictionary_response(self):
self.assertEqual(5, len(response.json()))

def test_negation_instrument_first_question_conserved(self):
self.assertEqual("I feel nervous", response.json()["questions"][0]["question_text"])

def test_negation_instrument_correct_number_of_questions(self):
self.assertEqual(2, len(response.json()["questions"]))

def test_negation_instrument_correct_number_of_matches(self):
self.assertEqual(2, len(response.json()["matches"]))
self.assertEqual(2, len(response.json()["matches"][0]))

def test_negation_instrument_high_like_for_like_match(self):
self.assertLess(0.99, response.json()["matches"][0][0])

def test_negation_instrument_negative_match(self):
self.assertGreater(0, response.json()["matches"][0][1])

def test_negation_instrument_positive_match(self):
self.assertLess(0, response_no_negation.json()["matches"][0][1])


if __name__ == '__main__':
unittest.main()
88 changes: 88 additions & 0 deletions tests/remote_tests/test_negate_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'''
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 unittest

import requests

headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
}

json_data_to_match_negation = {
'instruments': [
{
'questions': [
{
'question_no': '1',
'question_text': 'I feel nervous',
},
{
'question_no': '2',
'question_text': 'I don\'t feel nervous',
},
],
}
]
}

endpoint = 'https://api.harmonydata.ac.uk/text/match'

response = requests.post(endpoint, headers=headers, json=json_data_to_match_negation)

response_no_negation = requests.post(endpoint + "?is_negate=false", headers=headers, json=json_data_to_match_negation)


class TestMatch(unittest.TestCase):

def test_negation_instrument_correct_size_dictionary_response(self):
self.assertEqual(5, len(response.json()))

def test_negation_instrument_first_question_conserved(self):
self.assertEqual("I feel nervous", response.json()["questions"][0]["question_text"])

def test_negation_instrument_correct_number_of_questions(self):
self.assertEqual(2, len(response.json()["questions"]))

def test_negation_instrument_correct_number_of_matches(self):
self.assertEqual(2, len(response.json()["matches"]))
self.assertEqual(2, len(response.json()["matches"][0]))

def test_negation_instrument_high_like_for_like_match(self):
self.assertLess(0.99, response.json()["matches"][0][0])

def test_negation_instrument_negative_match(self):
self.assertGreater(0, response.json()["matches"][0][1])

def test_negation_instrument_positive_match(self):
self.assertLess(0, response_no_negation.json()["matches"][0][1])


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@

response = requests.post(endpoint, headers=headers, json=json_data_to_match_gad_7)


class TestMatchBiggerPayload(unittest.TestCase):

def test_big_match_command(self):
Expand Down
88 changes: 88 additions & 0 deletions tests/staging_tests/test_negate_staging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'''
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 unittest

import requests

headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
}

json_data_to_match_negation = {
'instruments': [
{
'questions': [
{
'question_no': '1',
'question_text': 'I feel nervous',
},
{
'question_no': '2',
'question_text': 'I don\'t feel nervous',
},
],
}
]
}

endpoint = 'https://harmonystagingtmp.azurewebsites.net/text/match'

response = requests.post(endpoint, headers=headers, json=json_data_to_match_negation)

response_no_negation = requests.post(endpoint + "?is_negate=false", headers=headers, json=json_data_to_match_negation)


class TestMatch(unittest.TestCase):

def test_negation_instrument_correct_size_dictionary_response(self):
self.assertEqual(5, len(response.json()))

def test_negation_instrument_first_question_conserved(self):
self.assertEqual("I feel nervous", response.json()["questions"][0]["question_text"])

def test_negation_instrument_correct_number_of_questions(self):
self.assertEqual(2, len(response.json()["questions"]))

def test_negation_instrument_correct_number_of_matches(self):
self.assertEqual(2, len(response.json()["matches"]))
self.assertEqual(2, len(response.json()["matches"][0]))

def test_negation_instrument_high_like_for_like_match(self):
self.assertLess(0.99, response.json()["matches"][0][0])

def test_negation_instrument_negative_match(self):
self.assertGreater(0, response.json()["matches"][0][1])

def test_negation_instrument_positive_match(self):
self.assertLess(0, response_no_negation.json()["matches"][0][1])


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