Exemplo n.º 1
0
            if not args or args[0] == '--':
                print('-c requires argument', file=sys.stderr)
                usage()
            build_dictionary.readConfig(settings, args.pop(0))
        else:
            print('unrecognized argument: ' + arg, file=sys.stderr)
            usage()

    build_dictionary.globalSymbolsFile = path_correction + settings['filename']
    build_dictionary.icd9File = path_correction + settings['icd9']
    build_dictionary.ccs_diag_file = path_correction + settings['ccs_diag']
    build_dictionary.ccs_proc_file = path_correction + settings['ccs_proc']
    build_dictionary.productFile = path_correction + settings['ndc_prod']
    build_dictionary.packageFile = path_correction + settings['ndc_package']
    build_dictionary.reportMissingEntries = False
    build_dictionary.init()

    allPaths = []
    while args:
        path = args.pop(0)
        if os.path.isfile(path) or path == '-':
            allPaths.append((path, True))
        elif os.path.isdir(path):
            allPaths.append((path, False))
        else:
            print('illegal argument: ' + path +
                  ' is neither file nor directory',
                  file=sys.stderr)

    vectors = {}
    header_list = []
Exemplo n.º 2
0
            if not args or args[0] == '--':
                print('-c requires argument', file=sys.stderr)
                usage()
            build_dictionary.readConfig(settings, args.pop(0))
        else:
            print('unrecognized argument: ' + arg, file=sys.stderr)
            usage()

    build_dictionary.globalSymbolsFile = path_correction + settings['filename']
    build_dictionary.icd9File = path_correction + settings['icd9']
    build_dictionary.ccs_diag_file = path_correction + settings['ccs_diag']
    build_dictionary.ccs_proc_file = path_correction + settings['ccs_proc']
    build_dictionary.productFile = path_correction + settings['ndc_prod']
    build_dictionary.packageFile = path_correction + settings['ndc_package']
    build_dictionary.reportMissingEntries = False
    build_dictionary.init()

    allPaths = []
    while args:
        path = args.pop(0)
        if os.path.isfile(path) or path == '-':
            allPaths.append((path, True))
        elif os.path.isdir(path):
            allPaths.append((path, False))
        else:
            print('illegal argument: '+path+' is neither file nor directory', file=sys.stderr)

    vectors = {}
    header_list = []
    header_counts = {}
    processAll(vectors, header_list, header_counts, allPaths, whitelist)
Exemplo n.º 3
0
def start_server(max_num, settings_file, format_file, class_file, line_file, cms_path, addr, port, debug):
    settings = {}
    util.read_config(settings, settings_file, True)
    use_cache = settings.get('use_cache', True)

    all_paths = []
    input_format = {}
    use_db = False
    omop = None
    if settings.get('omop_use_db', False):
        use_db = True
        omop = OMOP(settings, True)
    else:
        util.convert_paths([ cms_path ], all_paths)

        util.read_format(format_file, input_format, usage)
        cms_analyze.input_format = input_format
        cms_get_patient.input_format = input_format

        build_dictionary.debugOutput = True
        build_dictionary.init(settings, settings_file)

    dictionary_file = os.path.join(json_dir, dictionary_bind)

    patients = set()
    def save_patients():
        if not use_cache:
            return
        with open(patients_list, 'w') as pf:
            pf.write('\n'.join(sorted(list(patients))))
            pf.flush()

    if not os.path.isfile(patients_list) or not use_cache:
        if use_db:
            omop.list_patients(patients, prefix=json_dir, limit=max_num, show_old_ids=True)
        else:
            tf = StringIO()
            cms_analyze.compute(all_paths, {}, False, tf, filter_zero=True)
            tf.flush()
            tf.seek(0)
            lines = tf.readlines()[-max_num:] if max_num is not None else tf.readlines()
            for line in lines:
                patients.add(json_dir + line.strip() + '.json')
        save_patients()

    dict = {}
    if use_cache:
        if os.path.isfile(dictionary_file):
            with open(dictionary_file, 'r') as input:
                dict = json.loads(input.read())
        else:
            os.makedirs(json_dir)
            # write the initial empty dictionary
            # also ensures that the folder is writeable
            with open(dictionary_file, 'w') as output:
                output.write("{}")

    server = create_server((addr, port))
    server.bind_path('/', '..')

    prefix = '/' + os.path.basename(os.path.normpath(server.base_path))

    server.add_default_white_list()
    server.add_file_patterns([
            prefix + '/' + json_dir + '*',
            prefix + '/' + patients_list
        ], True)
    server.favicon_fallback = 'favicon.ico'
    server.report_slow_requests = True
    if debug:
        server.suppress_noise = True

    @server.text_get(prefix + '/' + patients_list, 0)
    def get_list(req, args):
        return '\n'.join(sorted(list(patients)))

    @server.json_get(prefix + '/' + json_dir, 1)
    def get_patient(req, args):
        pid = args['paths'][0]
        if pid.endswith('.json') and use_db:
            pid = pid[:-len('.json')]
            pid = omop.get_person_id(pid)
        cache_file = os.path.join(json_dir, pid)
        p_name = json_dir + pid.strip()
        if p_name not in patients:
            patients.add(p_name)
            save_patients()
        if pid.endswith('.json'):
            pid = pid[:-len('.json')]
        if not os.path.isfile(cache_file) or not use_cache:
            if use_db:
                patient = omop.get_patient(pid, dict, line_file, class_file)
            else:
                patient = cms_get_patient.process(all_paths, line_file, class_file, pid)
                build_dictionary.extractEntries(dict, patient)
            if use_cache:
                with open(cache_file, 'w') as pf:
                    pf.write(json_dumps(patient))
                    pf.flush()
            if use_cache:
                with open(dictionary_file, 'w') as output:
                    output.write(json_dumps(dict))
                    output.flush()
            return patient
        with open(cache_file, 'r') as pf:
            return json.loads(pf.read())

    @server.json_get(prefix + '/' + dictionary_file)
    def get_dictionary(req, args):
        return dict

    msg("starting server at {0}:{1}", addr if addr else 'localhost', port)
    server.serve_forever()
    msg("shutting down..")
    server.server_close()
Exemplo n.º 4
0
            if not args or args[0] == '--':
                print('-c requires argument', file=sys.stderr)
                usage()
            settingsFile = args.pop(0)
            util.read_config(settings, settingsFile, build_dictionary.debugOutput)
        elif arg == '--debug':
            build_dictionary.debugOutput = True
        else:
            print('unrecognized argument: ' + arg, file=sys.stderr)
            usage()

    if not len(query):
        print('query is required', file=sys.stderr)
        usage()

    build_dictionary.init(settings, settingsFile)

    allPaths = []
    while args:
        path = args.pop(0)
        if os.path.isfile(path) or path == '-':
            allPaths.append((path, True))
        elif os.path.isdir(path):
            allPaths.append((path, False))
        else:
            print('illegal argument: '+path+' is neither file nor directory', file=sys.stderr)

    qm = parseQuery(query)
    cohort = []
    processAll(qm, cohort, allPaths)
    with util.OutWrapper(output) as out: