Esempio n. 1
0
def main(script, task='', case='', output=''):
    """Perform project tasks.

    'task' is the project task to run.
    'case' is a path to the case to run, optional.
    'output' is the output to generate, optional.
    Usage: python3 <task> [<case|chapter>] [latex|json]
    """
    if not task:
        print("Usage: python3 <task> [<case|chapter>] [latex|json]")
        sys.exit(2)

    if get_empty_indices():
        print("Empty indices, run 'python3 index.py build' first!")
        sys.exit(1)

    if case and case in OUTPUTS:
        output = case
        case = ''
    if output in ('json', 'latex') and not os.path.exists(OUTPUT_FOLDER):
        os.mkdir(OUTPUT_FOLDER)
    if output not in OUTPUTS:
        print("Unknown output %s, valid:" % output, ', '.join(OUTPUTS.keys()))
        sys.exit(2)

    populate_all()

    if task == '5':
        task_5()

    # Perform a task which uses patient cases as input
    elif task in CASE_TASKS:
        cases = {name: obj for name, obj in PatientCase.ALL.items()
                    if (not case or name == case)}
        if not cases:
            print("Unknown patient case: %s" % case)
            sys.exit(2)
        _perform_task(task, CASE_TASKS[task], cases, OUTPUTS[output])

    # Perform a task which uses chapters as input
    elif task in CHAPTER_TASKS:
        chapters = {code: obj for code, obj in Therapy.ALL.items()
                        if (not case or case == obj.code)}
        if not chapters:
            print("Unknown therapy code: %s" % case)
            sys.exit(2)
        _perform_task(task, CHAPTER_TASKS[task], chapters,
                      OUTPUTS[output], progress=True)

    else:
        print("Unknown task '%s', valid tasks are: %s" % (task,
            ', '.join(list(CASE_TASKS.keys()) + list(CHAPTER_TASKS.keys()))))
        sys.exit(2)

    sys.exit(None)
Esempio n. 2
0
def main(script, command='', index='', field='', *query):
    """Store, clear or search data in whoosh indices.

    Can also be used to create vectors needed for task 3.
    'command' is either build|store|clean|search|vector
    'index' is either atc|icd|therapy|case

    Usage: python3 index.py <command> [index] [field] [query]
    """
    # Store all objects in index
    if command == 'build':
        populate_all()
        empty = get_empty_indices()
        for cls in empty:
            store_objects_in_index(cls)
        return

    classes = [ATC, ICD, PatientCase, Therapy]
    if index:
        classes = [i for i in classes if i._NAME == index]
        if not classes:
            print("Unknown index %s, valids: atc|icd|case|therapy" % index)
            sys.exit(2)

    # Store objects in index, will create duplicates if run several times
    if command == 'store':
        populate_all()
        for cls in classes:
            store_objects_in_index(cls)

    # Empty index
    elif command in ('clean', 'clear'):
        for cls in classes:
            create_or_open_index(cls)
            create_in(INDEX_DIR, SCHEMA_MAP[cls._NAME], cls._NAME)
            print("Emptied %s index" % cls.__name__)

    # Create vectors
    elif command.startswith('vector'):
        populate_all()
        create_vectors()

    # Search in whoosh index
    elif command == 'search':
        mapping = {'icd': ('short', 'label'), 'atc': ('code', 'title'),
                   'therapy': ('code', 'title'), 'case': ('code',)}
        query = ''.join(query)  # Flatten query
        cls, = classes  # Can only search on one index at a time
        print_result(extract(mapping[cls._NAME], search(cls, field, query)))

    # Unknown command
    else:
        print("Unknown command '%s'" % command)
        print("Usage: python3 index.py <command> [index] [field] [query]")
        print("Command is either build|store|clean|search|vector")
        sys.exit(2)

    sys.exit(None)