Search in sources :

Example 6 with ConceptTranslator

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

the class TranslateFunctionTest method translateCodeableConceptWithNonDefaultArguments.

@Test
void translateCodeableConceptWithNonDefaultArguments() {
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "type");
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    // The translations are
    // {
    // coding1 -> [translated1],
    // coding2 -> [translated1, translated2]
    // coding4 -> [translated2]
    // }
    // Use cases:
    // 1. [ {C2,C3,C1}, {C3}, {C4} ] -> [ [T1, T2],[], [T2]]
    // 2. [ {C3, C5}, {C3} ] -> [ [], [] ]
    // 3. [ {C2} ] -> [[T1, T2]]
    // 4. [ {C3}] -> [[]]
    // 5. [ ]-> []
    // 6. null -> null
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codeableConceptStructType()).withRow("encounter-1", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_2).addCoding(CODING_3).addCoding(CODING_1))).withRow("encounter-1", makeEid(1), rowFromCodeableConcept(new CodeableConcept(CODING_3).addCoding(CODING_5))).withRow("encounter-1", makeEid(2), rowFromCodeableConcept(new CodeableConcept(CODING_4))).withRow("encounter-2", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_3).addCoding(CODING_5))).withRow("encounter-2", makeEid(1), rowFromCodeableConcept(new CodeableConcept(CODING_3))).withRow("encounter-3", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_2))).withRow("encounter-4", makeEid(0), rowFromCodeableConcept(new CodeableConcept(CODING_3))).withRow("encounter-5", makeEid(0), null).withRow("encounter-6", null, null).buildWithStructValue();
    final ElementPath inputExpression = new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.type").singular(false).definition(definition).buildDefined();
    final ConceptTranslator returnedConceptTranslator = ConceptTranslatorBuilder.toSystem(DEST_SYSTEM_URI).put(new SimpleCoding(CODING_1), TRANSLATED_1).put(new SimpleCoding(CODING_2), TRANSLATED_1, TRANSLATED_2).put(new SimpleCoding(CODING_4), TRANSLATED_2).build();
    // Create a mock terminology client.
    when(terminologyService.translate(any(), any(), anyBoolean(), any())).thenReturn(returnedConceptTranslator);
    // Prepare the inputs to the function.
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
    final StringLiteralPath conceptMapUrlArgument = StringLiteralPath.fromString("'" + CONCEPT_MAP2_URI + "'", inputExpression);
    final BooleanLiteralPath reverseArgument = BooleanLiteralPath.fromString("true", inputExpression);
    final StringLiteralPath equivalenceArgument = StringLiteralPath.fromString("narrower,equivalent", inputExpression);
    final NamedFunctionInput translateInput = new NamedFunctionInput(parserContext, inputExpression, Arrays.asList(conceptMapUrlArgument, reverseArgument, equivalenceArgument));
    // Invoke the function.
    final FhirPath result = new TranslateFunction().invoke(translateInput);
    final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-1", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-1", makeEid(1, 0), null).withRow("encounter-1", makeEid(2, 0), rowFromCoding(TRANSLATED_2)).withRow("encounter-2", makeEid(0, 0), null).withRow("encounter-2", makeEid(1, 0), null).withRow("encounter-3", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-3", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-4", makeEid(0, 0), null).withRow("encounter-5", makeEid(0, 0), null).withRow("encounter-6", null, null).buildWithStructValue();
    // Check the result.
    assertThat(result).hasExpression("Encounter.type.translate('" + CONCEPT_MAP2_URI + "', true, 'narrower,equivalent')").isElementPath(CodingPath.class).hasFhirType(FHIRDefinedType.CODING).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
    // Verify mocks
    final Set<SimpleCoding> expectedSourceCodings = ImmutableSet.of(new SimpleCoding(CODING_1), new SimpleCoding(CODING_2), new SimpleCoding(CODING_3), new SimpleCoding(CODING_4), new SimpleCoding(CODING_5));
    final List<ConceptMapEquivalence> expectedEquivalences = Arrays.asList(ConceptMapEquivalence.NARROWER, ConceptMapEquivalence.EQUIVALENT);
    verify(terminologyService).translate(eq(expectedSourceCodings), eq(CONCEPT_MAP2_URI), eq(true), eq(expectedEquivalences));
    verifyNoMoreInteractions(terminologyService);
}
Also used : StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) ElementPath(au.csiro.pathling.fhirpath.element.ElementPath) SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) ConceptMapEquivalence(org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) BooleanLiteralPath(au.csiro.pathling.fhirpath.literal.BooleanLiteralPath) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) SparkHelpers.rowFromCodeableConcept(au.csiro.pathling.test.helpers.SparkHelpers.rowFromCodeableConcept) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with ConceptTranslator

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

the class TranslateFunctionTest method translateCodingWithDefaultArguments.

