use of org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser in project bigwarp by saalfeldlab.
the class BigWarpInit method loadN5Source.
public static Source<?> loadN5Source(final String n5Root, final String n5Dataset) {
final N5Reader n5;
try {
n5 = new N5Factory().openReader(n5Root);
} catch (IOException e) {
e.printStackTrace();
return null;
}
final N5MetadataParser<?>[] PARSERS = new N5MetadataParser[] { new ImagePlusLegacyMetadataParser(), new N5CosemMetadataParser(), new N5SingleScaleMetadataParser(), new CanonicalMetadataParser(), new N5GenericSingleScaleMetadataParser() };
final N5MetadataParser<?>[] GROUP_PARSERS = new N5MetadataParser[] { new N5CosemMultiScaleMetadata.CosemMultiScaleParser(), new N5ViewerMultiscaleMetadataParser(), new CanonicalMetadataParser() };
N5Metadata meta = null;
try {
final N5DatasetDiscoverer discoverer = new N5DatasetDiscoverer(n5, N5DatasetDiscoverer.fromParsers(PARSERS), N5DatasetDiscoverer.fromParsers(GROUP_PARSERS));
final N5TreeNode node = discoverer.discoverAndParseRecursive(n5Dataset);
meta = node.getMetadata();
} catch (IOException e) {
}
if (meta instanceof MultiscaleMetadata) {
return openAsSourceMulti(n5, (MultiscaleMetadata<?>) meta, true);
} else {
return openAsSource(n5, meta, true);
}
}
use of org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser in project mars-fx by duderstadt-lab.
the class BdvViewTable method buildBdvTableIndex.
protected BorderPane buildBdvTableIndex() {
TableView<MarsBdvSource> bdvTable = new TableView<MarsBdvSource>();
addBdvSourceNameField = new CustomTextField();
TableColumn<MarsBdvSource, String> typeColumn = new TableColumn<>();
typeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
typeColumn.setCellValueFactory(bdvSource -> new ReadOnlyObjectWrapper<>((bdvSource.getValue().isN5()) ? "N5" : "HD5"));
typeColumn.setSortable(false);
typeColumn.setEditable(false);
typeColumn.setPrefWidth(40);
typeColumn.setMinWidth(40);
typeColumn.setStyle("-fx-alignment: CENTER-LEFT;");
bdvTable.getColumns().add(typeColumn);
TableColumn<MarsBdvSource, MarsBdvSource> deleteColumn = new TableColumn<>();
deleteColumn.setPrefWidth(30);
deleteColumn.setMinWidth(30);
deleteColumn.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()));
deleteColumn.setCellFactory(param -> new TableCell<MarsBdvSource, MarsBdvSource>() {
private final Button removeButton = new Button();
@Override
protected void updateItem(MarsBdvSource pRow, boolean empty) {
super.updateItem(pRow, empty);
if (pRow == null) {
setGraphic(null);
return;
}
removeButton.setGraphic(FontAwesomeIconFactory.get().createIcon(de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.MINUS, "1.0em"));
removeButton.setCenterShape(true);
removeButton.setStyle("-fx-background-radius: 5em; " + "-fx-min-width: 18px; " + "-fx-min-height: 18px; " + "-fx-max-width: 18px; " + "-fx-max-height: 18px;");
setGraphic(removeButton);
removeButton.setOnAction(e -> {
marsImageMetadata.removeBdvSource(pRow.getName());
loadBdvSources();
});
}
});
deleteColumn.setStyle("-fx-alignment: CENTER;");
deleteColumn.setSortable(false);
bdvTable.getColumns().add(deleteColumn);
TableColumn<MarsBdvSource, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellFactory(column -> EditCell.createStringEditCell());
nameColumn.setOnEditCommit(event -> {
String newBdvName = event.getNewValue();
if (!marsImageMetadata.hasBdvSource(newBdvName)) {
MarsBdvSource bdvSource = event.getRowValue();
String oldName = bdvSource.getName();
bdvSource.setName(newBdvName);
// We need to rebuild the map to maintain the order in the table
List<MarsBdvSource> bdvSourceList = new ArrayList<MarsBdvSource>();
for (String key : marsImageMetadata.getBdvSourceNames()) {
if (key.equals(oldName))
bdvSourceList.add(bdvSource);
else
bdvSourceList.add(marsImageMetadata.getBdvSource(key));
}
marsImageMetadata.removeAllBdvSources();
for (MarsBdvSource item : bdvSourceList) marsImageMetadata.putBdvSource(item);
} else {
((MarsBdvSource) event.getTableView().getItems().get(event.getTablePosition().getRow())).setName(event.getOldValue());
bdvTable.refresh();
}
});
nameColumn.setCellValueFactory(bdvSource -> new ReadOnlyObjectWrapper<>(bdvSource.getValue().getName()));
nameColumn.setSortable(false);
nameColumn.setPrefWidth(100);
nameColumn.setMinWidth(100);
nameColumn.setStyle("-fx-alignment: CENTER-LEFT;");
bdvTable.getColumns().add(nameColumn);
bdvTable.setItems(bdvRowList);
bdvTable.setEditable(true);
bdvIndexTableListener = new ChangeListener<MarsBdvSource>() {
public void changed(ObservableValue<? extends MarsBdvSource> observable, MarsBdvSource oldMarsBdvSource, MarsBdvSource newMarsBdvSource) {
if (newMarsBdvSource != null)
bdvSourceOptionsPane.setMarsBdvSource(newMarsBdvSource);
else
bdvSourceOptionsPane.setMarsBdvSource(null);
}
};
bdvTable.getSelectionModel().selectedItemProperty().addListener(bdvIndexTableListener);
Button addButton = new Button();
addButton.setGraphic(FontAwesomeIconFactory.get().createIcon(de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.PLUS, "1.0em"));
addButton.setCenterShape(true);
addButton.setCursor(Cursor.DEFAULT);
addButton.setStyle("-fx-background-radius: 5em; " + "-fx-min-width: 18px; " + "-fx-min-height: 18px; " + "-fx-max-width: 18px; " + "-fx-max-height: 18px;");
addButton.setOnAction(e -> {
if (!addBdvSourceNameField.getText().equals("") && !marsImageMetadata.hasBdvSource(addBdvSourceNameField.getText())) {
MarsBdvSource bdvSource = new MarsBdvSource(addBdvSourceNameField.getText());
switch(this.buttonType) {
case 0:
bdvSource.setN5(true);
break;
case 1:
bdvSource.setN5(false);
break;
}
if (bdvSource.isN5()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DatasetSelectorDialog selectionDialog = new DatasetSelectorDialog(new N5ViewerReaderFun(), new N5BasePathFun(), System.getProperty(// no
"user.home"), // no
new N5MetadataParser[] {}, // parsers
new N5MetadataParser[] { new ImagePlusLegacyMetadataParser(), new N5CosemMetadataParser(), new N5SingleScaleMetadataParser(), new CanonicalMetadataParser(), new N5GenericSingleScaleMetadataParser() });
selectionDialog.setVirtualOption(false);
selectionDialog.setCropOption(false);
selectionDialog.setTreeRenderer(new N5DatasetTreeCellRenderer(true));
// Prevents NullPointerException
selectionDialog.setContainerPathUpdateCallback(x -> {
});
final Consumer<DataSelection> callback = (DataSelection dataSelection) -> {
Platform.runLater(new Runnable() {
@Override
public void run() {
bdvSource.setPath(selectionDialog.getN5RootPath());
bdvSource.setN5Dataset(dataSelection.metadata.get(0).getPath());
bdvSource.setProperty("info", getDatasetInfo(((N5DatasetMetadata) dataSelection.metadata.get(0)).getAttributes()));
marsImageMetadata.putBdvSource(bdvSource);
loadBdvSources();
}
});
};
selectionDialog.run(callback);
}
});
} else {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select xml");
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().add(new ExtensionFilter("xml file", "*.xml"));
File path = fileChooser.showOpenDialog(getNode().getScene().getWindow());
if (path != null) {
bdvSource.setPath(path.getAbsolutePath());
marsImageMetadata.putBdvSource(bdvSource);
loadBdvSources();
}
}
}
});
addBdvSourceNameField.textProperty().addListener((observable, oldValue, newValue) -> {
if (addBdvSourceNameField.getText().isEmpty()) {
addBdvSourceNameField.setRight(new Label(""));
} else {
addBdvSourceNameField.setRight(addButton);
}
});
addBdvSourceNameField.setStyle("-fx-background-radius: 2em; ");
typeButton = new Button();
typeButton.setText("N5");
typeButton.setCenterShape(true);
typeButton.setStyle("-fx-background-radius: 2em; " + "-fx-min-width: 60px; " + "-fx-min-height: 30px; " + "-fx-max-width: 60px; " + "-fx-max-height: 30px;");
typeButton.setOnAction(e -> {
buttonType++;
if (buttonType > 1)
buttonType = 0;
switch(buttonType) {
case 0:
typeButton.setText("N5");
typeButton.setGraphic(null);
break;
case 1:
typeButton.setText("HD5");
typeButton.setGraphic(null);
break;
}
});
BorderPane bomttomPane = new BorderPane();
bomttomPane.setCenter(addBdvSourceNameField);
bomttomPane.setLeft(typeButton);
BorderPane bdvTableIndex = new BorderPane();
bdvTableIndex.setCenter(bdvTable);
bdvTableIndex.setBottom(bomttomPane);
BorderPane.setMargin(addBdvSourceNameField, new Insets(5));
BorderPane.setMargin(typeButton, new Insets(5));
return bdvTableIndex;
}
use of org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser in project bigwarp by saalfeldlab.
the class ApplyBigwarpPlugin method runN5Export.
public static <S, T extends NativeType<T> & NumericType<T>> void runN5Export(final BigWarpData<S> data, final List<SourceAndConverter<S>> sources, final String fieldOfViewOption, final Interval outputInterval, final Interpolation interp, final double[] offset, final double[] resolution, final String unit, final ProgressWriter progressWriter, final WriteDestinationOptions writeOpts, final ExecutorService exec) {
final int nd = BigWarp.detectNumDims(data.sources);
// setup n5 parameters
final String dataset = writeOpts.n5Dataset;
final int[] blockSize = writeOpts.blockSize;
final Compression compression = writeOpts.compression;
if (dataset == null || dataset.isEmpty()) {
return;
}
N5Writer n5;
try {
n5 = new N5Factory().openWriter(writeOpts.pathOrN5Root);
} catch (IOException e1) {
e1.printStackTrace();
return;
}
// build metadata
final String[] axes = nd == 2 ? new String[] { "y", "x" } : new String[] { "z", "y", "x" };
final String[] units = nd == 2 ? new String[] { unit, unit } : new String[] { unit, unit, unit };
final N5CosemMetadata metadata = new N5CosemMetadata("", new N5CosemMetadata.CosemTransform(axes, resolution, offset, units), null);
N5CosemMetadataParser parser = new N5CosemMetadataParser();
// setup physical to pixel transform
final AffineTransform3D resolutionTransform = new AffineTransform3D();
resolutionTransform.set(resolution[0], 0, 0);
resolutionTransform.set(resolution[1], 1, 1);
if (resolution.length > 2)
resolutionTransform.set(resolution[2], 2, 2);
final AffineTransform3D offsetTransform = new AffineTransform3D();
offsetTransform.set(offset[0], 0, 3);
offsetTransform.set(offset[1], 1, 3);
if (resolution.length > 2)
offsetTransform.set(offset[2], 2, 3);
final AffineTransform3D pixelRenderToPhysical = new AffineTransform3D();
pixelRenderToPhysical.concatenate(resolutionTransform);
pixelRenderToPhysical.concatenate(offsetTransform);
// render and write
final int N = data.movingSourceIndices.length;
for (int i = 0; i < N; i++) {
final int movingSourceIndex = data.movingSourceIndices[i];
@SuppressWarnings("unchecked") final RealRandomAccessible<T> raiRaw = (RealRandomAccessible<T>) sources.get(movingSourceIndex).getSpimSource().getInterpolatedSource(0, 0, interp);
// to pixel space
final AffineRandomAccessible<T, AffineGet> rai = RealViews.affine(raiRaw, pixelRenderToPhysical.inverse());
final IntervalView<T> img = Views.interval(Views.raster(rai), Intervals.zeroMin(outputInterval));
final String srcName = data.sources.get(data.movingSourceIndices[i]).getSpimSource().getName();
String destDataset = dataset;
if (N > 1)
destDataset = dataset + String.format("/%s", srcName.replace(" ", "_"));
RandomAccessibleInterval<T> imgToWrite;
if (nd == 2)
imgToWrite = Views.hyperSlice(img, 2, 0);
else
imgToWrite = img;
try {
N5Utils.save(imgToWrite, n5, destDataset, blockSize, compression, exec);
if (parser != null && metadata != null)
parser.writeMetadata(metadata, n5, destDataset);
} catch (Exception e) {
e.printStackTrace();
}
progressWriter.setProgress((i + 1) / ((double) N));
}
progressWriter.setProgress(1.0);
}
Aggregations