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