Skip to content

Commit 02a0138

Browse files
committed
Fix bug that rendered transformations singular
N5 writers were not closed properly, which led to their caches not being flushed, which in turn led to transformations being written as all-zeros
1 parent 901660a commit 02a0138

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

src/main/java/io/SpatialDataIO.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,21 @@ public void writeData(STDataAssembly data) throws IOException {
229229
if (readOnly)
230230
throw new IllegalStateException("Trying to write to read-only file.");
231231

232-
N5Writer writer = (N5Writer) ioSupplier.get();
233-
STData stData = data.data();
232+
try (N5Writer writer = (N5Writer) ioSupplier.get()) {
233+
STData stData = data.data();
234234

235-
logger.debug( "Saving spatial data ... " );
236-
long time = System.currentTimeMillis();
237-
238-
initializeDataset(writer, stData);
235+
logger.debug("Saving spatial data ... ");
236+
long time = System.currentTimeMillis();
239237

240-
writeExpressionValues(writer, stData.getAllExprValues());
241-
writeLocations(writer, stData.getLocations());
242-
updateTransformation(writer, data.transform(), transformFieldName);
243-
//writeTransformation(writer, data.intensityTransform(), "intensity_transform");
238+
initializeDataset(writer, stData);
244239

245-
updateStoredAnnotations(stData.getAnnotations());
240+
writeExpressionValues(writer, stData.getAllExprValues());
241+
writeLocations(writer, stData.getLocations());
242+
updateTransformation(writer, data.transform(), transformFieldName);
246243

247-
logger.debug("Saving took {} ms.", System.currentTimeMillis() - time);
244+
updateStoredAnnotations(stData.getAnnotations());
245+
logger.debug("Saving took {} ms.", System.currentTimeMillis() - time);
246+
}
248247
}
249248

250249
/**
@@ -256,14 +255,15 @@ public void updateStoredAnnotations(Map<String, RandomAccessibleInterval<? exten
256255
if (readOnly)
257256
throw new IllegalStateException("Trying to write to read-only file.");
258257

259-
N5Writer writer = (N5Writer) ioSupplier.get();
260-
List<String> existingAnnotations = detectAnnotations(writer);
258+
try (N5Writer writer = (N5Writer) ioSupplier.get()) {
259+
List<String> existingAnnotations = detectAnnotations(writer);
261260

262-
for (Entry<String, RandomAccessibleInterval<? extends NativeType<?>>> newEntry : metadata.entrySet()) {
263-
if (existingAnnotations.contains(newEntry.getKey()))
264-
logger.warn("Metadata '{}' already exists. Skip writing.", newEntry.getKey());
265-
else
266-
writeAnnotations(writer, newEntry.getKey(), newEntry.getValue());
261+
for (Entry<String, RandomAccessibleInterval<? extends NativeType<?>>> newEntry : metadata.entrySet()) {
262+
if (existingAnnotations.contains(newEntry.getKey()))
263+
logger.warn("Metadata '{}' already exists. Skip writing.", newEntry.getKey());
264+
else
265+
writeAnnotations(writer, newEntry.getKey(), newEntry.getValue());
266+
}
267267
}
268268
}
269269

@@ -276,14 +276,15 @@ public void updateStoredGeneAnnotations(Map<String, RandomAccessibleInterval<? e
276276
if (readOnly)
277277
throw new IllegalStateException("Trying to write to read-only file.");
278278

279-
N5Writer writer = (N5Writer) ioSupplier.get();
280-
List<String> existingGeneAnnotations = detectGeneAnnotations(writer);
279+
try (N5Writer writer = (N5Writer) ioSupplier.get()) {
280+
List<String> existingGeneAnnotations = detectGeneAnnotations(writer);
281281

282-
for (Entry<String, RandomAccessibleInterval<? extends NativeType<?>>> newEntry : metadata.entrySet()) {
283-
if (existingGeneAnnotations.contains(newEntry.getKey()))
284-
logger.warn("Metadata '{}' already exists. Skip writing.", newEntry.getKey());
285-
else
286-
writeGeneAnnotations(writer, newEntry.getKey(), newEntry.getValue());
282+
for (Entry<String, RandomAccessibleInterval<? extends NativeType<?>>> newEntry : metadata.entrySet()) {
283+
if (existingGeneAnnotations.contains(newEntry.getKey()))
284+
logger.warn("Metadata '{}' already exists. Skip writing.", newEntry.getKey());
285+
else
286+
writeGeneAnnotations(writer, newEntry.getKey(), newEntry.getValue());
287+
}
287288
}
288289
}
289290

@@ -327,8 +328,9 @@ public void updateTransformation(AffineGet transform, String name) throws IOExce
327328
if (readOnly)
328329
throw new IllegalStateException("Trying to modify a read-only file.");
329330

330-
N5Writer writer = (N5Writer) ioSupplier.get();
331-
updateTransformation(writer, transform, name);
331+
try (N5Writer writer = (N5Writer) ioSupplier.get()) {
332+
updateTransformation(writer, transform, name);
333+
}
332334
}
333335

334336
/**

src/test/java/IOTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Arrays;
1818
import java.util.List;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.fail;
2223
import static org.junit.jupiter.api.Named.named;
@@ -60,15 +61,18 @@ public void io_works_for_transformations(String path) {
6061
STData data = TestUtils.createTestDataSet();
6162
STDataStatistics stats = new STDataStatistics(data);
6263
final AffineTransform2D transform = new AffineTransform2D();
63-
transform.rotate(3.14159 / 4);
64+
transform.rotate(3.14159 / 6);
6465
STDataAssembly expected = new STDataAssembly(data, stats, transform);
6566

6667
try {
6768
SpatialDataIO sdio = SpatialDataIO.open(getPlaygroundPath(path), executorService);
6869
sdio.writeData(expected);
6970
STDataAssembly actual = sdio.readData();
70-
71-
TestUtils.compareSTDataAssemblies(actual, expected);
71+
for (int i = 0; i < 2; i++) {
72+
for (int j = 0; j < 2; j++) {
73+
assertEquals(transform.get(i, j), actual.transform().get(i, j));
74+
}
75+
}
7276
}
7377
catch (IOException e) {
7478
fail("Could not write / read file: ", e);

0 commit comments

Comments
 (0)