Skip to content

Commit

Permalink
Merge pull request #123 from EHDEN/fix-read-maptype
Browse files Browse the repository at this point in the history
Fix compatibility and apply previous issues
  • Loading branch information
Maxim Moinat authored Apr 7, 2021
2 parents 7f44cc7 + a3ad8e9 commit 4192b88
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/org/ohdsi/usagi/Concept.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

import java.util.Objects;

/**
* Class for holding information about a single (target) concept in the Vocabulary
*/
Expand Down Expand Up @@ -77,4 +79,18 @@ public static Concept createEmptyConcept() {

public Concept() {
}

@Override
public boolean equals(Object o) {
// Only compare conceptId
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Concept concept = (Concept) o;
return conceptId == concept.conceptId;
}

@Override
public int hashCode() {
return Objects.hash(conceptId);
}
}
28 changes: 27 additions & 1 deletion src/org/ohdsi/usagi/MappingTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
******************************************************************************/
package org.ohdsi.usagi;

import java.util.Objects;

/**
* Class for holding information about a single (target) concept in the Vocabulary
*/
public class MappingTarget{
public enum Type {
MAPS_TO, MAPS_TO_VALUE, MAPS_TO_UNIT, MAPS_TO_OPERATOR, MAPS_TO_TYPE, MAPS_TO_NUMBER
MAPS_TO, MAPS_TO_VALUE, MAPS_TO_UNIT, MAPS_TO_OPERATOR, MAPS_TO_TYPE, MAPS_TO_NUMBER;

public static Type valueOfCompat(String value) {
// For backwards compatibility with old types
switch (value) {
case "EVENT": return MAPS_TO;
case "VALUE": return MAPS_TO_VALUE;
case "UNIT": return MAPS_TO_UNIT;
default: return valueOf(value);
}
}
}

private final Concept concept;
Expand Down Expand Up @@ -72,4 +84,18 @@ public Type getMappingType() {
public void setMappingType(Type mappingType) {
this.mappingType = mappingType;
}

@Override
public boolean equals(Object o) {
// Only compares target concept and mappingType.
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MappingTarget that = (MappingTarget) o;
return Objects.equals(concept, that.concept) && mappingType == that.mappingType;
}

@Override
public int hashCode() {
return Objects.hash(concept, mappingType);
}
}
7 changes: 1 addition & 6 deletions src/org/ohdsi/usagi/ReadCodeMappingsFromFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,9 @@ && new SourceCode(row).sourceName.equals(buffer.getSourceCode().sourceName)) {
buffer.setMappingStatus(MappingStatus.INVALID_TARGET);
buffer.setComment("Invalid existing target: " + row.get("conceptId"));
} else {
// Type and provenance might not be available in older Usagi files
MappingTarget mappingTarget = new MappingTarget(
concept,
MappingTarget.Type.valueOf(row.get("mappingType", "MAPS_TO")
.replace("EVENT", "MAPS_TO")
.replace("VALUE", "MAPS_TO_VALUE")
.replace("UNIT", "MAPS_TO_UNIT")
),
MappingTarget.Type.valueOfCompat(row.get("mappingType", "MAPS_TO")),
row.get("createdBy", ""),
row.getLong("createdOn", "0")
);
Expand Down
16 changes: 12 additions & 4 deletions src/org/ohdsi/usagi/ui/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JOptionPane;

import org.ohdsi.usagi.CodeMapping;
import org.ohdsi.usagi.ReadCodeMappingsFromFile;
import org.ohdsi.usagi.SourceCode;
import org.ohdsi.usagi.WriteCodeMappingsToFile;
import org.ohdsi.utilities.collections.Pair;

import static org.ohdsi.usagi.ui.DataChangeEvent.*;

Expand Down Expand Up @@ -83,9 +85,15 @@ public void saveToFile(String filename) {
}

public List<SourceCode> getSourceCodes() {
List<SourceCode> sourceCodes = new ArrayList<SourceCode>(size());
for (CodeMapping codeMapping : this)
sourceCodes.add(codeMapping.getSourceCode());
return sourceCodes;
return this.stream()
.map(CodeMapping::getSourceCode)
.collect(Collectors.toList());
}

public List<String> getAdditionalColumnNames() {
CodeMapping codeMapping = Global.mapping.get(0);
return codeMapping.getSourceCode().sourceAdditionalInfo.stream()
.map(Pair::getItem1)
.collect(Collectors.toList());
}
}
6 changes: 3 additions & 3 deletions src/org/ohdsi/usagi/ui/MappingTablePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ public void restructure() {
columnNames = defaultColumnNames;
addInfoColCount = 0;
if (Global.mapping.size() != 0) {
CodeMapping codeMapping = Global.mapping.get(0);
addInfoColCount = codeMapping.getSourceCode().sourceAdditionalInfo.size();
List<String> additionalColumns = Global.mapping.getAdditionalColumnNames();
addInfoColCount = additionalColumns.size();
columnNames = new String[defaultColumnNames.length + addInfoColCount];
for (int i = 0; i < ADD_INFO_START_COL; i++)
columnNames[i] = defaultColumnNames[i];

for (int i = 0; i < addInfoColCount; i++)
columnNames[i + ADD_INFO_START_COL] = codeMapping.getSourceCode().sourceAdditionalInfo.get(i).getItem1();
columnNames[i + ADD_INFO_START_COL] = additionalColumns.get(i);

for (int i = ADD_INFO_START_COL; i < defaultColumnNames.length; i++)
columnNames[i + addInfoColCount] = defaultColumnNames[i];
Expand Down
58 changes: 56 additions & 2 deletions src/org/ohdsi/usagi/ui/actions/ApplyPreviousMappingAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.AbstractAction;
import javax.swing.Action;
Expand All @@ -30,6 +33,7 @@
import org.ohdsi.usagi.CodeMapping;
import org.ohdsi.usagi.ui.Global;
import org.ohdsi.usagi.ui.Mapping;
import org.ohdsi.utilities.collections.Pair;

import static org.ohdsi.usagi.ui.DataChangeEvent.*;

Expand Down Expand Up @@ -62,10 +66,15 @@ public void actionPerformed(ActionEvent arg0) {
Mapping mappingToBeApplied = new Mapping();
mappingToBeApplied.loadFromFile(file.getAbsolutePath());

// Additional columns
List<String> existingAdditionalColumnNames = Global.mapping.getAdditionalColumnNames();

// Apply mapping. Add mappings not currently present
ApplyPreviousChangeSummary summary = new ApplyPreviousChangeSummary();
for (CodeMapping codeMappingToBeApplied : mappingToBeApplied) {
CodeMapping existingMapping = codeToMapping.get(codeMappingToBeApplied.getSourceCode().sourceCode);
if (existingMapping != null) {
summary.compare(existingMapping, codeMappingToBeApplied);
existingMapping.getSourceCode().sourceName = codeMappingToBeApplied.getSourceCode().sourceName;
existingMapping.setTargetConcepts(codeMappingToBeApplied.getTargetConcepts());
existingMapping.setMappingStatus(codeMappingToBeApplied.getMappingStatus());
Expand All @@ -76,13 +85,17 @@ public void actionPerformed(ActionEvent arg0) {
existingMapping.setStatusSetOn(codeMappingToBeApplied.getStatusSetOn());
mappingsApplied++;
} else {
// Add empty additional columns if these existed in existing mapping
codeMappingToBeApplied.getSourceCode().sourceAdditionalInfo = existingAdditionalColumnNames.stream()
.map(x -> new Pair<>(x, ""))
.collect(Collectors.toList());
Global.mapping.add(codeMappingToBeApplied);
mappingsAdded++;
}
}

String message = "The applied mapping contained " + mappingToBeApplied.size() + " mappings of which " + mappingsApplied
+ " were applied to the current mapping and " + mappingsAdded + " were newly added.";
+ " were applied to the current mapping and " + mappingsAdded + " were newly added.\n\n" + summary.createReport();
Global.mappingTablePanel.updateUI();
Global.mappingDetailPanel.updateUI();
Global.mapping.fireDataChanged(APPROVE_EVENT); // To update the footer
Expand All @@ -91,7 +104,48 @@ public void actionPerformed(ActionEvent arg0) {
Global.usagiSearchEngine.createDerivedIndex(Global.mapping.getSourceCodes(), Global.frame);
Global.mappingDetailPanel.doSearch();
}
JOptionPane.showMessageDialog(Global.frame, message);
JOptionPane.showMessageDialog(Global.frame, message, "Summary", JOptionPane.INFORMATION_MESSAGE);
}
}

private static class ApplyPreviousChangeSummary {
private int nChanged = 0;
private int nSourceNameChanged = 0;
private int nTargetConceptsChanged = 0;
private int nMappingStatusChanged = 0;
private int nEquivalenceChanged = 0;

private void compare(CodeMapping A, CodeMapping B) {
boolean hasChanged = false;
if (!A.getSourceCode().sourceName.equals(B.getSourceCode().sourceName)) {
nSourceNameChanged++;
hasChanged = true;
}
if (!A.getTargetConcepts().equals(B.getTargetConcepts())) {
nTargetConceptsChanged++; // This could be target concept, size OR type
hasChanged = true;
}
if (!A.getMappingStatus().equals(B.getMappingStatus())) {
nMappingStatusChanged++;
hasChanged = true;
}
if (!A.getEquivalence().equals(B.getEquivalence())) {
nEquivalenceChanged++;
hasChanged = true;
}
if (hasChanged) {
nChanged++;
}
}

private String createReport() {
StringBuilder report = new StringBuilder();
report.append("Of the applied mappings, " + nChanged + " mappings changed.");
report.append("\n\tSource name: " + nSourceNameChanged);
report.append("\n\tTarget concept: " + nTargetConceptsChanged + "\t(changed target concept, target type AND/OR number of targets)");
report.append("\n\tMapping status: " + nMappingStatusChanged);
report.append("\n\tMapping equivalence: " + nEquivalenceChanged);
return report.toString();
}
}
}

0 comments on commit 4192b88

Please sign in to comment.