-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...atastore/app/com/scalableminds/webknossos/datastore/explore/NeuroglancerUriExplorer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.scalableminds.webknossos.datastore.explore | ||
|
||
import com.scalableminds.util.geometry.{Vec3Double, Vec3Int} | ||
import com.scalableminds.util.tools.Fox | ||
import com.scalableminds.webknossos.datastore.dataformats.n5.{N5DataLayer, N5SegmentationLayer} | ||
import com.scalableminds.webknossos.datastore.dataformats.precomputed.{ | ||
PrecomputedDataLayer, | ||
PrecomputedSegmentationLayer | ||
} | ||
import com.scalableminds.webknossos.datastore.dataformats.zarr.{ZarrDataLayer, ZarrSegmentationLayer} | ||
import com.scalableminds.webknossos.datastore.dataformats.zarr3.{Zarr3DataLayer, Zarr3SegmentationLayer} | ||
import com.scalableminds.webknossos.datastore.datavault.VaultPath | ||
import com.scalableminds.webknossos.datastore.models.datasource.LayerViewConfiguration.LayerViewConfiguration | ||
import com.scalableminds.webknossos.datastore.models.datasource.{DataLayer, LayerViewConfiguration} | ||
import com.scalableminds.webknossos.datastore.storage.{DataVaultService, RemoteSourceDescriptor} | ||
import play.api.libs.json._ | ||
|
||
import java.net.URI | ||
import javax.inject.Inject | ||
import scala.concurrent.ExecutionContext | ||
|
||
class NeuroglancerUriExplorer @Inject()(dataVaultService: DataVaultService, | ||
exploreLayerService: ExploreLayerService, | ||
implicit val ec: ExecutionContext) | ||
extends RemoteLayerExplorer { | ||
override def name: String = "Neuroglancer URI Explorer" | ||
|
||
override def explore(remotePath: VaultPath, credentialId: Option[String]): Fox[List[(DataLayer, Vec3Double)]] = | ||
for { | ||
_ <- Fox.successful(()) | ||
spec <- Json | ||
.parse(remotePath.toUri.getFragment.drop(1)) | ||
.validate[JsObject] | ||
.toFox ?~> "Did not find JSON object in URI" | ||
layerSpecs <- (spec \ "layers").validate[JsArray].toFox | ||
_ <- Fox.bool2Fox(credentialId.isEmpty) ~> "Neuroglancer URI Explorer does not support credentials" | ||
exploredLayers = layerSpecs.value.map(exploreNeuroglancerLayer).toList | ||
layerLists <- Fox.combined(exploredLayers) | ||
layers = layerLists.flatten | ||
renamedLayers = exploreLayerService.makeLayerNamesUnique(layers.map(_._1)) | ||
} yield renamedLayers.zip(layers.map(_._2)) | ||
|
||
private def exploreNeuroglancerLayer(layerSpec: JsValue): Fox[List[(DataLayer, Vec3Double)]] = | ||
for { | ||
_ <- Fox.successful(()) | ||
obj <- layerSpec.validate[JsObject].toFox | ||
source <- (obj \ "source").validate[JsString].toFox | ||
layerType = new URI(source.value).getScheme | ||
sourceURI = new URI(source.value.substring(f"$layerType://".length)) | ||
name <- (obj \ "name").validate[JsString].toFox | ||
remoteSourceDescriptor = RemoteSourceDescriptor(sourceURI, None) | ||
remotePath <- dataVaultService.getVaultPath(remoteSourceDescriptor) ?~> "dataVault.setup.failed" | ||
viewConfiguration = getViewConfig(obj) | ||
layer <- exploreLayer(layerType, remotePath, name.value) | ||
layerWithViewConfiguration <- assignViewConfiguration(layer, viewConfiguration) | ||
} yield layerWithViewConfiguration | ||
|
||
private def exploreLayer(layerType: String, remotePath: VaultPath, name: String): Fox[List[(DataLayer, Vec3Double)]] = | ||
layerType match { | ||
case "n5" => | ||
Fox.firstSuccess( | ||
Seq(new N5ArrayExplorer().explore(remotePath, None), new N5MultiscalesExplorer().explore(remotePath, None))) | ||
case "precomputed" => new PrecomputedExplorer().explore(remotePath, None) | ||
case "zarr" | "zarr2" => | ||
Fox.firstSuccess( | ||
Seq(new NgffExplorer().explore(remotePath, None), | ||
new ZarrArrayExplorer(Vec3Int.ones, ec).explore(remotePath, None))) | ||
case "zarr3" => new Zarr3ArrayExplorer().explore(remotePath, None) | ||
case _ => Fox.failure(f"Can not explore layer of $layerType type") | ||
} | ||
|
||
private def getViewConfig(layerSpec: JsObject): LayerViewConfiguration = { | ||
val opacity = (layerSpec \ "opacity").validate[Double].getOrElse(1.0) | ||
val intensityRange = (layerSpec \ "shaderControls" \ "normalized" \ "range").validate[JsArray].asOpt | ||
val options = Seq("alpha" -> JsNumber(opacity * 100)) ++ intensityRange.map("intensityRange" -> _) | ||
options.toMap | ||
} | ||
|
||
private def assignViewConfiguration( | ||
value: List[(DataLayer, Vec3Double)], | ||
configuration: LayerViewConfiguration.LayerViewConfiguration): Fox[List[(DataLayer, Vec3Double)]] = | ||
for { | ||
_ <- Fox.successful(()) | ||
layers = value.map(_._1) | ||
layersWithViewConfigs = layers.map { | ||
case l: ZarrDataLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: ZarrSegmentationLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: N5DataLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: N5SegmentationLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: PrecomputedDataLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: PrecomputedSegmentationLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: Zarr3DataLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
case l: Zarr3SegmentationLayer => l.copy(defaultViewConfiguration = Some(configuration)) | ||
|
||
} | ||
} yield layersWithViewConfigs.zip(value.map(_._2)) | ||
|
||
} |