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;
}
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());
}
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;
}
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();
}
}
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;
}
Aggregations