diff --git a/Harmony_example_walkthrough.ipynb b/Harmony_example_walkthrough.ipynb index 3dd6605..8fcb9cc 100644 --- a/Harmony_example_walkthrough.ipynb +++ b/Harmony_example_walkthrough.ipynb @@ -291,7 +291,7 @@ "id": "c296cf93-e760-4184-b179-ea3f4a3f83d5", "metadata": {}, "source": [ - "See the questions and topics" + "See the questions, topics, and response options" ] }, { @@ -306,69 +306,99 @@ "text": [ "I was bothered by things that usually don’t bother me.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I did not feel like eating; my appetite was poor.\n", "\t Topics: ['eating']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt that I could not shake off the blues even with help from my family or friends.\n", "\t Topics: ['difficulty']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt I was just as good as other people.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I had trouble keeping my mind on what I was doing.\n", "\t Topics: ['difficulty']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt depressed.\n", "\t Topics: ['unhappy']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt that everything I did was an effort.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt hopeful about the future.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I thought my life had been a failure.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt fearful.\n", "\t Topics: ['scared']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "My sleep was restless.\n", "\t Topics: ['sleep']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I was happy.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I talked less than usual.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt lonely.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "People were unfriendly.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I enjoyed life.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I had crying spells.\n", "\t Topics: ['unhappy']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt sad.\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I felt that people dislike me.\n", "\t Topics: ['unhappy']\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "I could not get “going.”\n", "\t Topics: []\n", + "\t Response Options: ['Rarely or none of the time (less than 1 day)', 'Some or a little of the time (1-2 days)', 'Occasionally or a moderate amount of time (3-4 days)', 'Most or all of the time (5-7 days)']\n", "Sentir-se nervoso/a, ansioso/a ou muito tenso/a\n", "\t Topics: ['nervous']\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Não ser capaz de impedir ou de controlar as preocupações\n", "\t Topics: []\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Preocupar-se muito com diversas coisas\n", "\t Topics: ['scared']\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Dificuldade para relaxar\n", "\t Topics: []\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Ficar tão agitado/a que se torna difícil permanecer sentado/a\n", "\t Topics: ['difficulty']\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Ficar facilmente aborrecido/a ou irritado/a\n", "\t Topics: ['unhappy']\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Sentir medo como se algo horrível fosse acontecer\n", "\t Topics: ['scared']\n", + "\t Response Options: ['Nenhuma vez', 'Vários dias', 'Mais da metade dos dias', 'Quase todos os dias']\n", "Følt deg nervøs, engstelig eller veldig stresset\n", "\t Topics: ['nervous', 'scared']\n", + "\t Response Options: []\n", "Ikke klart å slutte å bekymre deg eller kontrolleren bekymringene dine\n", - "\t Topics: ['anxiety', 'scared']\n" + "\t Topics: ['anxiety', 'scared']\n", + "\t Response Options: []\n" ] } ], "source": [ "for q in questions:\n", " print (q.question_text)\n", - " print(\"\\t\", \"Topics:\", q.topics)" + " print(\"\\t\", \"Topics:\", q.topics)\n", + " print(\"\\t\", \"Response Options:\", q.options)" ] }, { diff --git a/src/harmony/util/instrument_helper.py b/src/harmony/util/instrument_helper.py index 8f69d27..6ab4609 100644 --- a/src/harmony/util/instrument_helper.py +++ b/src/harmony/util/instrument_helper.py @@ -35,7 +35,8 @@ def create_instrument_from_list(question_texts: list, answer_texts: list, question_numbers: list = None, answer_numbers: list = None, instrument_name: str = "My instrument", - file_name="My file") -> Instrument: + file_name="My file", + options_map= None) -> Instrument: """ Read a list of strings and create an Instrument object. :return: Single Instrument. @@ -46,6 +47,11 @@ def create_instrument_from_list(question_texts: list, answer_texts: list, questi question_no = question_numbers[ctr] else: question_no = str(ctr + 1) + + options = options_map.get(question_text, []) if options_map else [] + + questions.append(Question(question_text=question_text, question_no=question_no, + options=options)) questions.append(Question(question_text=question_text, question_no=question_no, options=answer_texts)) return Instrument(questions=questions, instrument_name=instrument_name, instrument_id=uuid.uuid4().hex, diff --git a/tests/test_create_instrument_from_list.py b/tests/test_create_instrument_from_list.py index f6584ec..3c8e950 100644 --- a/tests/test_create_instrument_from_list.py +++ b/tests/test_create_instrument_from_list.py @@ -58,6 +58,17 @@ def test_single_instrument_send_to_web(self): web_url = import_instrument_into_harmony_web(instrument) self.assertIn("harmonydata.ac.uk", web_url) + def test_single_instrument_with_options(self): + question_texts = ["How often do you feel anxious?", "Do you have trouble sleeping?"] + options_map = { + "How often do you feel anxious?": ["Never", "Sometimes", "Often", "Always"], + "Do you have trouble sleeping?": ["Yes", "No"] + } + instrument = create_instrument_from_list(question_texts, options_map=options_map) + + self.assertEqual(len(instrument.questions), 2) + self.assertEqual(instrument.questions[0].options, ["Never", "Sometimes", "Often", "Always"]) + self.assertEqual(instrument.questions[1].options, ["Yes", "No"]) if __name__ == '__main__': unittest.main()