Skip to content

Commit

Permalink
Merge pull request #20 from German-BioImaging/kvpairs
Browse files Browse the repository at this point in the history
KeyValue pairs functions implementation
  • Loading branch information
ppouchin authored Apr 5, 2024
2 parents 4ce9491 + 422b7f6 commit a36b41e
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -113,6 +114,8 @@ public class OMEROMacroExtension implements PlugIn, MacroExtension {
newDescriptor("getROIs", this, ARG_NUMBER, ARG_NUMBER + ARG_OPTIONAL, ARG_STRING + ARG_OPTIONAL),
newDescriptor("saveROIs", this, ARG_NUMBER, ARG_STRING + ARG_OPTIONAL),
newDescriptor("removeROIs", this, ARG_NUMBER, ARG_STRING + ARG_OPTIONAL),
newDescriptor("getKeyValuePairs", this, ARG_STRING, ARG_NUMBER, ARG_STRING + ARG_OPTIONAL),
newDescriptor("getValue", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_STRING + ARG_OPTIONAL),
newDescriptor("sudo", this, ARG_STRING),
newDescriptor("endSudo", this),
newDescriptor("disconnect", this),
Expand Down Expand Up @@ -1279,6 +1282,77 @@ public int saveROIs(ImagePlus imp, long id, String property) {
}


/**
* Retrieves a concatenated string of all key-value pairs (keys should be unique).
*
* @param type The object type.
* @param id The object ID.
* @param separator The character(s) used to separate the items in the string (TAB by default).
*
* @return The concatenated string of all key-value pairs for the specified repository object.
*/
public String getKeyValuePairs(String type, long id, String separator) {
Map<String, String> keyValuePairs = new TreeMap<>();

String sep = separator == null ? "\t" : separator;

GenericRepositoryObjectWrapper<?> object = getRepositoryObject(type, id);
try {
if (object != null) {
keyValuePairs = new TreeMap<>(object.getKeyValuePairs(client));
}
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not retrieve object: " + e.getMessage());
}

int size = 10 * keyValuePairs.size();

StringBuilder concatenation = new StringBuilder(size);
for (Map.Entry<String, String> entry : keyValuePairs.entrySet()) {
concatenation.append(entry.getKey())
.append(sep)
.append(entry.getValue())
.append(sep);
}
if (concatenation.length() > 0) {
concatenation.setLength(concatenation.length() - sep.length());
}
return concatenation.toString();
}


/**
* Retrieves the Value associated to the given Key of a Map annotation.
* <p> If no defaultValue is provided, generates an error.
*
* @param type The object type.
* @param id The object ID.
* @param key The key to return the value for.
* @param defaultValue The default value to return if the key doesn't exist.
*
* @return The value associated to the key for the specified repository object.
*/
public String getValue(String type, long id, String key, String defaultValue) {
String result = null;

GenericRepositoryObjectWrapper<?> object = getRepositoryObject(type, id);
try {
if (object != null) {
result = object.getValue(client, key);
}
} catch (NoSuchElementException e) {
if (defaultValue != null) {
result = defaultValue;
} else {
IJ.error("Could not retrieve value: " + e.getMessage());
}
} catch (ServiceException | AccessException | ExecutionException e) {
IJ.error("Could not retrieve value: " + e.getMessage());
}
return result;
}


/**
* Removes the ROIs from an image in OMERO.
*
Expand Down Expand Up @@ -1525,6 +1599,21 @@ public String handleExtension(String name, Object[] args) {
results = String.valueOf(removed);
break;

case "getKeyValuePairs":
type = (String) args[0];
id = ((Double) args[1]).longValue();
String separator = (String) args[2];
results = getKeyValuePairs(type, id, separator);
break;

case "getValue":
type = (String) args[0];
id = ((Double) args[1]).longValue();
String key = (String) args[2];
String defaultValue = (String) args[3];
results = getValue(type, id, key, defaultValue);
break;

case "sudo":
sudo((String) args[0]);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @String(label="Username") USERNAME
// @String(label="Password", style='password', persist=false) PASSWORD
// @String(label="Host", value='wss://workshop.openmicroscopy.org/omero-ws') HOST
// @Integer(label="Port", value=443) PORT
// @Integer(label="Dataset ID", value=2331) dataset_id

run("OMERO Extensions");

connected = Ext.connectToOMERO(HOST, PORT, USERNAME, PASSWORD);

if(connected == "true") {
// Read all key value pairs from a dataset
kvs_dataset = Ext.getKeyValuePairs("dataset", dataset_id);

// Default separator is a TAB
kvs_dataset = split(kvs_dataset, "\t");
// ! If some cells are empty, split will ignore the double separators
// -> add a space between them first with replace(kvs_dataset, "\t\t", "\t \t");

for(j=0; j<kvs_dataset.length; j=j+2){
// Every even index is the key, every odd index is the value
print(kvs_dataset[j] + ": " + kvs_dataset[j+1]);
}

images = Ext.list("images", "dataset", dataset_id);
image_ids = split(images, ",");
for(i=0; i<image_ids.length; i++) {
// Read from each image the value associated to the "condition" key
condition_image = Ext.getValue("image", image_ids[i], "condition");
print(condition_image);
}
}

Ext.disconnect();
print("processing done");
10 changes: 10 additions & 0 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,14 @@ void testCannotLinkOrUnlink(String function) {
assertEquals(expected, outContent.toString().trim());
}

@Test
void testKeyNotExist() {
final String key = "notExist";
final double imageId = 2;
Object[] args = {"image", imageId, key, null};
ext.handleExtension("getValue", args);
String expected = "Could not retrieve value: Key \"" + key + "\" not found";
assertEquals(expected, outContent.toString().trim());
}

}
24 changes: 24 additions & 0 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,30 @@ void testSaveAndGetROIs() {
}


@ParameterizedTest(name = "[{index}] {0},{1},{2},...")
@CsvSource(delimiter = ';', value = {"image;1;null;testKey1\ttestValue1\ttestKey2\t20",
"image;3;' ';testKey1 testValue1 testKey2 20",
"image;2;&&;testKey1&&testValue2&&testKey2&&30",
"image;4;'';''"}, nullValues = {"null"})
void testGetKeyValuePairs(String type, Double id, String separator, String output) {
Object[] args = {type, id, separator};
String result = ext.handleExtension("getKeyValuePairs", args);
assertEquals(output, result);
}


@ParameterizedTest
@CsvSource(delimiter = ';', value = {"image;1;testKey1;null;testValue1",
"image;3;testKey2;null;20",
"image;2;testKey2;null;30",
"image;2;notExist;default;default"}, nullValues = {"null"})
void testGetValue(String type, Double id, String key, String defaultValue, String output) {
Object[] args = {type, id, key, defaultValue};
String result = ext.handleExtension("getValue", args);
assertEquals(output, result);
}


@Test
void testImportImage() throws IOException {
String path = "8bit-unsigned&pixelType=uint8&sizeZ=3&sizeC=5&sizeT=7&sizeX=512&sizeY=512.fake";
Expand Down

0 comments on commit a36b41e

Please sign in to comment.