Exemple #1
0
def load_session(session:str, SMOREs_version:str):
    import pickle
    print('Loading Session...')
    _load_path = Path('../input/')
    input_file = _load_path.joinpath(session)
    filehandler = open(input_file, 'rb')
    try:
        session = pickle.load(filehandler)
        if session['version'] != SMOREs_version:
            print('\nWarning: Version of data loaded from previous session is not the same. Some functionality may not be supported.')
            print('Current: {0}\nLoaded: {1} \n'.format(SMOREs_version, session['version']))
        md_e = md.MedicationDictionary.load_session(session['MedicationDictionary'])
        mk_e = MedKit.load_session(session['MedKit'])
        if len(md_e) == 0 and len(mk_e) == 0:
            print('Success!')
        else:
            # TODO Smores Error Encode
            print('Errors encountered while loading session')
            if len(md_e) > 0:
                print('Medication Dictionaries with Errors: {0}'.format(md_e))
            if len(mk_e) > 0:
                print('Files with Errors: {0}'.format(mk_e))
        if 'date' in session.keys():
            print('\nSession Date: {0}'.format(session['date'].strftime("%Y-%m-%d %H:%M")))
        return list(MedKit.get_medkit().keys())
    except FileNotFoundError:
        smores_error('#Cx001.1')
        return None
    except PermissionError:
        smores_error('#Cx001.2')
        return None
Exemple #2
0
def run_client_cmd(client_cmd:str, med_id:str=None, med_id_type:str=None, file:str=None):
    client_cmds = {'rxn_status': {'func': get_rxn_status, 'display': 'RxNorm Status', 'restrict': None},
                   'rxn_ing': {'func': get_rxn_ingredients, 'display': 'RxNorm Ingredients', 'restrict': None},
                   'rxn_lookup': {'func': get_rxn_lookup, 'display': 'RxNorm Lookup', 'restrict': None, 'requires': True},
                   'rxn_remap': {'func': get_rxn_remap, 'display': 'Remapped RxNorm', 'restrict': None},
                   'rxn_history': {'func': get_rxn_history,
                                   'display': 'Retired RxNorm History',
                                   'restrict': m.RxCUI.get_historical_list}
                   # 'rxn_search': get_rxn_search
                   }
    try:
        this_cmd = client_cmds[client_cmd]['func']
        this_display = client_cmds[client_cmd]['display']
        this_restriction = client_cmds[client_cmd]['restrict']
        this_cmd_requires = client_cmds[client_cmd]['requires'] if 'requires' in  client_cmds[client_cmd].keys() else None
    except KeyError:
        smores_error('TBD', client_cmd)
        return

    if file is not None:
        if this_cmd_requires:
            if not get_cmd_requirements(client_cmd, file):
                print('Command {0} does not meet the requirements needed to execute the command'.format(client_cmd))
                return 0, [], file
        success_count, errors = 0, []
        if file == 'ALL':
            medkits = MedKit.get_medkit()
            num_kits = len(medkits)
            if num_kits > 1:
                kits_prog = tqdm(total=num_kits, unit='Files')
            for id, kit in medkits.items():
                _c, _e = process_event(kit, this_cmd, this_display, this_restriction)
                success_count += _c
                if len(_e) > 0:
                    errors = errors + _e if type(_e) is list else errors.append(_e)
                if num_kits > 1:
                    kits_prog.update(1)

        else:
            medkit = MedKit.get_medkit(file) if not isinstance(file, MedKit) else file
            success_count, errors = process_event(medkit, this_cmd, this_display, this_restriction)
        return success_count, errors, file

    elif med_id is not None and med_id_type is not None:
        med = m.get_med_by_id(med_id, med_id_type)
        success, error = this_cmd(med_id=med)
        return success, error, med_id

    else:
        smores_error('TBD', client_cmd)
        return 0, [], med_id
Exemple #3
0
def get_file_cui_types(_file: str):
    if MedKit.src_is_medkit(_file):
        _mks = MedKit.get_medkit(_file)
        if type(_mks) is dict:
            cui_types = []
            for _mk in _mks.values():
                for _ct in _mk.get_cui_types():
                    if _ct not in cui_types:
                        cui_types.append(_ct)
            return cui_types
        else:
            return _mks.get_cui_types()
    elif _file == 'ALL':
        _mks = MedKit.get_medkit()
    else:
        return None
