Skip to content

Commit

Permalink
Add more
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Oct 26, 2023
1 parent 7b61ba7 commit a2c6c84
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 109 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/embl/mobie/lib/color/ColorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public abstract class ColorHelper
{
final static public double goldenRatio = 1.0 / ( 0.5 * Math.sqrt( 5 ) + 0.5 );

public static final String RANDOM_FROM_GLASBEY = "randomFromGlasbey";

public static Color getColor( ARGBType argbType )
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/embl/mobie/lib/hcs/HCSDataAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@

import net.thisptr.jackson.jq.internal.misc.Strings;
import org.embl.mobie.lib.annotation.AnnotatedRegion;
import org.embl.mobie.lib.hcs.Channel;
import org.embl.mobie.lib.hcs.Plate;
import org.embl.mobie.lib.hcs.Site;
import org.embl.mobie.lib.hcs.Well;
import org.embl.mobie.lib.serialize.Dataset;
import org.embl.mobie.lib.serialize.ImageDataSource;
import org.embl.mobie.lib.serialize.View;
Expand Down Expand Up @@ -197,7 +193,7 @@ public void addData( Dataset dataset )

private String getSiteID( Plate plate, Channel channel, Well well, Site site )
{
return getWellID( plate, channel, well ) + "-s" + site.getName();
return getWellID( plate, channel, well ) + "-s" + site.getId();
}

private String getWellID( Plate plate, Channel channel, Well well )
Expand Down
172 changes: 118 additions & 54 deletions src/main/java/org/embl/mobie/lib/hcs/OperettaMetadata.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
/*-
* #%L
* Fiji viewer for MoBIE projects
* %%
* Copyright (C) 2018 - 2023 EMBL
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.embl.mobie.lib.hcs;

import ij.gui.PointRoi;
import ij.gui.Roi;
import ch.epfl.biop.bdv.img.opener.ChannelProperties;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import mpicbg.spim.data.sequence.VoxelDimensions;
import org.embl.mobie.lib.color.ColorHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand All @@ -41,16 +13,25 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import java.util.LinkedHashMap;

// one could extract an interface here for the
// getter methods if this is useful for other data
public class OperettaMetadata
{
private HashMap< String, Element > filenameToMetadata;
private HashMap< String, Element > filenameToImageElement;
private HashMap< String, Element > channelIDToElement;
private HashMap< String, Integer > filenameToImageIndex;
private double dx;
private double dy;
private String spatialUnit;
private int imageSizeX;
private int imageSizeY;
private int maxIntensity;

public OperettaMetadata( File xml )
{
Expand All @@ -76,29 +57,57 @@ private void parse( File xml ) throws ParserConfigurationException, SAXException
Document doc = builder.parse( xml );
doc.getDocumentElement().normalize();

final double dx = Double.parseDouble( doc.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getTextContent() );
final double dy = Double.parseDouble( doc.getElementsByTagName( "ImageResolutionY" ).item( 0 ).getTextContent() );
final String unit = doc.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getAttributes().item( 0 ).getTextContent();
dx = Double.parseDouble( doc.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getTextContent() );
dy = Double.parseDouble( doc.getElementsByTagName( "ImageResolutionY" ).item( 0 ).getTextContent() );
spatialUnit = doc.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getAttributes().item( 0 ).getTextContent();

// could be channel specific
//
imageSizeX = Integer.parseInt( doc.getElementsByTagName( "ImageSizeX" ).item( 0 ).getTextContent() );
imageSizeY = Integer.parseInt( doc.getElementsByTagName( "ImageSizeY" ).item( 0 ).getTextContent() );

// could be channel specific
//
maxIntensity = Integer.parseInt( doc.getElementsByTagName( "MaxIntensity" ).item( 0 ).getTextContent() );

filenameToMetadata = new HashMap<>();
final NodeList fileNames = doc.getElementsByTagName( "URL" );
final int numFiles = fileNames.getLength();
for ( int i = 0; i < numFiles; i++ )
filenameToImageElement = new LinkedHashMap<>();
filenameToImageIndex = new LinkedHashMap<>();
channelIDToElement = new LinkedHashMap<>();

final NodeList imageFileNames = doc.getElementsByTagName( "URL" );
final int numImages = imageFileNames.getLength();
for ( int imageIndex = 0; imageIndex < numImages; imageIndex++ )
{
final Node item = fileNames.item( i );
final Node item = imageFileNames.item( imageIndex );
final Element parentNode = (Element) item.getParentNode();
filenameToMetadata.put( item.getTextContent(), parentNode );
filenameToImageElement.put( item.getTextContent(), parentNode );
filenameToImageIndex.put( item.getTextContent(), imageIndex );
}

final NodeList channelIDs = doc.getElementsByTagName( "MaxIntensity" );
final int numChannels = channelIDs.getLength();
for ( int channelIndex = 0; channelIndex < numChannels; channelIndex++ )
{
final Node item = channelIDs.item( channelIndex );
final Element parentNode = (Element) item.getParentNode();
final String channelID = parentNode.getAttributes().item( 0 ).getTextContent();
channelIDToElement.put( channelID, parentNode );
}
}

public VoxelDimensions getVoxelDimensions( String path )
{
final Element element = getElement( path );
final double imageResolutionX = getDouble( element, "ImageResolutionX" );
final double imageResolutionY = getDouble( element, "ImageResolutionY" );
final String unit = element.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getAttributes().item( 0 ).getTextContent();
// In Operetta 4 and 5 this is not consistently at the same position
// thus we just fetch it once globally. Hopefully it is the same for all
// images anyway.

// This only works in Operetta 4
// final Element element = getElement( path );
// final double imageResolutionX = getDouble( element, "ImageResolutionX" );
// final double imageResolutionY = getDouble( element, "ImageResolutionY" );
// final String unit = element.getElementsByTagName( "ImageResolutionX" ).item( 0 ).getAttributes().item( 0 ).getTextContent();

return new FinalVoxelDimensions( unit, imageResolutionX, imageResolutionY, 1.0 );
return new FinalVoxelDimensions( spatialUnit, dx, dy, 1.0 );
}

private double getDouble( Element element, String tag )
Expand All @@ -114,10 +123,36 @@ private double getDouble( Element element, String tag )
}
}

private Element getElement( String path )
private int getInteger( Element element, String tag )
{
try
{
return Integer.parseInt( element.getElementsByTagName( tag ).item( 0 ).getTextContent() );
}
catch ( Exception e )
{
e.printStackTrace();
throw new RuntimeException( e );
}
}

private String getString( Element element, String tag )
{
try
{
return element.getElementsByTagName( tag ).item( 0 ).getTextContent();
}
catch ( Exception e )
{
e.printStackTrace();
throw new RuntimeException( e );
}
}

private Element getImageElement( String path )
{
final String filename = new File( path ).getName();
final Element element = filenameToMetadata.get( filename );
final Element element = filenameToImageElement.get( filename );
if ( element == null )
{
System.err.println("Could not find operetta metadata for " + filename );
Expand All @@ -129,15 +164,44 @@ private Element getElement( String path )
public boolean contains( String path )
{
final String filename = new File( path ).getName();
return filenameToMetadata.containsKey( filename );
return filenameToImageElement.containsKey( filename );
}

public double[] getRealPosition( String path )
{
final Element element = getElement( path );
final Element imageElement = getImageElement( path );
return new double[]{
getDouble( element, "PositionX" ),
-getDouble( element, "PositionY" )
};
getDouble( imageElement, "PositionX" ),
-getDouble( imageElement, "PositionY" )
};
}

public String getColor( String path )
{
final Element imageElement = getImageElement( path );
final String channelID = getString( imageElement, "ChannelID" );
final Element channelElement = channelIDToElement.get( channelID );
final int mainEmissionWavelength = getInteger( channelElement, "MainEmissionWavelength" );

final Color color = ChannelProperties.getColorFromWavelength( mainEmissionWavelength );
final String string = ColorHelper.getString( ColorHelper.getARGBType( color ) );
return string;
}

public int getImageIndex( String path )
{
return filenameToImageIndex.get( new File( path ).getName() );
}

public double[] getContrastLimits( String path )
{
// TODO: fetch per channel via channelID of image
return new double[]{ 0, maxIntensity };
}

public int[] getSiteDimensions( String path )
{
// TODO: fetch per channel via channelID of image
return new int[]{ imageSizeX, imageSizeY };
}
}
}
Loading

0 comments on commit a2c6c84

Please sign in to comment.