Skip to content

Commit

Permalink
Add check for new columns and handle empty ROI list in TableWrapper (#71
Browse files Browse the repository at this point in the history
)

* Check table columns before adding rows
* Fix ROI column creation when ROI list is empty
* Add test
  • Loading branch information
ppouchin authored Jul 27, 2023
1 parent f301a73 commit e32532f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/main/java/fr/igred/omero/annotations/TableWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -394,18 +395,18 @@ private static ROIData[] createROIColumn(ResultsTable results,
safeParseLong(r.getProperty(roiIdProperty))))
.filter(p -> p.getKey() != null)
.filter(p -> p.getValue() != null)
.collect(toMap(SimpleEntry::getKey,
p -> id2roi.get(p.getValue()),
(x1, x2) -> x1));
.collect(HashMap::new,
(m, v) -> m.put(v.getKey(), id2roi.get(v.getValue())),
HashMap::putAll);

Map<String, ROIData> shape2roi = ijRois.stream()
.map(r -> new SimpleEntry<>(r.getName(),
safeParseLong(r.getProperty(roiIdProperty))))
.filter(p -> p.getKey() != null)
.filter(p -> p.getValue() != null)
.collect(toMap(SimpleEntry::getKey,
p -> id2roi.get(p.getValue()),
(x1, x2) -> x1));
.collect(HashMap::new,
(m, v) -> m.put(v.getKey(), id2roi.get(v.getValue())),
HashMap::putAll);
String colToDelete = "";
if (results.columnExists(roiIdProperty)) {
Variable[] roiCol = results.getColumnAsVariables(roiIdProperty);
Expand Down Expand Up @@ -440,6 +441,20 @@ private static ROIData[] createROIColumn(ResultsTable results,
}


/**
* Checks if the new columns match the existing ones.
*
* @param nColumns The number of columns in the current Results Table.
* @param offset The offset in the current Results Table.
*/
private boolean checkColumns(int nColumns, int offset) {
boolean match = offset + nColumns == columnCount;
if (offset > 0 && !columns[0].getType().equals(ImageData.class)) match = false;
if (offset > 1 && !columns[1].getType().equals(ROIData.class)) match = false;
return match;
}


/**
* Sets the information about a given column.
*
Expand Down Expand Up @@ -522,8 +537,8 @@ public void addRows(Client client, ResultsTable results, Long imageId, List<? ex
String[] headings = rt.getHeadings();

int nColumns = headings.length;
if (nColumns + offset != columnCount) {
throw new IllegalArgumentException("Number of columns mismatch");
if (!checkColumns(nColumns, offset)) {
throw new IllegalArgumentException("Number or type of columns mismatch");
}

int n = rt.size();
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/fr/igred/omero/annotations/ImageJTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

Expand Down Expand Up @@ -722,4 +723,41 @@ void testSaveTableAs() throws Exception {
Files.deleteIfExists(file.toPath());
}


@Test
void testAddRowsWithMismatch() throws Exception {
List<ROIWrapper> rois = new ArrayList<>(2);
rois.add(new ROIWrapper());
rois.add(new ROIWrapper());

rois.get(0).setImage(image);
rois.get(1).setImage(image);

for (int i = 0; i < 4; i++) {
RectangleWrapper rectangle = new RectangleWrapper();
rectangle.setText(String.valueOf(10 + i % 2));
rectangle.setCoordinates(i * 2, i * 2, 10, 10);
rectangle.setZ(i);
rectangle.setT(0);
rectangle.setC(0);

if (i % 2 == 1) rois.get(0).addShape(rectangle);
else rois.get(1).addShape(rectangle);
}

List<ROIWrapper> newROIs = image.saveROIs(client, rois);
List<Roi> ijRois = ROIWrapper.toImageJ(newROIs);

String label1 = image.getName() + ":" + newROIs.get(0).getShapes().get(0).getText() + ":4";
String label2 = image.getName() + ":" + newROIs.get(1).getShapes().get(0).getText() + ":10";

ResultsTable results1 = createOneRowResultsTable(label1, volume1, unit1);
ResultsTable results2 = createOneRowResultsTable(label2, volume2, unit2);

TableWrapper table = new TableWrapper(client, results1, IMAGE1.id, ijRois);

assertThrows(IllegalArgumentException.class,
() -> table.addRows(client, results2, null, ijRois));
}

}

0 comments on commit e32532f

Please sign in to comment.