Beispiel #1
0
class TestCollectionResource(unittest.TestCase):
    def setUp(self):
        self.coll = CollectionResource("foo")

    def test_not_valid_volume(self):
        self.assertFalse(self.coll.valid_volume())

    def test_get_route(self):
        self.assertEqual(self.coll.name, self.coll.get_route())

    def test_get_list_route(self):
        self.assertEqual("foo/", self.coll.get_list_route())
Beispiel #2
0
 def setUp(self):
     self.resource = CollectionResource('coll1')
     self.chanResource = ChannelResource(
         'chan1', 'coll1', 'exp1', 'image', 'null descr', 0, 'uint8', 0)
     self.annoResource = ChannelResource(
         'annChan', 'coll1', 'exp1', 'annotation', 'null descr',
         0, 'uint64', 0, sources=['chan1'])
     self.test_project = ProjectImpl()
     self.test_meta = MetadataImpl()
     self.test_volume = VolumeImpl()
     self.url_prefix = 'https://api.theboss.io'
Beispiel #3
0
 def __init__(self,
              resource: Union[BossResource, str],
              remote: BossRemote = None):
     self._remote = remote or BossRemote(_DEFAULT_BOSS_OPTIONS)
     if isinstance(resource, str):
         resource = resource.split("://")[-1]
         path_items = resource.split("/")
         if len(path_items) == 1:
             self._resource = self._remote.get_project(
                 CollectionResource(path_items[0]))
         elif len(path_items) == 2:
             self._resource = self._remote.get_project(
                 ExperimentResource(path_items[1], path_items[0]))
         elif len(path_items) == 3:
             self._resource = self._remote.get_project(
                 ChannelResource(path_items[2], path_items[0],
                                 path_items[1]))
         else:
             raise ValueError(f"Invalid resource path: {resource}")
     else:
         self._resource = resource
Beispiel #4
0
 def setUpClass(cls):
     '''Initialized once, before any class tests are run'''
     boss = cls.boss = Remote()
     fixtures = {}
     fixtures['coordframe'] = CoordinateFrameResource(COORD_FRAME)
     fixtures['collection'] = CollectionResource(COLLECTION)
     fixtures['experiment'] = ExperimentResource(EXPERIMENT,
                                                 COLLECTION,
                                                 coord_frame=COORD_FRAME)
     fixtures['channel'] = ChannelResource(CHANNEL, COLLECTION, EXPERIMENT,
                                           'image')
     fixtures['ann_channel'] = ChannelResource(ANN_CHANNEL,
                                               COLLECTION,
                                               EXPERIMENT,
                                               'annotation',
                                               sources=[CHANNEL])
     cls.fixtures = fixtures
     for resource_name in [
             'coordframe', 'collection', 'experiment', 'channel',
             'ann_channel'
     ]:
         boss.create_project(fixtures[resource_name])
Beispiel #5
0
 def setUp(self):
     self.coll = CollectionResource("foo")
