use of com.cerner.bunsen.spark.codes.Mapping in project bunsen by cerner.
the class AbstractConceptMaps method getMappings.
/**
* Returns a dataset with the mappings for each uri and version.
*
* @param uriToVersion a map of concept map URI to the version to load
* @return a dataset of mappings for the given URIs and versions.
*/
public Dataset<Mapping> getMappings(Map<String, String> uriToVersion) {
JavaSparkContext context = new JavaSparkContext(this.spark.sparkContext());
Broadcast<Map<String, String>> broadcastMaps = context.broadcast(uriToVersion);
return this.mappings.filter((FilterFunction<Mapping>) mapping -> {
String latestVersion = broadcastMaps.getValue().get(mapping.getConceptMapUri());
return latestVersion != null && latestVersion.equals(mapping.getConceptMapVersion());
});
}
use of com.cerner.bunsen.spark.codes.Mapping in project bunsen by cerner.
the class ConceptMaps method addToConceptMap.
@Override
protected void addToConceptMap(ConceptMap map, Dataset<Mapping> mappings) {
// Sort the items so they are grouped together optimally, and so
// we consistently produce the same ordering, therefore making
// inspection and comparison of the concept maps easier.
List<Mapping> sortedMappings = mappings.sort("sourceSystem", "targetSystem", "sourceValue", "targetValue").collectAsList();
ConceptMapGroupComponent currentGroup = null;
SourceElementComponent element = null;
// Workaround for the decoder producing an immutable array by
// replacing it with a mutable one.
map.setGroup(new ArrayList<>(map.getGroup()));
for (Mapping mapping : sortedMappings) {
// Add a new group if we don't match the previous one.
if (currentGroup == null || !mapping.getSourceSystem().equals(currentGroup.getSource()) || !mapping.getTargetSystem().equals(currentGroup.getTarget())) {
currentGroup = null;
// Find a matching group.
for (ConceptMapGroupComponent candidate : map.getGroup()) {
if (mapping.getSourceSystem().equals(candidate.getSource()) && mapping.getTargetSystem().equals(candidate.getTarget())) {
currentGroup = candidate;
// Workaround for the decoder producing an immutable array by
// replacing it with a mutable one.
currentGroup.setElement(new ArrayList<>(currentGroup.getElement()));
break;
}
}
// No matching group found, so add it.
if (currentGroup == null) {
currentGroup = map.addGroup();
currentGroup.setSource(mapping.getSourceSystem());
currentGroup.setTarget(mapping.getTargetSystem());
// Ensure a new element is created for the newly created group.
element = null;
}
}
// so add one if it does not match the previous.
if (element == null || !mapping.getSourceValue().equals(element.getCode())) {
element = currentGroup.addElement();
element.setCode(mapping.getSourceValue());
}
element.addTarget().setCode(mapping.getTargetValue());
}
}
use of com.cerner.bunsen.spark.codes.Mapping in project bunsen by cerner.
the class ConceptMapsTest method testExpandMappings.
@Test
public void testExpandMappings() {
ConceptMap conceptMap = ConceptMaps.getEmpty(spark).withConceptMaps(conceptMap("urn:cerner:conceptmap:map", "1")).getConceptMap("urn:cerner:conceptmap:map", "1");
List<Mapping> mappings = ConceptMaps.expandMappings(conceptMap);
Mapping expectedValue = new Mapping("urn:cerner:conceptmap:map", "1", "urn:source:valueset", "urn:target:valueset", "urn:source:system", "urn:source:code:a", "urn:target:system", "urn:target:code:1", Mapping.EQUIVALENT);
Assert.assertEquals(1, mappings.size());
Assert.assertEquals(expectedValue, mappings.get(0));
}
use of com.cerner.bunsen.spark.codes.Mapping in project bunsen by cerner.
the class ConceptMaps method expandMappingsIterator.
private static Iterator<Mapping> expandMappingsIterator(ConceptMap map) {
List<Mapping> mappings = new ArrayList<>();
for (ConceptMapGroupComponent group : map.getGroup()) {
for (SourceElementComponent element : group.getElement()) {
for (TargetElementComponent target : element.getTarget()) {
Mapping mapping = new Mapping();
mapping.setConceptMapUri(map.getUrl());
mapping.setConceptMapVersion(map.getVersion());
try {
String sourceValue = map.getSource() instanceof UriType ? map.getSourceUriType().getValue() : map.getSourceReference().getReference();
mapping.setSourceValueSet(sourceValue);
String targetValue = map.getTarget() instanceof UriType ? map.getTargetUriType().getValue() : map.getTargetReference().getReference();
mapping.setTargetValueSet(targetValue);
} catch (FHIRException fhirException) {
// an exception.
throw new RuntimeException(fhirException);
}
mapping.setSourceSystem(group.getSource());
mapping.setSourceValue(element.getCode());
mapping.setTargetSystem(group.getTarget());
mapping.setTargetValue(target.getCode());
if (target.getEquivalence() != null) {
mapping.setEquivalence(target.getEquivalence().toCode());
}
mappings.add(mapping);
}
}
}
return mappings.iterator();
}
use of com.cerner.bunsen.spark.codes.Mapping in project bunsen by cerner.
the class ConceptMaps method broadcast.
@Override
public Broadcast<BroadcastableMappings> broadcast(Map<String, String> conceptMapUriToVersion) {
List<ConceptMap> mapsList = getMaps().collectAsList().stream().map(row -> (ConceptMap) conceptMapRowConverter.rowToResource(row)).collect(Collectors.toList());
Map<String, ConceptMap> mapsToLoad = mapsList.stream().filter(conceptMap -> conceptMap.getVersion().equals(conceptMapUriToVersion.get(conceptMap.getUrl()))).collect(Collectors.toMap(ConceptMap::getUrl, java.util.function.Function.identity()));
// Expand the concept maps to load and sort them so dependencies are before
// their dependents in the list.
List<String> sortedMapsToLoad = sortMapsToLoad(conceptMapUriToVersion.keySet(), mapsToLoad);
// Since this is used to map from one system to another, we use only targets
// that don't introduce inaccurate meanings. (For instance, we can't map
// general condition code to a more specific type, since that is not
// representative of the source data.)
Dataset<Mapping> mappings = getMappings(conceptMapUriToVersion).filter("equivalence in ('equivalent', 'equals', 'wider', 'subsumes')");
// Group mappings by their concept map URI
Map<String, List<Mapping>> groupedMappings = mappings.collectAsList().stream().collect(Collectors.groupingBy(Mapping::getConceptMapUri));
Map<String, BroadcastableConceptMap> broadcastableMaps = new HashMap<>();
for (String conceptMapUri : sortedMapsToLoad) {
ConceptMap map = mapsToLoad.get(conceptMapUri);
Set<String> children = getMapChildren(map);
List<BroadcastableConceptMap> childMaps = children.stream().map(child -> broadcastableMaps.get(child)).collect(Collectors.toList());
BroadcastableConceptMap broadcastableConceptMap = new BroadcastableConceptMap(conceptMapUri, groupedMappings.getOrDefault(conceptMapUri, Collections.emptyList()), childMaps);
broadcastableMaps.put(conceptMapUri, broadcastableConceptMap);
}
JavaSparkContext ctx = new JavaSparkContext(getMaps().sparkSession().sparkContext());
return ctx.broadcast(new BroadcastableMappings(broadcastableMaps));
}
Aggregations