def get_morbidities_from_war_code(war_code, include_percentage=False): """ Get morbidities by war code. If the result is already cached, then return the cached result. If not found raise EntityNotFoundError :param war_code: the war code :param include_percentage: True if percentage of morbidity should be added to output, False otherwise :return: the list with morbidities data """ global morbidity_map if morbidity_map.get(war_code) is not None: # return cached data return morbidity_map.get(war_code) file_path = DATASOURCES_DIR + '/morbidity_' + war_code + '.csv' try: with open(file_path, 'rU') as csv_file: morbidity_raw_list = csv.reader(csv_file, delimiter=',', quotechar='"') morbidity_list = [] first = True for row in morbidity_raw_list: if first: # skip first row with header first = False continue item = {'name': row[0], 'icd10Code': row[1]} if include_percentage: item['percentOfProbabilityToAcquireDiagnosis'] = float(row[2]) morbidity_list.append(item) morbidity_map[war_code] = morbidity_list return morbidity_list except Exception as e: logger.error(e) raise EntityNotFoundError('Could not open {0}. Error: {1}'.format(file_path, e))
def save(body_entity): """ Check whether the body entity warEra and morbidities exist or not. Then save it to the local file system. :param body_entity: the request dataset configuration entity :return: the same fully populated dataset configuration entity """ # make sure war era exists war_era = get_war_era_by_name(body_entity['warEra']['warEra']) # update request war era body_entity['warEra'] = convert_raw_war(war_era) total_morbidities = get_morbidities_from_war_code(war_era['war_code']) for request_morbidity in body_entity['morbiditiesData']: morbidity_code = request_morbidity['icd10Code'] # check whether morbidity exists in CSV file of the specified war morbidity_exists = False for morbidity in total_morbidities: if morbidity_code == morbidity['icd10Code']: # set or update morbidity name in request request_morbidity['name'] = morbidity['name'] morbidity_exists = True break # if not found, try to find morbidity in ICD-10 datasource if not morbidity_exists: morbidity_name = get_icd_morbidity_name_by_code(morbidity_code) if morbidity_name: # set or update morbidity name in request request_morbidity['name'] = morbidity_name morbidity_exists = True if not morbidity_exists: raise EntityNotFoundError( 'Morbidity with ICD-10 code {0} is unknown'.format( morbidity_code)) # set default output folder to the configuration output_folder = GENERATED_DATASETS_DIR # try to use short relative to the current directory path if possible try: output_folder = relpath(output_folder) except ValueError: pass body_entity['outputFolder'] = output_folder configuration_file = DATASET_CONFIGURATIONS_DIR + '/' + CONFIGURATION_PREFIX + '.' + body_entity[ 'title'] + '.json' try: with open(configuration_file, 'w') as f: f.write(dumps(body_entity, indent=2)) return body_entity except Exception as e: raise InnerServerError('Cannot save file {0}. Error: {1}'.format( configuration_file, e))
def delete_dataset_by_title(title): """ Delete dateset by title It raises EntityNotFoundError if dataset not found :param title: the dataset title """ dataset_path = join(GENERATED_DATASETS_DIR, title) if not exists(dataset_path): raise EntityNotFoundError("Dataset not found where name = " + title) rmtree(dataset_path)
def generate(title): """ Generate a dataset according to dataset configuration file with the specified title. It raises EntityNotFoundError if file cannot be found. :param title: the dataset configuration title :return: the number of generated report files """ configurations = dataset_configuration_service.get(title) # the length will be 1 if len(configurations) <= 0: raise EntityNotFoundError('Cannot find configuration with title ' + title) return generate_from_config(configurations[0])
def get_war_era_by_name(name): """ Get war era by its name. Raises EntityNotFoundError if not found. :param name: the war name :return: the war era details """ wars = get_wars_from_file() filter_wars = list(filter(lambda w: w['war_name'] == name, wars)) if len(filter_wars) <= 0: raise EntityNotFoundError('Cannot find war era with name ' + name) return filter_wars[0]
def get_configuration_by_title(title): """ Get single configuration with the specified title :param title: the configuration title :return: the dataset configuration instance """ configuration_file_path = DATASET_CONFIGURATIONS_DIR + '/' + CONFIGURATION_PREFIX + '.' + title + '.json' if isfile(configuration_file_path): return read_configuration_from_file(configuration_file_path) else: raise EntityNotFoundError('Cannot find configuration file for title ' + title)
def get_study_profile_by_name(name): """ Get study profile by its name. Raises EntityNotFoundError if not found. :param name: the study profile name :return: the study profile details """ study_profiles = get_study_profiles_from_file() filter_study_profiles = list( filter(lambda w: w['study_profile_name'] == name, study_profiles)) if len(filter_study_profiles) <= 0: raise EntityNotFoundError('Cannot find study profile with name ' + name) return filter_study_profiles[0]
def delete_config_by_title(title): """ Delete dateset Config by title It raises EntityNotFoundError if dataset config not found :param title: the dataset Config title """ config_file = join( DATASET_CONFIGURATIONS_DIR, '{0}.{1}.{2}'.format(CONFIGURATION_PREFIX, title, 'json')) if not exists(config_file): raise EntityNotFoundError("Dataset config not found where title = " + title) remove(config_file)
def delete_config_by_title(title): """ Delete dateset Config by title It raises EntityNotFoundError if dataset config not found :param title: the dataset Config title """ config_file = join( DATASET_CONFIGURATIONS_DIR, '{0}.{1}.{2}'.format(CONFIGURATION_PREFIX, title, 'json')) if not exists(config_file): raise EntityNotFoundError("Dataset config not found where title = " + title) remove(config_file) if get_cache_map().get(title) is not None: # delete from cache get_cache_map().pop(title, None) from rest.services.dataset_generation_service import remove_dateset_by_config_title remove_dateset_by_config_title(title)