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