@Test
void translateCodingWithDefaultArguments() {
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "class");
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    // The translations are
    // {
    // coding1 -> [translated1],
    // coding2 -> [translated1, translated2]
    // }
    // Use cases:
    // 1. [ C2,C3,C1 ] -> [ [T1, T2],[], [T1]]
    // 2. [ C3, C5 ] -> [[],[]]
    // 3. [ C2 ] -> [ [T1, T2] ]
    // 4. [] -> []
    // 5. null -> null
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0), rowFromCoding(CODING_2)).withRow("encounter-1", makeEid(1), rowFromCoding(CODING_3)).withRow("encounter-1", makeEid(2), rowFromCoding(CODING_1)).withRow("encounter-2", makeEid(0), rowFromCoding(CODING_3)).withRow("encounter-2", makeEid(1), rowFromCoding(CODING_5)).withRow("encounter-3", makeEid(0), rowFromCoding(CODING_2)).withRow("encounter-4", makeEid(0), null).withRow("encounter-5", null, null).buildWithStructValue();
    final CodingPath inputExpression = (CodingPath) new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.class").singular(false).definition(definition).buildDefined();
    final ConceptTranslator returnedConceptTranslator = ConceptTranslatorBuilder.toSystem(DEST_SYSTEM_URI).put(new SimpleCoding(CODING_1), TRANSLATED_1).put(new SimpleCoding(CODING_2), TRANSLATED_1, TRANSLATED_2).build();
    // Create a mock terminology client.
    when(terminologyService.translate(any(), any(), anyBoolean(), any())).thenReturn(returnedConceptTranslator);
    // Prepare the inputs to the function.
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
    final StringLiteralPath conceptMapUrlArgument = StringLiteralPath.fromString("'" + CONCEPT_MAP1_URI + "'", inputExpression);
    final NamedFunctionInput translateInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(conceptMapUrlArgument));
    // Invoke the function.
    final FhirPath result = new TranslateFunction().invoke(translateInput);
    final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-1", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-1", makeEid(1, 0), null).withRow("encounter-1", makeEid(2, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-2", makeEid(0, 0), null).withRow("encounter-2", makeEid(1, 0), null).withRow("encounter-3", makeEid(0, 0), rowFromCoding(TRANSLATED_1)).withRow("encounter-3", makeEid(0, 1), rowFromCoding(TRANSLATED_2)).withRow("encounter-4", makeEid(0, 0), null).withRow("encounter-5", null, null).buildWithStructValue();
    // Check the result.
    assertThat(result).hasExpression("Encounter.class.translate('" + CONCEPT_MAP1_URI + "')").isElementPath(CodingPath.class).hasFhirType(FHIRDefinedType.CODING).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
    // Verify mocks
    final Set<SimpleCoding> expectedSourceCodings = ImmutableSet.of(new SimpleCoding(CODING_1), new SimpleCoding(CODING_2), new SimpleCoding(CODING_3), new SimpleCoding(CODING_5));
    final List<ConceptMapEquivalence> expectedEquivalences = Collections.singletonList(ConceptMapEquivalence.EQUIVALENT);
    verify(terminologyService).translate(eq(expectedSourceCodings), eq(CONCEPT_MAP1_URI), eq(false), eq(expectedEquivalences));
    verifyNoMoreInteractions(terminologyService);
}
Also used : CodingPath(au.csiro.pathling.fhirpath.element.CodingPath) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) ConceptMapEquivalence(org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with ConceptTranslator

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

the class ParserTest method testTranslateWithWhereAndTranslate.

@Test
void testTranslateWithWhereAndTranslate() {
    final ConceptTranslator conceptTranslator1 = ConceptTranslatorBuilder.toSystem("uuid:test-system").putTimes(new SimpleCoding("http://snomed.info/sct", "195662009"), 3).putTimes(new SimpleCoding("http://snomed.info/sct", "444814009"), 2).build();
    final ConceptTranslator conceptTranslator2 = ConceptTranslatorBuilder.toSystem("uuid:other-system").putTimes(new SimpleCoding("uuid:test-system", "444814009-0"), 1).putTimes(new SimpleCoding("uuid:test-system", "444814009-1"), 2).build();
    // Create a mock terminology client.
    when(terminologyService.translate(any(), eq("uuid:cm=1"), anyBoolean(), any())).thenReturn(conceptTranslator1);
    when(terminologyService.translate(any(), eq("uuid:cm=2"), anyBoolean(), any())).thenReturn(conceptTranslator2);
    assertThatResultOf(ResourceType.CONDITION, "code.translate('uuid:cm=1', false, 'equivalent').where($this.translate('uuid:cm=2', false, 'equivalent').code.count()=13).code").selectOrderedResult().hasRows(spark, "responses/ParserTest/testTranslateWithWhereAndTranslate.csv");
}
Also used : SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Test(org.junit.jupiter.api.Test)

Example 9 with ConceptTranslator

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

the class ParserTest method testTranslateFunction.

@Test
void testTranslateFunction() {
    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').code").selectOrderedResult().hasRows(spark, "responses/ParserTest/testTranslateFunction.csv");
}
Also used : SimpleCoding(au.csiro.pathling.fhirpath.encoding.SimpleCoding) ConceptTranslator(au.csiro.pathling.terminology.ConceptTranslator) Test(org.junit.jupiter.api.Test)

Example 10 with ConceptTranslator

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

the class TerminologyServiceIntegrationTest method testCorrectlyTranslatesKnownAndUnknownCodes.

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

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