Beispiel #6
0
    def __init__(
        self,
        channel: Union[ChannelResource, Tuple, str],
        resolution: int = 0,
        volume_provider: VolumeProvider = None,
        axis_order: str = AxisOrder.ZYX,
        create_new: bool = False,
        description: Optional[str] = None,
        dtype: Optional[str] = None,
        extents: Optional[Tuple[int, int, int]] = None,
        voxel_size: Optional[Tuple[int, int, int]] = None,
        voxel_unit: Optional[str] = None,
        downsample_levels: int = 6,
        downsample_method: Optional[str] = "anisotropic",
        coordinate_frame_name: Optional[str] = None,
        coordinate_frame_desc: Optional[str] = None,
        collection_desc: Optional[str] = None,
        experiment_desc: Optional[str] = None,
        source_channel: Optional[str] = None,
        boss_config: Optional[dict] = None,
    ) -> None:
        """
        Construct a new intern-backed array.

        Arguments:
            channel (intern.resource.boss.ChannelResource): The channel from
                which data will be downloaded.
            resolution (int: 0): The native resolution or MIP to use
            volume_provider (VolumeProvider): The remote-like to use
            axis_order (str = AxisOrder.ZYX): The axis-ordering to use for data
                cutouts. Defaults to ZYX. DOES NOT affect the `voxel_size` or
                `extents` arguments to this constructor.
            create_new (bool: False): Whether to create new Resources if they
                do not exist. Does not work with public token.
            dtype (str): Only required if `create_new = True`. Specifies the
                numpy-style datatype for this new dataset (e.g. "uint8").
            description (str): Only required if `create_new = True`. Sets the
                description for the newly-created collection, experiment,
                channel, and coordframe resources.
            extents: Optional[Tuple[int, int, int]]: Only required if
                `create_new = True`. Specifies the total dataset extents of
                this new dataset, in ZYX order.
            voxel_size: Optional[Tuple[int, int, int]]: Only required if
                `create_new = True`. Specifies the voxel dimensions of this new
                dataset, in ZYX order.
            voxel_unit: Optional[str]: Only required if `create_new = True`.
                Specifies the voxel-dimension unit. For example, "nanometers".
            downsample_levels (int: 6): The number of downsample levels.
            downsample_method (Optional[str]): The type of downsample to use.
                If unset, defaults to 'anisotropic'.
            coordinate_frame_name (Optional[str]): If set, the name to use for
                the newly created coordinate frame. If not set, the name of the
                coordinate frame will be chosen automatically.
            coordinate_frame_desc (Optional[str]): If set, the description text
                to use for the newly created coordinate frame. If not set, the
                description will be chosen automatically.
            collection_desc (Optional[str]): The description text to use for a
                newly created collection. If not set, the description will be
                chosen automatically.
            experiment_desc (Optional[str]): The description text to use for a
                newly created experiment. If not set, the description will be
                chosen automatically.
            source_channel (Optional[str]): The channel to use as the source
                for this new channel, if `create_new` is True and this is
                going to be an annotation channel (dtype!=uint8).
            boss_config (Optional[dict]): The BossRemote configuration dict to
                use in order to authenticate with a BossDB remote. This option
                is mutually exclusive with the VolumeProvider configuration. If
                the `volume_provider` arg is set, this will be ignored.

        """
        self.axis_order = axis_order

        # Handle inferring the remote from the channel:
        volume_provider = volume_provider or _infer_volume_provider(channel)
        if volume_provider is None:
            if boss_config:
                volume_provider = _BossDBVolumeProvider(
                    BossRemote(boss_config))
            else:
                volume_provider = _BossDBVolumeProvider()

        # Handle custom Remote:
        self.volume_provider = volume_provider

        if create_new:

            if self.volume_provider.get_vp_type() != "bossdb":
                raise ValueError(
                    "Creating new resources with a non-bossdb volume provider is not currently supported."
                )

            # We'll need at least `extents` and `voxel_size`.
            description = description or "Created with intern"
            dtype = dtype or "uint8"

            if extents is None:
                raise ValueError(
                    "If `create_new` is True, you must specify the extents of the new coordinate frame as a [x, y, z]."
                )
            if voxel_size is None:
                raise ValueError(
                    "If `create_new` is True, you must specify the voxel_size of the new coordinate frame as a [x, y, z]."
                )

            uri = _parse_bossdb_uri(channel)

            # create collection if it doesn't exist:
            try:
                # Try to get an existing collection:
                collection = self.volume_provider.get_project(
                    CollectionResource(uri.collection))
            except:
                # Create the collection:
                collection = CollectionResource(uri.collection,
                                                description=collection_desc
                                                or description)
                self.volume_provider.create_project(collection)

            # create coordframe if it doesn't exist:
            try:
                # Try to get an existing coordframe:
                coordframe = self.volume_provider.get_project(
                    CoordinateFrameResource(
                        coordinate_frame_name
                        or f"CF_{uri.collection}_{uri.experiment}"))
            except:
                # Default to nanometers if a voxel unit isn't provided
                voxel_unit = voxel_unit or "nanometers"
                # Create the coordframe:
                coordframe = CoordinateFrameResource(
                    coordinate_frame_name
                    or f"CF_{uri.collection}_{uri.experiment}",
                    description=coordinate_frame_desc or description,
                    x_start=0,
                    y_start=0,
                    z_start=0,
                    x_stop=extents[2],
                    y_stop=extents[1],
                    z_stop=extents[0],
                    x_voxel_size=voxel_size[2],
                    y_voxel_size=voxel_size[1],
                    z_voxel_size=voxel_size[0],
                    voxel_unit=voxel_unit,
                )
                self.volume_provider.create_project(coordframe)

            # create experiment if it doesn't exist:
            try:
                # Try to get an existing experiment:
                experiment = self.volume_provider.get_project(
                    ExperimentResource(uri.experiment, uri.collection))
            except:
                # Create the experiment:
                experiment = ExperimentResource(
                    uri.experiment,
                    uri.collection,
                    description=experiment_desc or description,
                    coord_frame=coordframe.name,
                    num_hierarchy_levels=downsample_levels,
                    hierarchy_method=downsample_method,
                )
                self.volume_provider.create_project(experiment)

            # create channel if it doesn't exist:
            try:
                # Try to get an existing channel:
                channel = self.volume_provider.get_project(
                    ChannelResource(uri.channel, uri.collection,
                                    uri.experiment))
            except:
                # Create the channel:
                channel = ChannelResource(
                    uri.channel,
                    uri.collection,
                    uri.experiment,
                    description=description,
                    type="image"
                    if dtype in ["uint8", "uint16"] else "annotation",
                    datatype=dtype,
                    sources=[source_channel] if source_channel else [],
                )
                self.volume_provider.create_project(channel)

        self.resolution = resolution
        # If the channel is set as a Resource, then use that resource.
        if isinstance(channel, ChannelResource):
            self._channel = channel
        # If it is set as a string, then parse the channel and generate an
        # intern.Resource from a bossDB URI.
        else:  # if isinstance(channel, str):
            uri = {
                "BossDB": _parse_bossdb_uri,
                "CloudVolumeOpenData": _parse_cloudvolume_uri,
            }[self.volume_provider.get_vp_type()](channel)
            self.resolution = (uri.resolution if not (uri.resolution is None)
                               else self.resolution)
            self._channel = self.volume_provider.get_channel(
                uri.channel, uri.collection, uri.experiment)

        # Set col/exp/chan based upon the channel or URI provided.
        self.collection_name = self._channel.coll_name
        self.experiment_name = self._channel.exp_name
        self.channel_name = self._channel.name

        # Create a pointer to the metadata for the channel.
        self._channel_metadata = Metadata(self._channel)