def get_ohdsi_cohort(ent: str, attr: str, phenotype: PhenotypeModel):
    if len(phenotype['cohorts']) > 0:
        for c in phenotype['cohorts']:
            if c['name'] == ent and c['library'] == 'OHDSI' and c['funct'] == 'getCohort' and len(c['arguments']) > 0:
                cohort = getCohort(c['arguments'][0])['Patients']
                df = pd.DataFrame.from_records(cohort)
                df['cohortStartDate'] = df['cohortStartDate'].apply(long_to_datetime)
                df['cohortEndDate'] = df['cohortEndDate'].apply(long_to_datetime)
                df['subject'] = df['subjectId'].astype(int)
                return df

    return pd.DataFrame({'nothing': []})
def get_ohdsi_cohort(ent: str, attr: str, phenotype: PhenotypeModel):
    if len(phenotype['cohorts']) > 0:
        for c in phenotype['cohorts']:
            if c['name'] == ent and c['library'] == 'OHDSI' and c['funct'] == 'getCohort' and len(c['arguments']) > 0:
                cohort = getCohort(c['arguments'][0])['Patients']
                df = pd.DataFrame.from_records(cohort)
                df['cohortStartDate'] = df['cohortStartDate'].apply(long_to_datetime)
                df['cohortEndDate'] = df['cohortEndDate'].apply(long_to_datetime)
                df['subject'] = df['subjectId'].astype(int)
                return df

    return pd.DataFrame({'nothing': []})
def make_fq(types, tags, fq, mapper_url, mapper_inst, mapper_key,
            report_type_query, cohort_ids):
    new_fq = fq

    mapped_items = get_report_type_mappings(mapper_url, mapper_inst,
                                            mapper_key)

    if types and len(types) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_type_fq = 'report_type: ("' + '" OR "'.join(types) + '")'
        new_fq += report_type_fq

    if len(report_type_query) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_types = 'report_type: (' + report_type_query + ')'
        new_fq += report_types

    if len(cohort_ids) > 0:
        subjects = list()
        for c in cohort_ids:
            patients = getCohort(c)['Patients']
            subjects.extend([str(x['subjectId']) for x in patients])
            del patients
        if len(subjects) > 0:
            if len(new_fq) > 0:
                new_fq += ' AND '
            subject_fq = 'subject: (' + ' OR '.join(subjects) + ')'
            new_fq += subject_fq

    if len(tags) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        matched_reports = list()
        for tag in tags:
            try:
                lookup_tag = normalize_tag(tag)
                matched_reports.extend(mapped_items[lookup_tag])
            except Exception as e:
                traceback.print_exc(file=sys.stderr)
                print("Unable to map tag %s" % tag)
        if len(matched_reports) > 0:
            match_report_clause = '" OR "'.join(matched_reports)
            report_types = 'report_type: ("' + match_report_clause + '")'
            new_fq += report_types

    return new_fq
Exemple #4
0
def make_fq(types, tags, fq, mapper_url, mapper_inst, mapper_key,
            report_type_query, cohort_ids, job_results_filter, sources):
    new_fq = fq

    subjects = list()
    documents = list()

    if types and len(types) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_type_fq = util.solr_report_type_field + ': ("' + '" OR "'.join(
            types) + '")'
        new_fq += report_type_fq

    if len(report_type_query) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_types = util.solr_report_type_field + ': (' + report_type_query + ')'
        new_fq += report_types

    if job_results_filter:
        for k in job_results_filter.keys():
            job_filter = job_results_filter[k]
            context = job_filter.pop('context', None)
            results = phenotype_results_by_context(context, job_filter)
            if context.lower() == 'patient' or context.lower() == 'subject':
                subjects.extend(set([str(x['subject']) for x in results]))
            else:
                documents.extend(set([str(x['report_id']) for x in results]))

            del results

    if len(cohort_ids) > 0:
        for c in cohort_ids:
            patients = getCohort(c)['Patients']
            subjects.extend([str(x['subjectId']) for x in patients])
            del patients

    if len(tags) > 0:
        mapped_items = get_report_type_mappings(mapper_url, mapper_inst,
                                                mapper_key)
        if len(new_fq) > 0:
            new_fq += ' AND '
        matched_reports = list()
        for tag in tags:
            try:
                lookup_tag = normalize_tag(tag)
                if lookup_tag in mapped_items:
                    matched_reports.extend(mapped_items[lookup_tag])
            except Exception as e:
                if util.debug_mode:
                    traceback.print_exc(file=sys.stderr)
                    print("Unable to map tag %s" % tag)
        if len(matched_reports) > 0:
            match_report_clause = '" OR "'.join(matched_reports)
            report_types = util.solr_report_type_field + ': ("' + match_report_clause + '")'
            new_fq += report_types

    if len(subjects) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        subject_fq = util.solr_subject_field + ': (' + ' OR '.join(
            subjects) + ')'
        new_fq += subject_fq

    if len(documents) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        doc_fq = util.solr_report_id_field + ': (' + ' OR '.join(
            subjects) + ')'
        new_fq += doc_fq

    if sources and len(sources) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        sources_fq = util.solr_source_field + ': ("' + '" OR "'.join(
            sources) + '")'
        new_fq += sources_fq

    return new_fq
def make_fq(types, tags, fq, mapper_url, mapper_inst, mapper_key, report_type_query, cohort_ids, job_results_filter,
            sources):
    new_fq = fq

    subjects = list()
    documents = list()

    if sources and len(sources) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        sources_fq = util.solr_source_field + ': ("' + '" OR "'.join(sources) + '")'
        new_fq += sources_fq

    if types and len(types) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_type_fq = util.solr_report_type_field + ': ("' + '" OR "'.join(types) + '")'
        new_fq += report_type_fq

    if len(report_type_query) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        report_types = util.solr_report_type_field  + ': (' + report_type_query + ')'
        new_fq += report_types

    if job_results_filter:
        for k in job_results_filter.keys():
            job_filter = job_results_filter[k]
            context = job_filter.pop('context', None)
            results = phenotype_results_by_context(context, job_filter)
            if context.lower() == 'patient' or context.lower() == 'subject':
                subjects.extend(set([str(x['subject']) for x in results]))
            else:
                documents.extend(set([str(x['report_id']) for x in results]))

            del results

    if len(cohort_ids) > 0:
        for c in cohort_ids:
            patients = getCohort(c)['Patients']
            subjects.extend([str(x['subjectId']) for x in patients])
            del patients

    if len(tags) > 0:
        mapped_items = get_report_type_mappings(mapper_url, mapper_inst, mapper_key)
        if len(new_fq) > 0:
            new_fq += ' AND '
        matched_reports = list()
        for tag in tags:
            try:
                lookup_tag = normalize_tag(tag)
                if lookup_tag in mapped_items:
                    matched_reports.extend(mapped_items[lookup_tag])
            except Exception as e:
                if util.debug_mode:
                    traceback.print_exc(file=sys.stderr)
                    print("Unable to map tag %s" % tag)
        if len(matched_reports) > 0:
            match_report_clause = '" OR "'.join(matched_reports)
            report_types = util.solr_report_type_field + ': ("' + match_report_clause + '")'
            new_fq += report_types

    if len(subjects) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        subject_fq = util.solr_subject_field + ': (' + ' OR '.join(subjects) + ')'
        new_fq += subject_fq

    if len(documents) > 0:
        if len(new_fq) > 0:
            new_fq += ' AND '
        doc_fq = util.solr_report_id_field + ': (' + ' OR '.join(subjects) + ')'
        new_fq += doc_fq


    return new_fq