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

Replace Doubles with Integers #7

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 53 additions & 45 deletions src/loci/formats/in/ZarrReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
Expand Down Expand Up @@ -72,15 +72,15 @@ public class ZarrReader extends FormatReader {
private HashMap<Integer, ArrayList<String>> resSeries = new HashMap<Integer, ArrayList<String>>();
private HashMap<String, Integer> resCounts = new HashMap<String, Integer>();
private HashMap<String, Integer> resIndexes = new HashMap<String, Integer>();

private boolean hasSPW = false;

public ZarrReader() {
super("Zarr", "zarr");
suffixSufficient = false;
domains = new String[] {FormatTools.UNKNOWN_DOMAIN};
}

/* @see loci.formats.IFormatReader#isThisType(String, boolean) */
@Override
public boolean isThisType(String name, boolean open) {
Expand All @@ -90,7 +90,7 @@ public boolean isThisType(String name, boolean open) {
}
return super.isThisType(name, open);
}

/* @see loci.formats.IFormatReader#close() */
@Override
public void close() throws IOException {
Expand All @@ -110,14 +110,14 @@ public int getOptimalTileHeight() {
FormatTools.assertId(currentId, true, 1);
return zarrService.getChunkSize()[1];
}

/* @see loci.formats.IFormatReader#getOptimalTileHeight() */
@Override
public int getOptimalTileWidth() {
FormatTools.assertId(currentId, true, 1);
return zarrService.getChunkSize()[0];
}

/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
Expand All @@ -128,9 +128,9 @@ protected void initFile(String id) throws FormatException, IOException {
String zarrRootPath = zarrPath.substring(0, zarrPath.indexOf(".zarr") + 5);
String name = zarrRootPath.substring(zarrRootPath.lastIndexOf(File.separator)+1, zarrRootPath.length() - 5);
Location omeMetaFile = new Location( zarrRootPath, name+".ome.xml" );

initializeZarrService(zarrRootPath);

/*
* Open OME metadata file
* TODO: Old code to either be reworked with writer or removed entirely
Expand All @@ -150,7 +150,7 @@ protected void initFile(String id) throws FormatException, IOException {
}
}
omeDocument.getDocumentElement().normalize();

OMEXMLService service = null;
String xml = null;
try
Expand All @@ -171,17 +171,17 @@ protected void initFile(String id) throws FormatException, IOException {
{
LOGGER.debug( "", e1 );
}

int numDatasets = omexmlMeta.getImageCount();

int oldSeries = getSeries();
core.clear();
for (int i=0; i<numDatasets; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);

setSeries(i);

Integer w = omexmlMeta.getPixelsSizeX(i).getValue();
Integer h = omexmlMeta.getPixelsSizeY(i).getValue();
Integer t = omexmlMeta.getPixelsSizeT(i).getValue();
Expand All @@ -190,7 +190,7 @@ protected void initFile(String id) throws FormatException, IOException {
if (w == null || h == null || t == null || z == null | c == null) {
throw new FormatException("Image dimensions not found");
}

Boolean endian = null;
String pixType = omexmlMeta.getPixelsType(i).toString();
ms.dimensionOrder = omexmlMeta.getPixelsDimensionOrder(i).toString();
Expand Down Expand Up @@ -225,14 +225,14 @@ protected void initFile(String id) throws FormatException, IOException {
try {
jsonAttr = ZarrUtils.toJson(attr, true);
store.setXMLAnnotationValue(jsonAttr, attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:", attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:", attrIndex);
store.setXMLAnnotationID(xml_id, attrIndex);
} catch (JZarrException e) {
LOGGER.warn("Failed to convert attributes to JSON");
e.printStackTrace();
}
}

// Parse group attributes
for (String key: zarrService.getGroupKeys(zarrRootPath)) {
Map<String, Object> attributes = zarrService.getGroupAttr(zarrRootPath+File.separator+key);
Expand All @@ -245,15 +245,15 @@ protected void initFile(String id) throws FormatException, IOException {
try {
jsonAttr = ZarrUtils.toJson(attributes, true);
store.setXMLAnnotationValue(jsonAttr, attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex);
store.setXMLAnnotationID(xml_id, attrIndex);
} catch (JZarrException e) {
LOGGER.warn("Failed to convert attributes to JSON");
e.printStackTrace();
}
}
}

// Parse array attributes
for (String key: zarrService.getArrayKeys(zarrRootPath)) {
Map<String, Object> attributes = zarrService.getArrayAttr(zarrRootPath+File.separator+key);
Expand All @@ -263,7 +263,7 @@ protected void initFile(String id) throws FormatException, IOException {
try {
jsonAttr = ZarrUtils.toJson(attributes, true);
store.setXMLAnnotationValue(jsonAttr, attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex);
String xml_id = MetadataTools.createLSID("AttributesAnnotation:"+key, attrIndex);
store.setXMLAnnotationID(xml_id, attrIndex);
} catch (JZarrException e) {
LOGGER.warn("Failed to convert attributes to JSON");
Expand All @@ -274,9 +274,9 @@ protected void initFile(String id) throws FormatException, IOException {

arrayPaths = new ArrayList<String>();
arrayPaths.addAll(zarrService.getArrayKeys(zarrRootPath));

orderArrayPaths(zarrRootPath);

core.clear();
int resolutionTotal = 0;
for (int i=0; i<arrayPaths.size(); i++) {
Expand All @@ -288,10 +288,10 @@ protected void initFile(String id) throws FormatException, IOException {
if (resIndexes.get(zarrRootPath+File.separator+arrayPaths.get(i)) != null) {
resolutionIndex = resIndexes.get(zarrRootPath+File.separator+arrayPaths.get(i));
}

CoreMetadata ms = new CoreMetadata();
core.add(ms);

if (hasFlattenedResolutions()) {
setSeries(i);
}
Expand All @@ -305,7 +305,7 @@ protected void initFile(String id) throws FormatException, IOException {

ms.pixelType = zarrService.getPixelType();
int[] shape = zarrService.getShape();

ms.sizeX = shape[4];
ms.sizeY = shape[3];
ms.sizeT = shape[0];
Expand Down Expand Up @@ -353,7 +353,7 @@ protected void initializeZarrService(String rootPath) throws IOException, Format
zarrService = new JZarrServiceImpl(rootPath);
openZarr();
}

@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
Expand Down Expand Up @@ -407,13 +407,13 @@ else if (image instanceof double[]) {
}
return buf;
}

@Override
public void setSeries(int no) {
super.setSeries(no);
openZarr();
}

private void openZarr() {
try {
if (currentId != null && zarrService != null) {
Expand All @@ -428,7 +428,7 @@ private void openZarr() {
e.printStackTrace();
}
}

private void orderArrayPaths(String root) {
for (int i = 0; i < resSeries.size(); i++) {
for (String arrayPath: resSeries.get(i)) {
Expand All @@ -439,7 +439,7 @@ private void orderArrayPaths(String root) {
}
}
}

private void parseResolutionCount(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand All @@ -462,7 +462,7 @@ private void parseResolutionCount(String root, String key) throws IOException, F
}
}
}

private void parsePlate(String root, String key, MetadataStore store) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand All @@ -477,7 +477,7 @@ private void parsePlate(String root, String key, MetadataStore store) throws IOE
ArrayList<Object> acquistions = (ArrayList<Object> )plates.get("acquisitions");
String plateName = (String) plates.get("name");
String fieldCount = (String) plates.get("filed_count");

String plate_id = MetadataTools.createLSID("Plate", p);
store.setPlateID(plate_id, p);
store.setPlateName(plateName, p);
Expand Down Expand Up @@ -518,7 +518,7 @@ private void parsePlate(String root, String key, MetadataStore store) throws IOE
}
}
}

private int parseWells(String root, String key, MetadataStore store, int plateIndex, int wellIndex, int wellSamplesCount) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand All @@ -530,8 +530,8 @@ private int parseWells(String root, String key, MetadataStore store, int plateIn
for (int i = 0; i < images.size(); i++) {
Map<String, Object> image = (Map<String, Object>) images.get(i);
String imagePath = (String) image.get("path");
double acquisition = (double) image.get("acquisition");
double acquisition = (double) image.get("acquisition");

String site_id = MetadataTools.createLSID("WellSample", wellSamplesCount);
store.setWellSampleID(site_id, plateIndex, wellIndex, i);
store.setWellSampleIndex(new NonNegativeInteger(i), plateIndex, wellIndex, i);
Expand All @@ -543,7 +543,7 @@ private int parseWells(String root, String key, MetadataStore store, int plateIn
}
return wellSamplesCount;
}

private void parseLabels(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand All @@ -554,7 +554,7 @@ private void parseLabels(String root, String key) throws IOException, FormatExce
}
}
}

private void parseImageLabels(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand Down Expand Up @@ -593,7 +593,7 @@ private void parseImageLabels(String root, String key) throws IOException, Forma
}
}
}

private void parseOmeroMetadata(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
Expand All @@ -613,10 +613,10 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For
String channelLabel = (String) channel.get("label");
Map<String, Object> window = (Map<String, Object>)channel.get("window");
if (window != null) {
Double windowStart = (Double) window.get("start");
Double windowEnd = (Double) window.get("end");
Double windowMin = (Double) window.get("min");
Double windowMax = (Double) window.get("max");
Double windowStart = getDouble(window, "start");
Double windowEnd = getDouble(window, "end");
Double windowMin = getDouble(window, "min");
Double windowMax = getDouble(window, "max");
}
}
Map<String, Object> rdefs = (Map<String, Object>)omeroMetadata.get("rdefs");
Expand All @@ -627,11 +627,19 @@ private void parseOmeroMetadata(String root, String key) throws IOException, For
}
}
}


private Double getDouble(Map<String, Object> src, String key) {
Number val = (Number) src.get(key);
if (val == null) {
sbesson marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
return val.doubleValue();
}

/* @see loci.formats.IFormatReader#getUsedFiles(boolean) */
@Override
public String[] getUsedFiles(boolean noPixels) {

FormatTools.assertId(currentId, true, 1);
String zarrRootPath = currentId.substring(0, currentId.indexOf(".zarr") + 5);
ArrayList<String> usedFiles = new ArrayList<String>();
Expand All @@ -654,7 +662,7 @@ public String[] getUsedFiles(boolean noPixels) {
fileArr = usedFiles.toArray(fileArr);
return fileArr;
}

/* @see loci.formats.SubResolutionFormatReader#getDomains() */
@Override
public String[] getDomains() {
Expand Down