Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ALS-5787] Open Access StatViz is not showing "Other" #180

Merged
merged 10 commits into from
Feb 13, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package edu.harvard.hms.dbmi.avillach.resource.visualization.service;

import edu.harvard.dbmi.avillach.util.VisualizationUtil;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;

import java.util.HashMap;
import java.util.Map;

public class VisualizationUtilTests {

@Test
@DisplayName("Test limitKeySize")
public void testLimitKeySizeUniqueness() {
// Testing name uniqueness
Map<String, Integer> axisMap = new HashMap<>();
axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1);
axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1);
axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1);
axisMap.put("Disease-Specific (Asthma, Allergy and Inflammation)", 1);

Map<String, Integer> stringIntegerMap = VisualizationUtil.limitKeySize(axisMap);
assert (stringIntegerMap.size() == 4);
}
Gcolon021 marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,62 @@ public static Map<String, Integer> doProcessResults(Map<String, Integer> axisMap
}

/**
* Replaces long column names with shorter version.
* This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer
* than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...".
* If the shortened key is not unique, we will create a unique one
* <p>
*
* @param axisMap
* @return
* @param axisMap - Map of the categories and their counts
* @return Map<String, Integer> - Map of the categories and their counts with the keys limited to 45 characters
*/
private static Map<String, Integer> limitKeySize(Map<String, Integer> axisMap) {
List<String> toRemove = new ArrayList<>();
Map<String, Integer> toAdd = new HashMap<>();
axisMap.keySet().forEach(key -> {
if (key.length() > MAX_X_LABEL_LINE_LENGTH) {
toRemove.add(key);
toAdd.put(
key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "...",
axisMap.get(key));
public static Map<String, Integer> limitKeySize(Map<String, Integer> axisMap) {
Map<String, Integer> newAxisMap = new HashMap<>();
HashSet<String> keys = new HashSet<>();

axisMap.forEach((key, value) -> {
if (key.length() < MAX_X_LABEL_LINE_LENGTH) {
newAxisMap.put(key, value);
keys.add(key);
} else {
// proposed key
StringBuilder proposedKey;

// Check if the key exists in the keys set.
boolean hasEqual = false;
// Let first check if the proposed key is a leading substring of another key
for (String innerKey : axisMap.keySet()) {// Check if this key will be equal to any future keys.
if (innerKey.length() >= MAX_X_LABEL_LINE_LENGTH) {
hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH));
if (hasEqual) {
break;
}
}
}

if (hasEqual) { // if we have an equal we want to create a more unique key
int countFromEnd = 6;
String uniqueness;
do {
// get the original key substring max x + 3 (for the dots) + countFromEnd.
Gcolon021 marked this conversation as resolved.
Show resolved Hide resolved
proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd));

// Get the last "countFromEnd" characters
uniqueness = key.substring(key.length() - countFromEnd);

proposedKey.append("...").append(uniqueness);
countFromEnd++; // increase the number of characters from the end of the string we will use
} while (keys.contains(proposedKey.toString()));

} else {
proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "...");
}

newAxisMap.put(proposedKey.toString(), value);
keys.add(proposedKey.toString());
}
});
toRemove.forEach(key -> axisMap.remove(key));
axisMap.putAll(toAdd);
return axisMap;

return newAxisMap;
}

}
Loading