Search in sources :

Example 1 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class CohortEngineRestHandler method evaluateCohort.

private List<String> evaluateCohort(IGenericClient dataClient, CqlTerminologyProvider termProvider, CqlLibraryProvider libraryProvider, RetrieveCacheContext retrieveCacheContext, CohortEvaluation evaluationRequest) {
    CqlDataProvider dataProvider = R4DataProviderFactory.createDataProvider(dataClient, termProvider, retrieveCacheContext);
    CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setCacheContexts(false);
    Set<String> expressions = new HashSet<>();
    expressions.add(evaluationRequest.getDefineToRun());
    String[] patients = evaluationRequest.getPatientIds().split(",");
    CqlLibraryDescriptor topLevelLibrary = CqlLibraryHelpers.filenameToLibraryDescriptor(evaluationRequest.getEntrypoint());
    List<String> retVal = new ArrayList<>();
    for (String patientId : patients) {
        Pair<String, String> contextPair = new ImmutablePair<>(ContextNames.PATIENT, patientId);
        CqlEvaluationResult result = evaluator.evaluate(topLevelLibrary.getVersionedIdentifier(), evaluationRequest.getParameters(), contextPair, expressions, evaluationRequest.getLoggingLevel(), ZonedDateTime.now());
        for (Map.Entry<String, Object> entry : result.getExpressionResults().entrySet()) {
            String expression = entry.getKey();
            Object rawValue = entry.getValue();
            if (rawValue == null) {
                throw new RuntimeException(String.format("Null result is unsupported! Expression: \"%s\", ContextId: %s", expression, patientId));
            }
            if (!(rawValue instanceof Boolean)) {
                throw new RuntimeException(String.format("Only boolean CQLs are currently supported! Expression: \"%s\", Result: %s, ContextId: %s", expression, result, patientId));
            }
            Boolean booleanResult = (Boolean) rawValue;
            if (booleanResult) {
                retVal.add(patientId);
            }
            logger.info(String.format("Expression: \"%s\", Result: %s, ContextId: %s", expression, booleanResult, patientId));
        }
    }
    return retVal;
}
Also used : ArrayList(java.util.ArrayList) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Map(java.util.Map) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) HashSet(java.util.HashSet)

Example 2 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class TranslationCLI method runWithArgs.

public void runWithArgs(TranslationOptions options, PrintStream out) throws Exception {
    CqlLibraryProvider libraryProvider;
    if (options.directory != null && options.directory.exists()) {
        MapCqlLibraryProviderFactory libraryProviderFactory = new MapCqlLibraryProviderFactory();
        libraryProvider = libraryProviderFactory.fromDirectory(options.directory.toPath());
    } else {
        libraryProvider = new MapCqlLibraryProvider(Collections.emptyMap());
    }
    CqlLibraryProvider fhirClasspathProvider = new ClasspathCqlLibraryProvider();
    libraryProvider = new PriorityCqlLibraryProvider(libraryProvider, fhirClasspathProvider);
    CqlToElmTranslator translator = new CqlToElmTranslator();
    if (options.modelInfoFile != null && options.modelInfoFile.exists()) {
        translator.registerModelInfo(options.modelInfoFile);
    }
    String content;
    try (InputStream is = new FileInputStream(options.cqlPath)) {
        content = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    // The values in the descriptor are not relevant for the translation CLI.
    CqlLibraryDescriptor descriptor = new CqlLibraryDescriptor().setFormat(Format.CQL).setLibraryId("TranslationCLI").setVersion("TranslationCLI");
    CqlLibrary library = new CqlLibrary().setDescriptor(descriptor).setContent(content);
    CqlTranslationResult result = translator.translate(library, new ProviderBasedCqlLibrarySourceProvider(libraryProvider));
    out.println("Translated Library: ");
    out.println(result.getMainLibrary().getContent());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MapCqlLibraryProvider(com.ibm.cohort.cql.library.MapCqlLibraryProvider) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) MapCqlLibraryProvider(com.ibm.cohort.cql.library.MapCqlLibraryProvider) FileInputStream(java.io.FileInputStream) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) ProviderBasedCqlLibrarySourceProvider(com.ibm.cohort.cql.provider.ProviderBasedCqlLibrarySourceProvider) MapCqlLibraryProviderFactory(com.ibm.cohort.cql.library.MapCqlLibraryProviderFactory) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlTranslationResult(com.ibm.cohort.cql.translation.CqlTranslationResult)

Example 3 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class ColumnRuleCreator method getDataRequirementsForContext.

