use of ch.epfl.biop.bdv.bioformats.bioformatssource.BioFormatsBdvOpener in project bigdataviewer-biop-tools by BIOP.
the class QuPathImageLoader method getSetupImgLoader.
@Override
public BioFormatsSetupLoader<?, ?> getSetupImgLoader(int setupId) {
if (imgLoaders.containsKey(setupId)) {
// Already created - return it
return imgLoaders.get(setupId);
} else {
QuPathEntryAndChannel qec = viewSetupToQuPathEntryAndChannel.get(setupId);
BioFormatsBdvOpener opener = this.openerMap.get(qec.entry.uri);
int iS = qec.entry.bioformatsIndex;
int iC = qec.iChannel;
logger.debug("loading qupath entry number = " + qec.entry + "setupId = " + setupId + " series" + iS + " channel " + iC);
BioFormatsSetupLoader<?, ?> imgL = new BioFormatsSetupLoader(opener, iS, iC, tTypeGetter.get(setupId), vTypeGetter.get(setupId));
imgLoaders.put(setupId, imgL);
return imgL;
}
}
use of ch.epfl.biop.bdv.bioformats.bioformatssource.BioFormatsBdvOpener in project bigdataviewer-biop-tools by BIOP.
the class QuPathToSpimData method getSpimDataInstance.
public AbstractSpimData getSpimDataInstance(URI quPathProject, final BioFormatsBdvOpener openerModel) {
viewSetupCounter = 0;
nTileCounter = 0;
maxTimepoints = -1;
channelCounter = 0;
// No Illumination
Illumination dummy_ill = new Illumination(0);
// No Angle
Angle dummy_ang = new Angle(0);
// Many View Setups
List<ViewSetup> viewSetups = new ArrayList<>();
try {
JsonObject projectJson = ProjectIO.loadRawProject(new File(quPathProject));
Gson gson = new Gson();
MinimalQuPathProject project = gson.fromJson(projectJson, MinimalQuPathProject.class);
logger.debug("Opening QuPath project " + project.uri);
Set<QuPathImageLoader.QuPathBioFormatsSourceIdentifier> quPathSourceIdentifiers = new HashSet<>();
// Performance
Map<BioFormatsBdvOpener, IFormatReader> cachedReaders = new HashMap<>();
project.images.forEach(image -> {
logger.debug("Opening qupath image " + image);
QuPathImageLoader.QuPathBioFormatsSourceIdentifier identifier = new QuPathImageLoader.QuPathBioFormatsSourceIdentifier();
if (image.serverBuilder.builderType.equals("rotated")) {
// "ROTATE_ANGLE" for instance "ROTATE_0", "ROTATE_270", etc
String angleDegreesStr = image.serverBuilder.rotation.substring(7);
logger.debug("Rotated image server (" + angleDegreesStr + ")");
if (angleDegreesStr.equals("NONE")) {
identifier.angleRotationZAxis = 0;
} else {
identifier.angleRotationZAxis = (Double.valueOf(angleDegreesStr) / 180.0) * Math.PI;
}
// To keep the metadata (pixel size for instance)
MinimalQuPathProject.ServerBuilderMetadata metadata = image.serverBuilder.metadata;
// Skips the rotation
image.serverBuilder = image.serverBuilder.builder;
image.serverBuilder.metadata = metadata;
}
if (image.serverBuilder.builderType.equals("uri")) {
logger.debug("URI image server");
if (image.serverBuilder.providerClassName.equals("qupath.lib.images.servers.bioformats.BioFormatsServerBuilder")) {
try {
URI uri = new URI(image.serverBuilder.uri.getScheme(), image.serverBuilder.uri.getHost(), image.serverBuilder.uri.getPath(), null);
// This appears to work more reliably than converting to a File
String filePath = Paths.get(uri).toString();
if (!openerMap.keySet().contains(image.serverBuilder.uri)) {
BioFormatsBdvOpener opener = new BioFormatsBdvOpener(openerModel).location(Paths.get(uri).toString());
opener = opener.ignoreMetadata();
openerMap.put(image.serverBuilder.uri, opener);
cachedReaders.put(opener, opener.getNewReader());
uriToFileIndexMap.put(image.serverBuilder.uri, fileIndexCounter);
fileIndexCounter++;
}
identifier.uri = image.serverBuilder.uri;
identifier.sourceFile = filePath;
identifier.indexInQuPathProject = project.images.indexOf(image);
identifier.entryID = project.images.get(identifier.indexInQuPathProject).entryID;
int iSerie = image.serverBuilder.args.indexOf("--series");
Tile tile = new Tile(nTileCounter);
nTileCounter++;
if (iSerie == -1) {
logger.error("Series not found in qupath project server builder!");
identifier.bioformatsIndex = -1;
} else {
identifier.bioformatsIndex = Integer.valueOf(image.serverBuilder.args.get(iSerie + 1));
}
logger.debug(identifier.toString());
quPathSourceIdentifiers.add(identifier);
BioFormatsBdvOpener opener = openerMap.get(image.serverBuilder.uri);
IFormatReader memo = cachedReaders.get(opener);
memo.setSeries(identifier.bioformatsIndex);
logger.debug("Number of Series : " + memo.getSeriesCount());
IMetadata omeMeta = (IMetadata) memo.getMetadataStore();
memo.setMetadataStore(omeMeta);
logger.debug("\t Serie " + identifier.bioformatsIndex + " Number of timesteps = " + omeMeta.getPixelsSizeT(identifier.bioformatsIndex).getNumberValue().intValue());
// ---------- Serie > Channels
logger.debug("\t Serie " + identifier.bioformatsIndex + " Number of channels = " + omeMeta.getChannelCount(identifier.bioformatsIndex));
IntStream channels = IntStream.range(0, omeMeta.getChannelCount(identifier.bioformatsIndex));
if (omeMeta.getPixelsSizeT(identifier.bioformatsIndex).getNumberValue().intValue() > maxTimepoints) {
maxTimepoints = omeMeta.getPixelsSizeT(identifier.bioformatsIndex).getNumberValue().intValue();
}
// number of pixels .. no calibration
Dimensions dims = BioFormatsMetaDataHelper.getSeriesDimensions(omeMeta, identifier.bioformatsIndex);
logger.debug("X:" + dims.dimension(0) + " Y:" + dims.dimension(1) + " Z:" + dims.dimension(2));
VoxelDimensions voxDims = BioFormatsMetaDataHelper.getSeriesVoxelDimensions(omeMeta, identifier.bioformatsIndex, opener.u, opener.voxSizeReferenceFrameLength);
QuPathEntryEntity qpentry = new QuPathEntryEntity(identifier.entryID);
qpentry.setName(QuPathEntryEntity.getNameFromURIAndSerie(identifier.uri, identifier.bioformatsIndex));
qpentry.setQuPathProjectionLocation(Paths.get(quPathProject).toString());
channels.forEach(iCh -> {
QuPathImageLoader.QuPathEntryAndChannel usc = new QuPathImageLoader.QuPathEntryAndChannel(identifier, iCh);
viewSetupToQuPathEntryAndChannel.put(viewSetupCounter, usc);
int ch_id = getChannelId(omeMeta, identifier.bioformatsIndex, iCh, memo.isRGB());
String setupName = image.imageName + "_" + getChannelName(omeMeta, identifier.bioformatsIndex, iCh);
logger.debug(setupName);
ViewSetup vs = new ViewSetup(viewSetupCounter, setupName, dims, voxDims, // Tile is index of Serie
tile, channelIdToChannel.get(ch_id), dummy_ang, dummy_ill);
viewSetupToImageEntry.put(viewSetupCounter, image);
FileIndex fi = new FileIndex(uriToFileIndexMap.get(identifier.uri), identifier.sourceFile);
vs.setAttribute(fi);
SeriesNumber sn = new SeriesNumber(identifier.bioformatsIndex);
vs.setAttribute(sn);
vs.setAttribute(qpentry);
// Attempt to set color
Displaysettings ds = new Displaysettings(viewSetupCounter);
ds.min = 0;
ds.max = 255;
ds.isSet = false;
// ----------- Color
ARGBType color = BioFormatsMetaDataHelper.getColorFromMetadata(omeMeta, identifier.bioformatsIndex, iCh);
if (color != null) {
ds.isSet = true;
ds.color = new int[] { ARGBType.red(color.get()), ARGBType.green(color.get()), ARGBType.blue(color.get()), ARGBType.alpha(color.get()) };
}
vs.setAttribute(ds);
viewSetups.add(vs);
logger.debug("View Setup " + viewSetupCounter + " series " + iSerie + " channel " + iCh);
viewSetupCounter++;
});
} catch (URISyntaxException e) {
logger.error("URI Syntax error " + e.getMessage());
e.printStackTrace();
}
} else {
logger.error("Unsupported " + image.serverBuilder.providerClassName + " class name provider");
IJ.log("Unsupported " + image.serverBuilder.providerClassName + " class name provider");
}
} else {
logger.error("Unsupported " + image.serverBuilder.builderType + " server builder");
IJ.log("Unsupported " + image.serverBuilder.builderType + " server builder");
}
});
// ------------------- BUILDING SPIM DATA
List<TimePoint> timePoints = new ArrayList<>();
IntStream.range(0, maxTimepoints).forEach(tp -> timePoints.add(new TimePoint(tp)));
final ArrayList<ViewRegistration> registrations = new ArrayList<>();
List<ViewId> missingViews = new ArrayList<>();
for (int iViewSetup = 0; iViewSetup < viewSetupCounter; iViewSetup++) {
QuPathImageLoader.QuPathEntryAndChannel usc = viewSetupToQuPathEntryAndChannel.get(iViewSetup);
BioFormatsBdvOpener opener = openerMap.get(usc.entry.uri);
IFormatReader memo = cachedReaders.get(openerMap.get(usc.entry.uri));
final IMetadata omeMeta = (IMetadata) memo.getMetadataStore();
final int bfIndex = usc.entry.bioformatsIndex;
final int nTimepoints = omeMeta.getPixelsSizeT(bfIndex).getNumberValue().intValue();
final int vs = iViewSetup;
AffineTransform3D affine = BioFormatsMetaDataHelper.getSeriesRootTransform(omeMeta, bfIndex, opener.u, // AffineTransform3D positionPreTransform,
opener.positionPreTransformMatrixArray, // AffineTransform3D positionPostTransform,
opener.positionPostTransformMatrixArray, opener.positionReferenceFrameLength, // boolean positionIsImageCenter,
opener.positionIsImageCenter, // voxSizePreTransform,
opener.voxSizePreTransformMatrixArray, // AffineTransform3D voxSizePostTransform,
opener.voxSizePostTransformMatrixArray, // null, //Length voxSizeReferenceFrameLength,
opener.voxSizeReferenceFrameLength, // axesOfImageFlip
opener.axesOfImageFlip);
MinimalQuPathProject.PixelCalibrations pixelCalibrations = null;
if (viewSetupToImageEntry.get(vs) != null) {
if (viewSetupToImageEntry.get(vs).serverBuilder != null)
if (viewSetupToImageEntry.get(vs).serverBuilder.metadata != null)
pixelCalibrations = viewSetupToImageEntry.get(vs).serverBuilder.metadata.pixelCalibration;
}
boolean performQuPathRescaling = false;
AffineTransform3D quPathRescaling = new AffineTransform3D();
if (pixelCalibrations != null) {
double scaleX = 1.0;
double scaleY = 1.0;
double scaleZ = 1.0;
Length[] voxSizes = BioFormatsMetaDataHelper.getSeriesVoxelSizeAsLengths(omeMeta, bfIndex);
if (pixelCalibrations.pixelWidth != null) {
MinimalQuPathProject.PixelCalibration pc = pixelCalibrations.pixelWidth;
// if (pc.unit.equals("um")) {
if ((voxSizes[0] != null) && (voxSizes[0].value(UNITS.MICROMETER) != null)) {
logger.debug("xVox size = " + pc.value + " micrometer");
scaleX = pc.value / voxSizes[0].value(UNITS.MICROMETER).doubleValue();
} else {
Length defaultxPix = new Length(1, opener.u);
scaleX = pc.value / defaultxPix.value(UNITS.MICROMETER).doubleValue();
logger.debug("rescaling x");
}
/*} else {
logger.warn("Unrecognized unit in QuPath project: "+pc.unit);
}*/
}
if (pixelCalibrations.pixelHeight != null) {
MinimalQuPathProject.PixelCalibration pc = pixelCalibrations.pixelHeight;
// if (pc.unit.equals("um")) {
if ((voxSizes[1] != null) && (voxSizes[1].value(UNITS.MICROMETER) != null)) {
scaleY = pc.value / voxSizes[1].value(UNITS.MICROMETER).doubleValue();
} else {
Length defaultxPix = new Length(1, opener.u);
scaleY = pc.value / defaultxPix.value(UNITS.MICROMETER).doubleValue();
logger.debug("rescaling y");
}
/*} else {
logger.warn("Unrecognized unit in QuPath project: "+pc.unit);
}*/
}
if (pixelCalibrations.zSpacing != null) {
MinimalQuPathProject.PixelCalibration pc = pixelCalibrations.zSpacing;
// if (pc.unit.equals("um")) { problem with micrometer character
if ((voxSizes[2] != null) && (voxSizes[2].value(UNITS.MICROMETER) != null)) {
scaleZ = pc.value / voxSizes[2].value(UNITS.MICROMETER).doubleValue();
} else {
if ((voxSizes[2] != null)) {
} else {
logger.warn("Null Z voxel size");
}
// logger.warn("Null Z voxel size");
}
/*} else {
logger.warn("Unrecognized unit in QuPath project: "+pc.unit);
}*/
}
logger.debug("ScaleX: " + scaleX + " scaleY:" + scaleY + " scaleZ:" + scaleZ);
if ((Math.abs(scaleX - 1.0) > 0.0001) || (Math.abs(scaleY - 1.0) > 0.0001) || (Math.abs(scaleZ - 1.0) > 0.0001)) {
logger.debug("Perform QuPath rescaling");
quPathRescaling.scale(scaleX, scaleY, scaleZ);
double oX = affine.get(0, 3);
double oY = affine.get(1, 3);
double oZ = affine.get(2, 3);
affine.preConcatenate(quPathRescaling);
affine.set(oX, 0, 3);
affine.set(oY, 1, 3);
affine.set(oZ, 2, 3);
}
}
logger.debug("ViewSetup : " + vs + " append view registrations ");
timePoints.forEach(iTp -> {
if (iTp.getId() < nTimepoints) {
registrations.add(new ViewRegistration(iTp.getId(), vs, affine));
} else {
missingViews.add(new ViewId(iTp.getId(), vs));
}
});
}
// Cleaning opened readers
cachedReaders.values().forEach(reader -> {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
});
SequenceDescription sd = new SequenceDescription(new TimePoints(timePoints), viewSetups, null, new MissingViews(missingViews));
sd.setImgLoader(new QuPathImageLoader(quPathProject, openerModel, sd, openerModel.nFetcherThread, openerModel.numPriorities));
final SpimData spimData = new SpimData(null, sd, new ViewRegistrations(registrations));
return spimData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of ch.epfl.biop.bdv.bioformats.bioformatssource.BioFormatsBdvOpener in project bigdataviewer-biop-tools by BIOP.
the class ReorderDatasetCommand method run.
@Override
public void run() {
if (xmlout.exists()) {
IJ.error("The output file already exist! Skipping execution");
} else {
try {
BioFormatsBdvOpener opener = BioFormatsConvertFilesToSpimData.getDefaultOpener(file.getAbsolutePath()).micrometer();
IFormatReader reader = opener.getNewReader();
// .getSeriesRootTransform()
Length[] voxSizes = BioFormatsMetaDataHelper.getSeriesVoxelSizeAsLengths((IMetadata) reader.getMetadataStore(), 0);
double pixSizeXYMicrometer = voxSizes[0].value(UNITS.MICROMETER).doubleValue();
double scalingForBigStitcher = 1 / pixSizeXYMicrometer;
reader.close();
AbstractSpimData<?> asd = BioFormatsConvertFilesToSpimData.getSpimData(opener.voxSizeReferenceFrameLength(new Length(1, UNITS.MICROMETER)).positionReferenceFrameLength(new Length(1, UNITS.MICROMETER)));
String intermediateXml = FilenameUtils.removeExtension(xmlout.getAbsolutePath()) + "_nonreordered.xml";
System.out.println(intermediateXml);
// Remove display settings attributes because this causes issues with BigStitcher
SpimDataHelper.removeEntities(asd, Displaysettings.class, FileIndex.class);
// Save non reordered dataset
asd.setBasePath((new File(intermediateXml)).getParentFile());
if (asd instanceof SpimData) {
(new XmlIoSpimData()).save((SpimData) asd, intermediateXml);
} else if (asd instanceof SpimDataMinimal) {
(new XmlIoSpimDataMinimal()).save((SpimDataMinimal) asd, FilenameUtils.getName(intermediateXml));
}
// Creates reordered dataset
LifReOrdered kd = new LifReOrdered(intermediateXml, nTiles, nChannels);
kd.initialize();
AbstractSpimData reshuffled = kd.constructSpimData();
// TODO TOFIX
reshuffled.setBasePath(new File(xmlout.getAbsolutePath()).getParentFile());
new XmlIoSpimData().save((SpimData) reshuffled, xmlout.getAbsolutePath());
SpimDataHelper.scale(reshuffled, "BigStitcher Scaling", scalingForBigStitcher);
String bigstitcherXml = FilenameUtils.removeExtension(xmlout.getAbsolutePath()) + "_bigstitcher.xml";
new XmlIoSpimData().save((SpimData) reshuffled, bigstitcherXml);
IJ.log("- Dataset created - " + intermediateXml);
IJ.log("- Reordered Dataset created - " + xmlout.getAbsolutePath());
IJ.log("- Reordered Dataset created, rescaled for BigStitched - " + bigstitcherXml);
IJ.log("Done!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of ch.epfl.biop.bdv.bioformats.bioformatssource.BioFormatsBdvOpener in project bigdataviewer-biop-tools by BIOP.
the class XmlIoQuPathImgLoader method fromXml.
@Override
public QuPathImageLoader fromXml(Element elem, File basePath, AbstractSequenceDescription<?, ?, ?> sequenceDescription) {
try {
final int numFetcherThreads = XmlHelpers.getInt(elem, CACHE_NUM_FETCHER);
final int numPriorities = XmlHelpers.getInt(elem, CACHE_NUM_PRIORITIES);
String openerClassName = XmlHelpers.getText(elem, OPENER_CLASS_TAG);
if (!openerClassName.equals(BioFormatsBdvOpener.class.getName())) {
throw new UnsupportedOperationException("Error class " + openerClassName + " not recognized.");
}
Gson gson = new Gson();
String jsonInString = XmlHelpers.getText(elem, OPENER_MODEL_TAG);
BioFormatsBdvOpener modelOpener = gson.fromJson(jsonInString, BioFormatsBdvOpener.class);
// , Paths.get(imgLoader.getProjectURI()).toString());
String qupathProjectUri = XmlHelpers.getText(elem, QUPATH_PROJECT_TAG);
URI qpProjURI = (new Gson()).fromJson(qupathProjectUri, URI.class);
return new QuPathImageLoader(qpProjURI, modelOpener, sequenceDescription, numFetcherThreads, numPriorities);
} catch (final Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
use of ch.epfl.biop.bdv.bioformats.bioformatssource.BioFormatsBdvOpener in project bigdataviewer-biop-tools by BIOP.
the class FileToBigStitcherDatasetCommand method run.
@Override
public void run() {
if (xmlout.exists()) {
IJ.error("The output file already exist! Skipping execution");
return;
}
try {
BioFormatsBdvOpener opener = BioFormatsConvertFilesToSpimData.getDefaultOpener(file.getAbsolutePath()).micrometer();
IFormatReader reader = opener.getNewReader();
// .getSeriesRootTransform()
Length[] voxSizes = BioFormatsMetaDataHelper.getSeriesVoxelSizeAsLengths((IMetadata) reader.getMetadataStore(), 0);
double pixSizeXYMicrometer = voxSizes[0].value(UNITS.MICROMETER).doubleValue();
double scalingForBigStitcher = 1 / pixSizeXYMicrometer;
reader.close();
AbstractSpimData<?> asd = BioFormatsConvertFilesToSpimData.getSpimData(opener.voxSizeReferenceFrameLength(new Length(1, UNITS.MICROMETER)).positionReferenceFrameLength(new Length(1, UNITS.MICROMETER)));
// Remove display settings attributes because this causes issues with BigStitcher
SpimDataHelper.removeEntities(asd, Displaysettings.class, FileIndex.class);
// Scaling such as size of one pixel = 1
SpimDataHelper.scale(asd, "BigStitcher Scaling", scalingForBigStitcher);
// TODO TOFIX
asd.setBasePath(new File(xmlout.getAbsolutePath()).getParentFile());
new XmlIoSpimData().save((SpimData) asd, xmlout.getAbsolutePath());
IJ.log("- Done! Dataset created - " + xmlout.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations