Search in sources :

Example 1 with ConceptTranslator

use of au.csiro.pathling.terminology.ConceptTranslator in project pathling by aehrc.

the class TranslateFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    validateInput(input);
    final ElementPath inputPath = (ElementPath) input.getInput();
    final ParserContext inputContext = input.getContext();
    final Column idColumn = inputPath.getIdColumn();
    final Column conceptColumn = inputPath.getValueColumn();
    final boolean isCodeableConcept = isCodeableConcept(inputPath);
    final Column codingArrayCol = isCodeableConcept ? conceptColumn.getField("coding") : when(conceptColumn.isNotNull(), array(conceptColumn)).otherwise(lit(null));
    // The definition of the result is always the Coding element.
    @SuppressWarnings("OptionalGetWithoutIsPresent") final ElementDefinition resultDefinition = isCodeableConcept ? inputPath.getChildElement("coding").get() : inputPath.getDefinition().get();
    // Prepare the data which will be used within the map operation. All of these things must be
    // Serializable.
    @SuppressWarnings("OptionalGetWithoutIsPresent") final TerminologyServiceFactory terminologyServiceFactory = inputContext.getTerminologyServiceFactory().get();
    final Arguments arguments = Arguments.of(input);
    final String conceptMapUrl = arguments.getValue(0, String.class);
    final boolean reverse = arguments.getValueOr(1, DEFAULT_REVERSE);
    final String equivalence = arguments.getValueOr(2, DEFAULT_EQUIVALENCE);
    final Dataset<Row> dataset = inputPath.getDataset();
    final MapperWithPreview<List<SimpleCoding>, Row[], ConceptTranslator> mapper = new TranslateMapperWithPreview(MDC.get("requestId"), terminologyServiceFactory, conceptMapUrl, reverse, Strings.parseCsvList(equivalence, wrapInUserInputError(ConceptMapEquivalence::fromCode)));
    final Dataset<Row> translatedDataset = SqlExtensions.mapWithPartitionPreview(dataset, codingArrayCol, SimpleCodingsDecoders::decodeList, mapper, StructField.apply("result", DataTypes.createArrayType(CodingEncoding.DATA_TYPE), true, Metadata.empty()));
    // The result is an array of translations per each input element, which we now
    // need to explode in the same way as for path traversal, creating unique element ids.
    final MutablePair<Column, Column> valueAndEidColumns = new MutablePair<>();
    final Dataset<Row> resultDataset = inputPath.explodeArray(translatedDataset, translatedDataset.col("result"), valueAndEidColumns);
    // Construct a new result expression.
    final String expression = expressionFromInput(input, NAME);
    return ElementPath.build(expression, resultDataset, idColumn, Optional.of(valueAndEidColumns.getRight()), valueAndEidColumns.getLeft(), false, inputPath.getCurrentResource(), inputPath.getThisColumn(), resultDefinition);
}
Also used : TerminologyServiceFactory(au.csiro.pathling.fhir.TerminologyServiceFactory) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) Column(org.apache.spark.sql.Column) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) SimpleCodingsDecoders(au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders) List(java.util.List) ConceptMapEquivalence(org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) Nonnull(javax.annotation.Nonnull)

Example 2 with ConceptTranslator

use of au.csiro.pathling.terminology.ConceptTranslator in project pathling by aehrc.

the class TranslateMapperWithPreview method preview.

@Override
@Nonnull
public ConceptTranslator preview(@Nonnull final Iterator<List<SimpleCoding>> input) {
    if (!input.hasNext() || equivalences.isEmpty()) {
        return new ConceptTranslator();
    }
    // Add the request ID to the logging context, so that we can track the logging for this
    // request across all workers.
    MDC.put("requestId", requestId);
    final Set<SimpleCoding> uniqueCodings = Streams.streamOf(input).filter(Objects::nonNull).flatMap(List::stream).collect(Collectors.toSet());
    final TerminologyService terminologyService = terminologyServiceFactory.buildService(log);
    return terminologyService.translate(uniqueCodings, conceptMapUrl, reverse, equivalences);
}
Also used : SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) TerminologyService(au.csiro.pathling.terminology.TerminologyService) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Objects(java.util.Objects) Nonnull(javax.annotation.Nonnull)

Example 3 with ConceptTranslator