/**
 * Retrieve the merged set of data type and column filters for all CQL jobs that will
 * be evaluated for a given aggregation context.
 *
 * @param context ContextDefinition whose CQL jobs will be interrogated for data requirements
 * @return Map of data type to the fields in that datatype that are used by the CQL jobs
 */
public Map<String, Set<StringMatcher>> getDataRequirementsForContext(ContextDefinition context) {
    Map<CqlLibraryDescriptor, Set<String>> expressionsByLibrary = new HashMap<>();
    for (CqlEvaluationRequest request : requests) {
        Set<String> expressions = expressionsByLibrary.computeIfAbsent(request.getDescriptor(), desc -> new HashSet<>());
        request.getExpressions().stream().forEach(exp -> expressions.add(exp.getName()));
    }
    DataTypeRequirementsProcessor requirementsProcessor = new DataTypeRequirementsProcessor(cqlTranslator);
    Map<String, Set<StringMatcher>> pathsByDataType = new HashMap<>();
    for (Map.Entry<CqlLibraryDescriptor, Set<String>> entry : expressionsByLibrary.entrySet()) {
        LOG.debug("Extracting data requirements for {}", entry.getKey());
        DataTypeRequirementsProcessor.DataTypeRequirements requirements = requirementsProcessor.getDataRequirements(libraryProvider, entry.getKey(), entry.getValue());
        Map<String, Set<StringMatcher>> newPaths = requirements.allAsStringMatcher();
        newPaths.forEach((key, value) -> {
            pathsByDataType.merge(key, value, (prev, current) -> {
                prev.addAll(current);
                return prev;
            });
        });
    }
    Set<StringMatcher> contextFields = pathsByDataType.computeIfAbsent(context.getPrimaryDataType(), dt -> new HashSet<>());
    contextFields.add(new EqualsStringMatcher(context.getPrimaryKeyColumn()));
    if (context.getRelationships() != null) {
        for (Join join : context.getRelationships()) {
            Set<StringMatcher> joinFields = pathsByDataType.get(join.getRelatedDataType());
            if (joinFields != null) {
                joinFields.add(new EqualsStringMatcher(join.getRelatedKeyColumn()));
                joinFields.add(new EqualsStringMatcher(ContextRetriever.JOIN_CONTEXT_VALUE_IDX));
                // if the join key is not the primary key of the primary data table, then we need to add in the alternate key
                if (join.getPrimaryDataTypeColumn() != null) {
                    contextFields.add(new EqualsStringMatcher(join.getPrimaryDataTypeColumn()));
                }
                if (join instanceof ManyToMany) {
                    ManyToMany manyToMany = (ManyToMany) join;
                    Set<StringMatcher> associationFields = pathsByDataType.computeIfAbsent(manyToMany.getAssociationDataType(), dt -> new HashSet<>());
                    associationFields.add(new EqualsStringMatcher(manyToMany.getAssociationOneKeyColumn()));
                    associationFields.add(new EqualsStringMatcher(manyToMany.getAssociationManyKeyColumn()));
                }
                if (join instanceof MultiManyToMany) {
                    ManyToMany with = ((MultiManyToMany) join).getWith();
                    while (with != null) {
                        Set<StringMatcher> relatedFields = pathsByDataType.computeIfAbsent(with.getRelatedDataType(), dt -> new HashSet<>());
                        relatedFields.add(new EqualsStringMatcher(with.getRelatedKeyColumn()));
                        relatedFields.add(new EqualsStringMatcher(ContextRetriever.JOIN_CONTEXT_VALUE_IDX));
                        with = (with instanceof MultiManyToMany) ? ((MultiManyToMany) with).getWith() : null;
                    }
                }
            }
        }
    }
    pathsByDataType.values().forEach((matcherSet -> {
        matcherSet.add(new EqualsStringMatcher(ContextRetriever.SOURCE_FACT_IDX));
    }));
    return pathsByDataType;
}
Also used : EqualsStringMatcher(com.ibm.cohort.cql.util.EqualsStringMatcher) Logger(org.slf4j.Logger) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) DataTypeRequirementsProcessor(com.ibm.cohort.cql.spark.optimizer.DataTypeRequirementsProcessor) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) HashMap(java.util.HashMap) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) HashSet(java.util.HashSet) StringMatcher(com.ibm.cohort.cql.util.StringMatcher) List(java.util.List) Map(java.util.Map) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) Set(java.util.Set) HashSet(java.util.HashSet) DataTypeRequirementsProcessor(com.ibm.cohort.cql.spark.optimizer.DataTypeRequirementsProcessor) HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) EqualsStringMatcher(com.ibm.cohort.cql.util.EqualsStringMatcher) StringMatcher(com.ibm.cohort.cql.util.StringMatcher) EqualsStringMatcher(com.ibm.cohort.cql.util.EqualsStringMatcher) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class SparkCqlEvaluatorTest method testParameterMatrixOutputSimpleSuccess.