Exemple #4
0
def run_med_to_csv(file:str=None,out_file:str=None, params:dict=None):
    '''
    Only supports outputing bundles, not individual meds
    :param file: the target input file to be output to CSV, or ALL
    :param out_file: basename to be used for file outputs, can be None: default name will be used
    :param params: Customization parameters for the CSV output
    :param incr: Number of medications to be saved per file (not rows)
    :return:
    '''
    smoresLog.debug('Preparing to generate CSV')
    incr = int(util.read_config_value('OUTPUT_CONF')['file_size_max'])
    csv_files = {}
    csv_outputs = params.keys() if params is not None else ''
    if 'detail' in csv_outputs:
        for _d in params['detail'].values():
            base_filename, ext = process_filename(_d['type'])
            csv_files[_d] = {'file': base_filename, 'ext': ext, 'detail': _d}

    if file is not None:
        kits = MedKit.get_medkit(file)
        if type(kits) is dict:
            i=1
            for file, kit in kits.items():
                _file, ext = process_filename(out_file=out_file, info_type=kit.file_name.split('.')[0])
                save_csv_bundle(kit.m_dict, _file, incr=incr, ext=ext)
                i += 1
        else:
            _file, ext = process_filename(out_file=out_file, info_type=kits.file_name.split('.')[0])
            save_csv_bundle(kits.m_dict, _file, incr=incr, ext=ext)

    else:
        smores_error('#Kx001.2')
    return
Exemple #5
0
def run_med_to_json(med_id=None, med_id_type:str=None, file:str=None,
                    out_file:str=None):
    '''

    :param med_id:
    :param med_id_type:
    :param file:
    :param out_file:
    :param incr:
    :return:
    '''
    smoresLog.debug('Preparing to generate FHIR JSON Files')

    def get_bundle_json(in_dict:md.MedicationDictionary, json_file:str):
        incr = int(util.read_config_value('OUTPUT_CONF')['file_size_max'])
        _total = in_dict.get_med_count()
        _iters = int(math.ceil(_total/incr)) if incr is not None else 1
        _med_list = list(in_dict.med_list.values())
        fhir_construct = {'resourceType': 'Bundle', 'date': datetime.today().strftime('%Y-%m-%d %H:%M:%S%z'), 'total':0, 'entry':[]}
        if _iters > 1:
            _count = 0
            print('Medications will be saved across {0} files.'.format(_iters))
            for i in trange(_iters, desc='File Number', position=0):
                bundle = fhir_construct.copy()
                list_end = _count + incr if (_count + incr) < _total else _total
                _b = [med.get_fhir() for med in _med_list[_count:list_end]]
                bundle['entry'] = _b
                bundle['total'] = list_end - _count
                write_file(output_file=json_file, iter=i+1, data=bundle, ext='json')
                _count += incr
        else:
            bundle = fhir_construct
            for med in _med_list:
                 bundle['entry'].append(med.get_fhir())
                 bundle['total'] += 1
            write_file(output_file=json_file, data=fhir_construct, ext='json')
        return

    def get_single_json(object:m.Medication):
        return object.get_fhir()

    if file is not None:
        kits = MedKit.get_medkit(file)
        if type(kits) is dict:
            i=1
            for file, kit in kits.items():
                _file, _ = process_filename(out_file=out_file, info_type=kit.file_name.split('.')[0])
                get_bundle_json(kit.m_dict, _file)
                i += 1
        else:
            _file, _ = process_filename(out_file=out_file, info_type=kits.file_name.split('.')[0])
            get_bundle_json(kits.m_dict, _file)

    elif med_id is not None and med_id_type is not None:
        med_o = m.get_med_by_id(med_id, file)
        med_o.add_json(get_single_json(med_o))
    else:
        smores_error('#Kx001.2')
    return
Exemple #6
0
def save_session(SMOREs_version:str):
    import pickle
    print('Saving Session...')
    _write_path = Path('../output/')
    file_name, ext = process_filename(info_type='session', ext='smr')
    _file = file_name + '.' + ext
    output_path = _write_path.joinpath(_file)
    try:
        filehandler = open(output_path, 'wb')
        medKits = MedKit.get_medkit()
        medDicts = md.get_available_med_dict()
        session = {'MedKit': medKits, 'MedicationDictionary': medDicts, 'version': SMOREs_version, 'date': datetime.now()}
        pickle.dump(session, filehandler)
        if output_path.resolve() and output_path.stat().st_size > 0:
            print('Session Saved: {0}'.format(_file))
    except:
        print('UNKNOWN ERROR - save_session()')