use of au.csiro.pathling.terminology.ConceptTranslator in project pathling by aehrc.

the class ParserTest method testExtensionFunctionOnTranslateResult.

@Test
void testExtensionFunctionOnTranslateResult() {
    // This is a special case as the codings here are created from the terminology server response
    // using the hardcoded encoding core in CodingEncoding.
    final ConceptTranslator returnedConceptTranslator = ConceptTranslatorBuilder.toSystem("uuid:test-system").putTimes(new SimpleCoding("http://snomed.info/sct", "195662009"), 3).putTimes(new SimpleCoding("http://snomed.info/sct", "444814009"), 2).build();
    // Create a mock terminology client.
    when(terminologyService.translate(any(), any(), anyBoolean(), any())).thenReturn(returnedConceptTranslator);
    assertThatResultOf(ResourceType.CONDITION, "code.coding.translate('http://snomed.info/sct?fhir_cm=900000000000526001', false, 'equivalent').extension('uuid:any').url").selectOrderedResult().hasRows(spark, "responses/ParserTest/testExtensionFunctionOnTranslateResult.csv");
}
Also used : SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Test(org.junit.jupiter.api.Test)

Example 4 with ConceptTranslator

use of au.csiro.pathling.terminology.ConceptTranslator in project pathling by aehrc.

the class TerminologyServiceIntegrationTest method testCorrectlyTranslatesInReverse.

@Test
void testCorrectlyTranslatesInReverse() {
    final ConceptTranslator actualTranslation = terminologyService.translate(Arrays.asList(simpleOf(CD_SNOMED_720471000168102), snomedSimple("444814009")), CM_HIST_ASSOCIATIONS, true, ALL_EQUIVALENCES);
    final ConceptTranslator expectedTranslation = ConceptTranslatorBuilder.empty().put(CD_SNOMED_720471000168102, CD_SNOMED_72940011000036107).build();
    assertEquals(expectedTranslation, actualTranslation);
}
Also used : ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Test(org.junit.jupiter.api.Test)

Example 5 with ConceptTranslator

use of au.csiro.pathling.terminology.ConceptTranslator in project pathling by aehrc.

the class TerminologyServiceIntegrationTest method testIgnoresUnknownSystems.

// TODO: Enable when fixed in terminology server, that is it does not accept ignore systems in
// codings.
@Test
@Disabled
void testIgnoresUnknownSystems() {
    final ConceptTranslator actualTranslation = terminologyService.translate(Arrays.asList(testSimple("72940011000036107"), testSimple("444814009")), CM_HIST_ASSOCIATIONS, false, ALL_EQUIVALENCES);
    final ConceptTranslator expectedTranslation = ConceptTranslatorBuilder.empty().build();
    assertEquals(expectedTranslation, actualTranslation);
}
Also used : ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

ConceptTranslator (au.csiro.pathling.terminology.ConceptTranslator)10 Test (org.junit.jupiter.api.Test)8 SimpleCoding (au.csiro.pathling.fhirpath.encoding.SimpleCoding)6 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)3 ParserContext (au.csiro.pathling.fhirpath.parser.ParserContext)3 Row (org.apache.spark.sql.Row)3 ConceptMapEquivalence (org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence)3 FhirPath (au.csiro.pathling.fhirpath.FhirPath)2 ElementPath (au.csiro.pathling.fhirpath.element.ElementPath)2 NamedFunctionInput (au.csiro.pathling.fhirpath.function.NamedFunctionInput)2 StringLiteralPath (au.csiro.pathling.fhirpath.literal.StringLiteralPath)2 DatasetBuilder (au.csiro.pathling.test.builders.DatasetBuilder)2 ElementPathBuilder (au.csiro.pathling.test.builders.ElementPathBuilder)2 ParserContextBuilder (au.csiro.pathling.test.builders.ParserContextBuilder)2 Nonnull (javax.annotation.Nonnull)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TerminologyServiceFactory (au.csiro.pathling.fhir.TerminologyServiceFactory)1 CodingPath (au.csiro.pathling.fhirpath.element.CodingPath)1 SimpleCodingsDecoders (au.csiro.pathling.fhirpath.encoding.SimpleCodingsDecoders)1 BooleanLiteralPath (au.csiro.pathling.fhirpath.literal.BooleanLiteralPath)1