@Test
public void testParameterMatrixOutputSimpleSuccess() throws Exception {
    String outputLocation = "target/output/param-matrix/patient_cohort";
    CqlEvaluationRequest template = new CqlEvaluationRequest();
    template.setDescriptor(new CqlLibraryDescriptor().setLibraryId("SampleLibrary").setVersion("1.0.0"));
    template.setExpressionsByNames(Collections.singleton("IsFemale"));
    template.setContextKey("Patient");
    template.setContextValue("NA");
    CqlEvaluationRequests requests = new CqlEvaluationRequests();
    requests.setEvaluations(new ArrayList<>());
    List<Integer> ages = Arrays.asList(15, 17, 18);
    for (Integer age : ages) {
        Map<String, Parameter> parameters = new HashMap<>();
        parameters.put("MinimumAge", new IntegerParameter(age));
        CqlEvaluationRequest request = new CqlEvaluationRequest(template);
        request.setParameters(parameters);
        requests.getEvaluations().add(request);
    }
    ObjectMapper om = new ObjectMapper();
    File jobsFile = new File("target/output/param-matrix-simple/cql-jobs.json");
    if (!jobsFile.exists()) {
        jobsFile.getParentFile().mkdirs();
    }
    FileUtils.write(jobsFile, om.writeValueAsString(requests), StandardCharsets.UTF_8);
    try {
        String[] args = new String[] { "-d", "src/test/resources/simple-job/context-definitions.json", "-j", jobsFile.getPath(), "-m", "src/test/resources/simple-job/modelinfo/simple-modelinfo-1.0.0.xml", "-c", "src/test/resources/simple-job/cql", "-i", "Patient=" + new File("src/test/resources/simple-job/testdata/patient").toURI().toString(), "-o", "Patient=" + new File(outputLocation).toURI().toString(), "--output-format", "delta", "--overwrite-output-for-contexts", "--metadata-output-path", outputLocation };
        SparkCqlEvaluator.main(args);
        validateOutputCountsAndColumns(outputLocation, new HashSet<>(Arrays.asList("id", "parameters", "SampleLibrary|IsFemale")), 10 * ages.size(), "delta");
    } finally {
        jobsFile.delete();
    }
}
Also used : IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) HashMap(java.util.HashMap) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) DateParameter(com.ibm.cohort.cql.evaluation.parameters.DateParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) IntervalParameter(com.ibm.cohort.cql.evaluation.parameters.IntervalParameter) DecimalParameter(com.ibm.cohort.cql.evaluation.parameters.DecimalParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with CqlLibraryDescriptor

use of com.ibm.cohort.cql.library.CqlLibraryDescriptor in project quality-measure-and-cohort-service by Alvearie.

the class SparkCqlEvaluatorTest method makeEvaluationRequest.

private CqlEvaluationRequest makeEvaluationRequest(String contextName, String libraryName, String libraryVersion) {
    CqlEvaluationRequest cqlEvaluationRequest = new CqlEvaluationRequest();
    cqlEvaluationRequest.setContextKey(contextName);
    cqlEvaluationRequest.setContextValue("NA");
    CqlLibraryDescriptor descriptor = new CqlLibraryDescriptor();
    descriptor.setLibraryId(libraryName);
    descriptor.setVersion(libraryVersion);
    cqlEvaluationRequest.setDescriptor(descriptor);
    return cqlEvaluationRequest;
}
Also used : CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor)

Aggregations

CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)50 Test (org.junit.Test)34 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)19 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)18 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)17 HashMap (java.util.HashMap)17 CqlExpressionConfiguration (com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration)14 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)14 ContextDefinitions (com.ibm.cohort.cql.spark.aggregation.ContextDefinitions)10 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)9 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)8 File (java.io.File)8 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)7 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)7 DecimalParameter (com.ibm.cohort.cql.evaluation.parameters.DecimalParameter)5 StringParameter (com.ibm.cohort.cql.evaluation.parameters.StringParameter)5 Format (com.ibm.cohort.cql.library.Format)5 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)4