Exemple #7
0
def load_file(input_file: str):
    def process_file(curr_medkit):
        try:
            c_records = 0
            c_dup = 0
            errors = []
            _last = None
            with open(curr_medkit.path, 'r') as file_handle:
                reader = csv.DictReader(file_handle,
                                        delimiter=",",
                                        skipinitialspace=True)

                for line in tqdm(enumerate(reader),
                                 total=curr_medkit.file_lines,
                                 desc="Progress",
                                 unit=' rows'):
                    _med, _dup, _err, _type = line_read(
                        curr_medkit, line, _last, False)
                    if _med is None:
                        smores_error(_err)
                    elif not _med:
                        pass
                    else:
                        if not _dup:
                            curr_medkit.add_med(_med, _type)
                            c_records += 1
                            _last = _med
                        if _dup:
                            c_dup += 1
                        if len(_err) > 0:
                            errors.append(_err)

        except FileNotFoundError:
            return False, "#Cx001.1"
        except PermissionError:
            return False, "#Cx001.2"
        except BaseException as e:
            print(e)
            return False, None
        else:
            time.sleep(.01)  # Clean exit of tqdm
            return {
                'records': c_records,
                'dups': c_dup,
                'errors': errors,
                'file': curr_medkit.file_name
            }

    if ':\\' in input_file:
        input_file_path = Path(input_file).resolve()
    elif 'tests/' in input_file:
        input_file_path = Path("..", 'tests', input_file).resolve()
    else:
        input_file_path = Path("..", 'input', input_file).resolve()

    try:
        if input_file_path.exists():
            medkit = MedKit(input_file_path)
            file_stat = process_file(medkit)
            medkit.set_record_count(file_stat['records'])
        else:
            raise FileNotFoundError
    except FileNotFoundError:
        return False, "#Cx001.1"
    except PermissionError:
        return False, '#Cx001.2'
    except OSError:
        raise OSError('#Cx001.6')
    except BaseException as e:
        print(e)
        return False, None
    else:
        return True, file_stat
Exemple #8
0
def run_client_cmd(client_cmd: str,
                   med_id: str = None,
                   med_id_type: str = None,
                   file: str = None,
                   args=None):
    """

    :param client_cmd:
    :param med_id:
    :param med_id_type:
    :param file:
    :param opts:
    :return:
    """
    client_cmds = {
        'rxn_status': {
            'func': get_rxn_status,
            'display': 'RxNorm Status',
            'restrict': None
        },
        'rxn_ing': {
            'func': get_rxn_ingredients,
            'display': 'RxNorm Ingredients',
            'restrict': None
        },
        'rxn_lookup': {
            'func': get_rxn_lookup,
            'display': 'RxNorm Lookup',
            'restrict': None
        },
        'rxn_remap': {
            'func': get_rxn_remap,
            'display': 'Remapped RxNorm',
            'restrict': None
        },
        'rxn_history': {
            'func': get_rxn_history,
            'display': 'Retired RxNorm History',
            'restrict': m.RxCUI.get_historical_list
        },
        'code_lookup': {
            'func': get_code_lookup,
            'display': 'Code Lookup',
            'restrict': None
        }
        # 'rxn_search': get_rxn_search
    }
    try:
        this_cmd = client_cmds[client_cmd]['func']
        this_display = client_cmds[client_cmd]['display']
        this_restriction = client_cmds[client_cmd]['restrict']
        this_cmd_requires = client_cmds[client_cmd][
            'requires'] if 'requires' in client_cmds[client_cmd].keys(
            ) else None
    except KeyError:
        smores_error('TBD', client_cmd)
        return

    if file is not None:
        # If a command has a requirement in order to process without complete failure, check that it has passed
        if this_cmd_requires:
            pass_requires, error = get_cmd_requirements(
                client_cmd, [file, args])
            if not pass_requires:
                print(
                    'Command {0} does not meet the requirements needed to execute the command'
                    .format(client_cmd))
                if error is not None:
                    print('   Requirements: {0} \n'.format(error))
                return 0, [], file
        success_count, errors = 0, []
        if file == 'ALL':
            medkits = MedKit.get_medkit()
            num_kits = len(medkits)
            if num_kits > 1:
                kits_prog = tqdm(total=num_kits, unit='Files')
            for id, kit in medkits.items():
                _c, _e = process_event(kit, this_cmd, this_display,
                                       this_restriction, args)
                success_count += _c
                if _e is not None and len(_e) > 0:
                    errors = errors + _e if type(
                        _e) is list else errors.append(_e)
                if num_kits > 1:
                    kits_prog.update(1)

        else:
            #elif MedKit.src_is_medkit(file):
            medkit = MedKit.get_medkit(file) if not isinstance(
                file, MedKit) and MedKit.src_is_medkit(file) else file
            success_count, errors = process_event(medkit, this_cmd,
                                                  this_display,
                                                  this_restriction, args)

        return success_count, errors, file

    elif med_id is not None and med_id_type is not None:
        med = m.get_med_by_id(med_id, med_id_type)
        success, error = this_cmd(med_id=med)
        return success, error, med_id

    else:
        smores_error('TBD', client_cmd)
        return 0, [], med_id