Esempio n. 1
0
    def upload_boss_image(self, img, offset):
        shape = Vec(*img.shape[:3])
        offset = Vec(*offset)

        bounds = Bbox(offset, shape + offset)

        if bounds.subvoxel():
            raise exceptions.EmptyRequestException(
                'Requested less than one pixel of volume. {}'.format(bounds))

        x_rng = [bounds.minpt.x, bounds.maxpt.x]
        y_rng = [bounds.minpt.y, bounds.maxpt.y]
        z_rng = [bounds.minpt.z, bounds.maxpt.z]

        layer_type = 'image' if self.layer_type == 'unknown' else self.layer_type

        chan = ChannelResource(
            collection_name=self.path.bucket,
            experiment_name=self.path.dataset,
            name=self.path.layer,  # Channel
            type=layer_type,
            datatype=self.dtype,
        )

        if img.shape[3] == 1:
            img = img.reshape(img.shape[:3])

        rmt = BossRemote(boss_credentials)
        img = img.T
        img = np.asfortranarray(img.astype(self.dtype))

        rmt.create_cutout(chan, self.mip, x_rng, y_rng, z_rng, img)
Esempio n. 2
0
    def __init__(self, **kwargs):
        """
        Create a new RelayStorageManager.

        Arguments:

            block_size: How much data should go in each file
        """
        self.block_size = kwargs.get("block_size", (256, 256, 16))

        if "next_layer" in kwargs:
            self._next = kwargs["next_layer"]
            self.is_terminal = False
        else:
            self.is_terminal = True

        if "boss_remote" in kwargs:
            self.boss_remote = kwargs["boss_remote"]
        elif "upstream_uri" in kwargs:
            self.token = kwargs.get("token", "public")
            self.boss_remote = BossRemote({
                "host":
                kwargs["upstream_uri"],
                "protocol":
                kwargs.get("protocol", "http"),
                "token":
                kwargs.get("token", "public"),
            })
Esempio n. 3
0
    def setup(self, parameters):
        """ Method to load the file for uploading data. Assumes intern token is set via environment variable or config
        default file

        Args:
            parameters (dict): Parameters for the dataset to be processed


        MUST HAVE THE CUSTOM PARAMETERS: "x_offset": offset to apply when querying the Boss
                                         "y_offset": offset to apply when querying the Boss
                                         "z_offset": offset to apply when querying the Boss
                                         "x_tile": size of a tile in x dimension
                                         "y_tile": size of a tile in y dimension
                                         "collection": source collection
                                         "experiment": source experiment
                                         "channel": source channel
                                         "resolution": source resolution

        Returns:
            None
        """
        self.parameters = parameters
        self.remote = BossRemote()
        self.channel = ChannelResource(self.parameters["channel"],
                                       self.parameters["collection"],
                                       self.parameters["experiment"])
        self.channel = self.remote.get_project(self.channel)
Esempio n. 4
0
 def __init__(self, boss: BossRemote = None):
     if boss is None:
         try:
             boss = BossRemote()
         except:
             boss = BossRemote(_DEFAULT_BOSS_OPTIONS)
     self.boss = boss
Esempio n. 5
0
 def test_array_instantiation_with_channel(self):
     """
     Array can be instantiated.
     .
     """
     boss = BossRemote(_DEFAULT_BOSS_OPTIONS)
     _ = array(boss.get_channel("cc", "kasthuri2015", "em"))
Esempio n. 6
0
    def __init__(self,
                 host,
                 token,
                 collection,
                 experiment,
                 requested_channels,
                 x_range=None,
                 y_range=None,
                 z_range=None,
                 resolution=0):

        self._bossRemote = BossRemote({
            'protocol': 'https',
            'host': host,
            'token': token
        })
        self.collection = collection
        self.experiment = experiment
        self.resolution = resolution

        self.channels = self._bossRemote.list_channels(collection, experiment)

        if len(requested_channels) == 0:
            self.requested_channels = self.channels
        else:
            self.requested_channels = requested_channels

        self._get_coord_frame_details()
        # validate range
        #if not self.correct_range(z_range, y_range, x_range):
        #    raise Exception("Error: Inccorect dimension range")

        self.x_range = x_range or [0, self.max_dimensions[2]]
        self.y_range = y_range or [0, self.max_dimensions[1]]
        self.z_range = z_range or [0, self.max_dimensions[0]]
Esempio n. 7
0
def push_tif():
	#	message1 = request.form['channel']
	#if message1:
	#	print(message1)
	col = request.form["collection"]
	exp = request.form["experiment"]
	channel = request.form["channel"]
	dtype = request.form["datatype"]
	host,token = get_host_token()
	x_range = [int(request.form["xstart"]),int(request.form["xstop"])]
	y_range = [int(request.form["ystart"]),int(request.form["ystop"])]
	z_range = [int(request.form["zstart"]),int(request.form["zstop"])]
	
	remote = BossRemote('./neurodata.cfg')
	voxel_size = '1 1 1'
	voxel_unit = 'micrometers'
	
	channel_resource = ChannelResource(channel, col, exp, 'image', '', 0, dtype, 0)
	project = remote.get_project(channel_resource)
	my_file = request.form["file"]	
	my_array = np.array(io.imread('./DATA/'+my_file)).astype(np.uint8)
	
	for z in range(z_range[0],z_range[1]):
		remote.create_cutout(channel_resource, 0, (x_range[0],x_range[1]), (y_range[0],y_range[1]), (z,z+1), my_array[z].reshape(-1,my_array.shape[1],my_array.shape[2]))
	return 'Successfully pushed'
Esempio n. 8
0
    def test_init_with_config_dict(self):
        config = {
            "protocol": "https",
            "host": "api.test.com",
            "token": "asdlsdj2192isja"
        }

        rmt = BossRemote(config)
        actual_prj = dict(rmt._config.items("Default"))
        self.assertEqual('https', actual_prj[CONFIG_PROTOCOL])
        self.assertEqual('api.test.com', actual_prj[CONFIG_HOST])
        self.assertEqual('asdlsdj2192isja', actual_prj[CONFIG_TOKEN])

        actual_meta = dict(rmt._config.items("Default"))
        self.assertEqual('https', actual_meta[CONFIG_PROTOCOL])
        self.assertEqual('api.test.com', actual_meta[CONFIG_HOST])
        self.assertEqual('asdlsdj2192isja', actual_meta[CONFIG_TOKEN])

        actual_vol = dict(rmt._config.items("Default"))
        self.assertEqual('https', actual_vol[CONFIG_PROTOCOL])
        self.assertEqual('api.test.com', actual_vol[CONFIG_HOST])
        self.assertEqual('asdlsdj2192isja', actual_vol[CONFIG_TOKEN])

        rmt.token_metadata = actual_meta[CONFIG_TOKEN]
        rmt.token_volume = actual_vol[CONFIG_TOKEN]
        rmt.token_project = actual_prj[CONFIG_TOKEN]
Esempio n. 9
0
 def setUp(self):
     config = {
         "protocol": "https",
         "host": "test.theboss.io",
         "token": "my_secret"
     }
     self.remote = BossRemote(config)
Esempio n. 10
0
 def test_array_instantiation_dtype(self):
     """
     Array can be instantiated with type.
     .
     """
     boss = BossRemote(_DEFAULT_BOSS_OPTIONS)
     test_array = array(boss.get_channel("cc", "kasthuri2015", "em"))
     self.assertEqual(test_array.dtype, "uint8")
Esempio n. 11
0
 def __init__(self, collection_name):
     """
     Constructor
     """
     self.collection_name = collection_name
     try:
         self.rmt = BossRemote()
     except:
         print('Unexpected Error:', sys.exc_info()[0])
 def __init__(self, host, token, collection, experiment):
     self._bossRemote = BossRemote({'protocol': 'https',
                                    'host': host,
                                    'token': token})
     self.collection = collection
     self.experiment = experiment
     self.channels = self._bossRemote.list_channels(collection, experiment)
     self.channels.remove('empty') #Delete "empty" channel
     self._get_coord_frame_details()
Esempio n. 13
0
 def setup_remote(self):
     if self.ingest_job.boss_config_file:
         return BossRemote(self.ingest_job.boss_config_file)
     else:
         # try to load from environment variable
         token = os.environ['BOSS_TOKEN']
         protocol = 'https'
         host = 'api.boss.neurodata.io'
         config_dict = {'token': token, 'protocol': protocol, 'host': host}
         return BossRemote(config_dict)
Esempio n. 14
0
def delete_channel(remote: BossRemote, channel, coll_name=None, exp_name=None):
    chan_obj = None
    if isinstance(channel, ChannelResource):
        chan_obj = channel
    elif isinstance(channel, str) and coll_name is not None and exp_name is not None:
        chan_name = channel
        chan_obj = remote.get_project(ChannelResource(name=chan_name, experiment_name=exp_name))
    if chan_obj is not None:
        print('Deleting channel "{0}"...'.format(chan_obj.name))
        remote.delete_project(chan_obj)
Esempio n. 15
0
    def fetch_info(self):
        experiment = ExperimentResource(name=self.path.dataset,
                                        collection_name=self.path.bucket)
        rmt = BossRemote(boss_credentials)
        experiment = rmt.get_project(experiment)

        coord_frame = CoordinateFrameResource(name=experiment.coord_frame)
        coord_frame = rmt.get_project(coord_frame)

        channel = ChannelResource(self.path.layer, self.path.bucket,
                                  self.path.dataset)
        channel = rmt.get_project(channel)

        unit_factors = {
            'nanometers': 1,
            'micrometers': 1e3,
            'millimeters': 1e6,
            'centimeters': 1e7,
        }

        unit_factor = unit_factors[coord_frame.voxel_unit]

        cf = coord_frame
        resolution = [cf.x_voxel_size, cf.y_voxel_size, cf.z_voxel_size]
        resolution = [int(round(_)) * unit_factor for _ in resolution]

        bbox = Bbox((cf.x_start, cf.y_start, cf.z_start),
                    (cf.x_stop, cf.y_stop, cf.z_stop))
        bbox.maxpt = bbox.maxpt

        layer_type = 'unknown'
        if 'type' in channel.raw:
            layer_type = channel.raw['type']

        info = CloudVolume.create_new_info(
            num_channels=1,
            layer_type=layer_type,
            data_type=channel.datatype,
            encoding='raw',
            resolution=resolution,
            voxel_offset=bbox.minpt,
            volume_size=bbox.size3(),
            chunk_size=(512, 512, 16),  # fixed in s3 implementation
        )

        each_factor = Vec(2, 2, 1)
        if experiment.hierarchy_method == 'isotropic':
            each_factor = Vec(2, 2, 2)

        factor = each_factor.clone()
        for _ in range(1, experiment.num_hierarchy_levels):
            self.add_scale(factor, info=info)
            factor *= each_factor

        return info
Esempio n. 16
0
def delete_coord_frame(remote: BossRemote, coord_frame):
    frame_obj = None
    # frame_name = None
    if isinstance(coord_frame, CoordinateFrameResource):
        frame_obj = coord_frame
        # frame_name = coord_frame.name
    elif isinstance(coord_frame, str):
        frame_name = coord_frame
        frame_obj = remote.get_project(CoordinateFrameResource(name=frame_name))
    if frame_obj is not None:
        print('Deleting coordinate frame "{0}"...'.format(frame_obj.name))
        remote.delete_project(frame_obj)
Esempio n. 17
0
 def __init__(self, collection_name):
     """
     Constructor:
     Takes the following argument:
     :param collection_name: name of the collection in BOSS
     :type collection_name: string
     """
     self.collection_name = collection_name
     try:
         self.rmt = BossRemote()
     except:
         print('Unexpected Error:', sys.exc_info()[0])
Esempio n. 18
0
 def test_can_retrieve_correct_u64_data(self):
     data = array("bossdb://kasthuri2015/em/3cylneuron_v1")
     boss = BossRemote(_DEFAULT_BOSS_OPTIONS)
     get_cutout_data = boss.get_cutout(
         boss.get_channel("3cylneuron_v1", "kasthuri2015", "em"),
         0,
         [6000, 6200],
         [12000, 12500],
         [923, 924],
     )
     self.assertTrue(
         (get_cutout_data == data[923:924, 12000:12500, 6000:6200]).all()
     )
Esempio n. 19
0
def set_experiment_resource(remote: BossRemote, params: dict) -> ExperimentResource:
    """Use the arguments in the class config to create an experiment resource object"""
    if 'name' not in params:
        params['name'] = 'experiment{0}'.format(hex(round(time.time()))[2:])
    param_names = [str(p.name) for p in inspect.signature(ExperimentResource).parameters.values()]
    filtered_params = {k: v for k, v in list(params.items()) if k in param_names}  # Filter unexpected arguments
    exp_resource = ExperimentResource(**filtered_params)
    if exp_resource.name in remote.list_experiments(exp_resource.coll_name):
        experiment = remote.update_project(exp_resource.name, exp_resource)
        print('Updated experiment {0}'.format(exp_resource.name))
    else:
        experiment = remote.create_project(exp_resource)
        print('Created experiment {0}'.format(exp_resource.name))
    return experiment
Esempio n. 20
0
def set_coordinate_frame_resource(remote: BossRemote, params: dict) -> CoordinateFrameResource:
    """Use the arguments in the class config to create a frame resource object"""
    if 'name' not in params:
        params['name'] = 'frame{0}'.format(hex(round(time.time()))[2:])
    param_names = [str(p.name) for p in inspect.signature(CoordinateFrameResource).parameters.values()]
    filtered_params = {k: v for k, v in list(params.items()) if k in param_names}  # Filter unexpected arguments
    frame_resource = CoordinateFrameResource(**filtered_params)
    if frame_resource.name in remote.list_coordinate_frames():
        coordinate_frame = remote.update_project(frame_resource.name, frame_resource)
        print('Updated frame {0}'.format(frame_resource.name))
    else:
        coordinate_frame = remote.create_project(frame_resource)
        print('Created frame {0}'.format(frame_resource.name))
    return coordinate_frame
Esempio n. 21
0
def set_channel_resource(remote: BossRemote, params: dict) -> ChannelResource:
    """Use the arguments in the class config to create a channel resource object"""
    if 'name' not in params:
        params['name'] = 'channel{0}'.format(hex(round(time.time()))[2:])
    param_names = [str(p.name) for p in inspect.signature(ChannelResource).parameters.values()]
    filtered_params = {k: v for k, v in list(params.items()) if k in param_names}  # Filter unexpected arguments
    chan_resource = ChannelResource(**filtered_params)
    if chan_resource.name in remote.list_channels(chan_resource.coll_name, chan_resource.exp_name):
        channel = remote.update_project(chan_resource.name, chan_resource)
        print('Updated channel {0}'.format(chan_resource.name))
    else:
        channel = remote.create_project(chan_resource)
        print('Created channel {0}'.format(chan_resource.name))
    return channel
Esempio n. 22
0
    def download(self, bbox, mip, parallel=1, renumber=False):
        if parallel != 1:
            raise ValueError("Only parallel=1 is supported for boss.")
        elif renumber != False:
            raise ValueError("Only renumber=False is supported for boss.")

        bounds = Bbox.clamp(bbox, self.meta.bounds(mip))

        if self.autocrop:
            image, bounds = autocropfn(self.meta, image, bounds, mip)

        if bounds.subvoxel():
            raise exceptions.EmptyRequestException(
                'Requested less than one pixel of volume. {}'.format(bounds))

        x_rng = [bounds.minpt.x, bounds.maxpt.x]
        y_rng = [bounds.minpt.y, bounds.maxpt.y]
        z_rng = [bounds.minpt.z, bounds.maxpt.z]

        layer_type = 'image' if self.meta.layer_type == 'unknown' else self.meta.layer_type

        chan = ChannelResource(
            collection_name=self.meta.path.bucket,
            experiment_name=self.meta.path.dataset,
            name=self.meta.path.layer,  # Channel
            type=layer_type,
            datatype=self.meta.data_type,
        )

        rmt = BossRemote(boss_credentials)
        cutout = rmt.get_cutout(chan, mip, x_rng, y_rng, z_rng, no_cache=True)
        cutout = cutout.T
        cutout = cutout.astype(self.meta.dtype)
        cutout = cutout[::steps.x, ::steps.y, ::steps.z]

        if len(cutout.shape) == 3:
            cutout = cutout.reshape(tuple(list(cutout.shape) + [1]))

        if self.bounded or self.autocrop or bounds == bbox:
            return VolumeCutout.from_volume(self.meta, mip, cutout, bounds)

        # This section below covers the case where the requested volume is bigger
        # than the dataset volume and the bounds guards have been switched
        # off. This is useful for Marching Cubes where a 1px excess boundary
        # is needed.
        shape = list(bbox.size3()) + [cutout.shape[3]]
        renderbuffer = np.zeros(shape=shape, dtype=self.meta.dtype, order='F')
        shade(renderbuffer, bbox, cutout, bounds)
        return VolumeCutout.from_volume(self.meta, mip, renderbuffer, bbox)
Esempio n. 23
0
    def test_init_with_default_file(self):
        rmt = BossRemote(self.default_cfg)
        actual_default = dict(rmt._config.items("Default"))
        self.assertEqual('https', actual_default[CONFIG_PROTOCOL])
        self.assertEqual('default.theboss.io', actual_default[CONFIG_HOST])
        self.assertEqual('default_secret_token', actual_default[CONFIG_TOKEN])

        actual_meta = dict(rmt._config.items(CONFIG_METADATA_SECTION))
        self.assertEqual('file', actual_meta[CONFIG_PROTOCOL])
        self.assertEqual('meta.theboss.io', actual_meta[CONFIG_HOST])
        self.assertEqual('my_secret_token2', actual_meta[CONFIG_TOKEN])

        rmt.token_metadata = actual_meta[CONFIG_TOKEN]
        rmt.token_volume = actual_default[CONFIG_TOKEN]
        rmt.token_project = actual_default[CONFIG_TOKEN]
    def initialize(cls):
        """Initialization for each test.

        Called by both setUp() and setUpClass().
        """
        cls.rmt = BossRemote('test.cfg', API_VER)

        # Turn off SSL cert verification.  This is necessary for interacting with
        # developer instances of the Boss.
        cls.rmt.project_service.session_send_opts = {'verify': False}
        cls.rmt.metadata_service.session_send_opts = {'verify': False}
        cls.rmt.volume_service.session_send_opts = {'verify': False}
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        coll_name = 'coll2309_{}'.format(random.randint(0, 9999))
        cls.coll = CollectionResource(coll_name, 'bar')

        cf_name = 'MetaFrame{}'.format(random.randint(0, 9999))
        cls.coord = CoordinateFrameResource(cf_name, 'Test coordinate frame.',
                                            0, 10, -5, 5, 3, 6, 1, 1, 1,
                                            'nanometers', 1, 'nanoseconds')

        cls.exp = ExperimentResource('myMetaExp2309', cls.coll.name,
                                     cls.coord.name, 'my experiment', 1,
                                     'isotropic', 1)

        cls.chan = ChannelResource('myTestMetaChan', cls.coll.name,
                                   cls.exp.name, 'image', 'test channel', 0,
                                   'uint8', 0)
Esempio n. 25
0
    def setUpClass(cls):
        """Do an initial DB clean up in case something went wrong the last time.

        If a test failed really badly, the DB might be in a bad state despite
        attempts to clean up during tearDown().
        """
        warnings.filterwarnings('ignore')
        cls.rmt = BossRemote('test.cfg', API_VER)

        # Turn off SSL cert verification.  This is necessary for interacting with
        # developer instances of the Boss.
        cls.rmt.project_service.session_send_opts = {'verify': False}
        cls.rmt.metadata_service.session_send_opts = {'verify': False}
        cls.rmt.volume_service.session_send_opts = {'verify': False}
        requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

        cls.create_grp_name = 'int_test_group{}'.format(random.randint(0, 9999))
        cls.existing_grp_name = 'int_test_group_exists{}'.format(random.randint(0, 9999))

        cls.rmt.create_group(cls.existing_grp_name)

        cls.user_name = 'bossadmin'

        # Create a new user because the user tests run under, will
        # automatically be a member and maintainer of any groups created
        # during testing.
        cls.created_user = '******'.format(random.randint(0, 9999))

        password = '******'
        cls.rmt.add_user(cls.created_user, 'John', 'Doeski', 'jdoe{}@rime.com'.format(random.randint(0, 9999)),
                         password)
        token = cls.get_access_token(cls.created_user, password)
        cls.login_user(token)
Esempio n. 26
0
def getChan(CONFIG_FILE, coll, exp):
    config = configparser.ConfigParser()
    config.read(CONFIG_FILE)
    TOKEN = config['Default']['token']
    boss_url = ''.join((config['Default']['protocol'], '://',
                        config['Default']['host'], '/v1/'))
    #print(boss_url)
    #'https://api.boss.neurodata.io/v1/'

    #intern
    rem = BossRemote(CONFIG_FILE)
    chan = rem.list_channels(coll, exp)
    out = list(
        filter(lambda x: re.search(r'^(?!annotation|empty|EM)', x), chan))
    em = list(filter(lambda x: re.search(r'(EM*)', x), chan))
    return (out + em)
Esempio n. 27
0
class NeuroDataResource:
    def __init__(self, host, token, collection, experiment, chanList):
        self._collection = collection
        self._experiment = experiment
        self._bossRemote = BossRemote({'protocol':'https',
                                       'host':host,
                                       'token':token})
        self._chanList = {}
        for chanDict in chanList:
            try:
                self._chanList[chanDict['name']] = ChannelResource(chanDict['name'],
                                                                   collection,
                                                                   experiment,
                                                                   'image',
                                                                   datatype=chanDict['dtype'])
            except:
                #TODO error handle here
                raise

    def get_cutout(self, chan, zRange=None, yRange=None, xRange=None):
        if not chan in self._chanList.keys():
            print('Error: Channel Not Found in this Resource')
            return
        if zRange is None or yRange is None or xRange is None:
            print('Error: You must supply zRange, yRange, xRange kwargs in list format')
        data = self._bossRemote.get_cutout(self._chanList[chan],
                                           0,
                                           xRange,
                                           yRange,
                                           zRange)
        return data
Esempio n. 28
0
    def __init__(self, ingest_job, get_only=True):
        self.ingest_job = ingest_job

        self.coord_frame_name = '_'.join(
            (ingest_job.coll_name, ingest_job.exp_name))

        self.rmt = BossRemote(self.ingest_job.boss_config_file)

        self.coll_resource = self.setup_boss_collection(get_only=get_only)

        self.coord_frame_resource = self.setup_boss_coord_frame(
            get_only=get_only)

        self.exp_resource = self.setup_boss_experiment(get_only=get_only)

        self.ch_resource = self.setup_boss_channel(get_only=get_only)
Esempio n. 29
0
def delete_collection(remote: BossRemote, collection):
    coll_obj = None
    coll_name = None
    if isinstance(collection, CollectionResource):
        coll_obj = collection
        coll_name = collection.name
    elif isinstance(collection, str):
        coll_name = collection
        coll_obj = remote.get_project(CollectionResource(name=coll_name))
    if coll_name is not None:
        print('Deleting experiments of collection "{0}"...'.format(coll_name))
        exp_names = remote.list_experiments(coll_name)
        for n in exp_names:
            delete_experiment(remote, n, coll_name)
    if coll_obj is not None:
        print('Deleting collection "{0}"...'.format(coll_name))
        remote.delete_project(coll_obj)
Esempio n. 30
0
def main():
    config = sys.argv[1]
    config_p = configparser.ConfigParser()
    config_p.read(config)
    rmt = BossRemote(config)
    viz_link = register_data(rmt, config_p)
    logfile = config_p['Default']['logfile']
    write_to_logfile('registration viz link: {}'.format(viz_link), logfile)
Esempio n. 31
0
 def __init__(self, host, token, collection, experiment, chanList):
     self._collection = collection
     self._experiment = experiment
     self._bossRemote = BossRemote({'protocol':'https',
                                    'host':host,
                                    'token':token})
     self._chanList = {}
     for chanDict in chanList:
         try:
             self._chanList[chanDict['name']] = ChannelResource(chanDict['name'],
                                                                collection,
                                                                experiment,
                                                                'image',
                                                                datatype=chanDict['dtype'])
         except:
             #TODO error handle here
             raise
Esempio n. 32
0
    def _boss_cutout(self, requested_bbox, steps, channel_slice=slice(None)):
        bounds = Bbox.clamp(requested_bbox, self.bounds)

        if bounds.subvoxel():
            raise exceptions.EmptyRequestException(
                'Requested less than one pixel of volume. {}'.format(bounds))

        x_rng = [bounds.minpt.x, bounds.maxpt.x]
        y_rng = [bounds.minpt.y, bounds.maxpt.y]
        z_rng = [bounds.minpt.z, bounds.maxpt.z]

        layer_type = 'image' if self.layer_type == 'unknown' else self.layer_type

        chan = ChannelResource(
            collection_name=self.path.bucket,
            experiment_name=self.path.dataset,
            name=self.path.layer,  # Channel
            type=layer_type,
            datatype=self.dtype,
        )

        rmt = BossRemote(boss_credentials)
        cutout = rmt.get_cutout(chan,
                                self.mip,
                                x_rng,
                                y_rng,
                                z_rng,
                                no_cache=True)
        cutout = cutout.T
        cutout = cutout.astype(self.dtype)
        cutout = cutout[::steps.x, ::steps.y, ::steps.z]

        if len(cutout.shape) == 3:
            cutout = cutout.reshape(tuple(list(cutout.shape) + [1]))

        if self.bounded or self.autocrop or bounds == requested_bbox:
            return VolumeCutout.from_volume(self, cutout, bounds)

        # This section below covers the case where the requested volume is bigger
        # than the dataset volume and the bounds guards have been switched
        # off. This is useful for Marching Cubes where a 1px excess boundary
        # is needed.
        shape = list(requested_bbox.size3()) + [cutout.shape[3]]
        renderbuffer = np.zeros(shape=shape, dtype=self.dtype, order='F')
        txrx.shade(renderbuffer, requested_bbox, cutout, bounds)
        return VolumeCutout.from_volume(self, renderbuffer, requested_bbox)
Esempio n. 33
0
def main():
    config = sys.argv[1]
    config_p = configparser.ConfigParser()
    config_p.read(config)
    rmt = BossRemote(config)
    viz_link = detect_cells(rmt, config_p)
    logfile = config_p['Default']['logfile']
    boss_util.write_to_logfile('cell_detection viz link: {}'.format(viz_link),logfile)
Esempio n. 34
0
class NeuroDataResource:
    def __init__(self, host, token, collection, experiment, chanList):
        self._collection = collection
        self._experiment = experiment
        self._bossRemote = BossRemote({'protocol':'https',
                                       'host':host,
                                       'token':token})
        self._chanList = {}
        for chanDict in chanList:
            try:
                self._chanList[chanDict['name']] = ChannelResource(chanDict['name'],
                                                                   collection,
                                                                   experiment,
                                                                   'image',
                                                                   datatype=chanDict['dtype'])
            except:
                #TODO error handle here
                raise

    def get_cutout(self, chan, zRange=None, yRange=None, xRange=None):
        if not chan in self._chanList.keys():
            print('Error: Channel Not Found in this Resource')
            return
        if zRange is None or yRange is None or xRange is None:
            print('Error: You must supply zRange, yRange, xRange kwargs in list format')
        data = self._bossRemote.get_cutout(self._chanList[chan],
                                           0,
                                           xRange,
                                           yRange,
                                           zRange)
        return data



    def save_img(self, fname, cutout, format = 'tif'):
        '''
        Saves a image as a tif or nii file
        Leave the file type suffix out of fname
        '''
        if (not fname) or (type(fname) != str):
            print('Error: Give valid filename')
        if (not cutout) or type(cutout) != np.array:
            print('Error: Give valid image cutout')

        if format == 'tif':
            max_grey_val = cutout.max(axis=1).max(axis=1).max()
            dim = cutout.shape()
            cutout_rgb = np.zeros((dim[0], dim[1], dim[2], 3))
            for i in range(dim[0]):
                for j in range(dim[1]):
                    for k in range(dim[2]):
                        normalized_rgb = int( 255 * (cutout[i][j][k]/max_grey_val) )
                        for p in range(3):
                            cutout_rgb[i][j][k][p] = normalized_rgb
            imsave(fname+'.tif', cutout_rgb.astype(np.uint8))
        else:
            nifti_img = nib.Nifti1Image(cutout, np.eye(4))
            nib.save(nifti_img, fname+'.nii')
Esempio n. 35
0
def delete_experiment(remote: BossRemote, experiment, coll_name=None):
    exp_obj = None
    exp_name = None
    if isinstance(experiment, ExperimentResource):
        exp_obj = experiment
        exp_name = experiment.name
        coll_name = experiment.coll_name
    elif isinstance(experiment, str) and coll_name is not None:
        exp_name = experiment
        exp_obj = remote.get_project(ExperimentResource(name=exp_name, collection_name=coll_name))
    if exp_name is not None:
        print('Deleting channels of experiment "{0}"...'.format(exp_name))
        chan_names = remote.list_channels(coll_name, exp_name)
        for n in chan_names:
            delete_channel(remote, n, exp_name, coll_name)
    if exp_obj is not None:
        print('Deleting experiment "{0}"...'.format(exp_obj.name))
        remote.delete_project(exp_obj)
Esempio n. 36
0
 def __init__(self, host, token, collection, experiment, requested_channels, 
              xRange, 
              yRange, 
              zRange):
     
     self._bossRemote = BossRemote({'protocol': 'https',
                                    'host': host,
                                    'token': token})
     self.collection = collection
     self.experiment = experiment
     if len(requested_channels) == 0:
         self.requested_channels = self.channels
     else:
         self.requested_channels = requested_channels
     self.channels = self._bossRemote.list_channels(collection, experiment)
     self._get_coord_frame_details()
     self.x_range = xRange
     self.y_range = yRange
     self.z_range = zRange
Esempio n. 37
0
def main():
    t_start_overall = time.time()
    parser = argparse.ArgumentParser(description='Register a brain in the BOSS and upload it back in a new experiment.')
    parser.add_argument('--collection', help='Name of collection to upload tif stack to', type=str)
    parser.add_argument('--experiment', help='Name of experiment to upload tif stack to', type=str)
    parser.add_argument('--channel', help='Name of channel to upload tif stack to. Default is new channel will be created unless otherwise specified. See --new_channel', type=str)
    parser.add_argument('--config', help='Path to configuration file with Boss API token. Default: ~/.intern/intern.cfg', default=os.path.expanduser('~/.intern/intern.cfg'))
    parser.add_argument('--scale', help='Scale at which to perform the bias correction. Default is 0.1 meaning 1/10th the size of the original image', default=0.1, type=float)

    args = parser.parse_args()

    # mm to um conversion factor
    mm_to_um = 1000.0

    # download image
    rmt = BossRemote(cfg_file_or_dict=args.config)

    # resolution level from 0-6
    resolution_image = 0
    image_isotropic = True

    # downloading image
    print('downloading experiment: {}, channel: {}...'.format(args.experiment, args.channel))
    t1 = time.time()
    img = download_image(rmt, args.collection, args.experiment, args.channel, res=resolution_image, isotropic=image_isotropic)
    print("time to download image at res {} um: {} seconds".format(img.GetSpacing()[0] * mm_to_um, time.time()-t1))
    
    print("correcting bias in image...")
    t1 = time.time()
#    scale = 0.05 # scale at which to perform bias correction
    img_bc = preprocessor.correct_bias_field(img, scale=args.scale, niters=[100,100,100,100])
    print("time to correct bias in image at res {} um: {} seconds".format(img.GetSpacing()[0] * mm_to_um * (1.0/args.scale), time.time()-t1))

#    print("upsampling bias to match original size of image...")
#    ch_rsc_og = create_channel_resource(rmt, args.channel, args.collection, args.experiment, new_channel=False)
#    meta = get_xyz_extents(rmt, ch_rsc_og)
#    spacing = np.array(meta[-1])/mm_to_um
#    x_size = meta[0][1]
#    y_size = meta[1][1]
#    z_size = meta[2][1]
#    size = (x_size, y_size, z_size)
#    
#    # recover bias
#    bias_ds = img_bc / sitk.Cast(img, img_bc.GetPixelID())
#
#    # Upsample bias
#    bias = imgResample(bias_ds, spacing=spacing, size=img.GetSize())
#    
#    # apply bias to image
#    img_bc = sitk.Cast(img, sitk.sitkFloat32) * sitk.Cast(bias, sitk.sitkFloat32)
#
    print("uploading bias corrected image back to the BOSS")
    new_channel = args.channel + '_bias_corrected'
    ch_rsc_bc = create_channel_resource(rmt, new_channel, args.collection, args.experiment, datatype='uint16', type='image')
    upload_to_boss(rmt, sitk.GetArrayFromImage(img_bc).astype('uint16'), ch_rsc_bc)
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
from intern.remote.boss import BossRemote
from intern.resource.boss.resource import *


CONFIG_FILE = "demo_cfg.json"

rmt = BossRemote("./boss.cfg")

# Load Configuration File
with open(os.path.join("./db_configs", CONFIG_FILE), 'rt') as cfg:
    config = json.load(cfg)

# Create a collection
collection = CollectionResource(config["collection"]["name"], config["collection"]["description"])
try:
    collection = rmt.create_project(collection)
except Exception as e:
    collection = rmt.get_project(collection)

# Create a coord frame
coord = CoordinateFrameResource(config["coordinate_frame"]["name"],
                                config["coordinate_frame"]["description"],
Esempio n. 39
0
def gen_urls(args):
    config = {'protocol': 'https',
              'host': args.hostname,
              'token': args.token}

    boss = BossRemote(config)
    results = []

    try:
        if args.collection is not None:
            collections = [args.collection]
        else:
            collections = boss.list_collections()

        for collection in collections:
            if args.experiment is not None:
                experiments = [args.experiment]
            else:
                experiments = boss.list_experiments(collection)

            for experiment in experiments:
                if args.channel is not None:
                    channels = [args.channel]
                else:
                    channels = boss.list_channels(collection, experiment)

                exp = ExperimentResource(name = experiment,
                                         collection_name = collection)
                exp = boss.get_project(exp)

                coord = CoordinateFrameResource(name = exp.coord_frame)
                coord = boss.get_project(coord)

                for channel in channels:
                    ch = ChannelResource(name = channel,
                                         experiment_name = experiment,
                                         collection_name = collection)
                    ch = boss.get_project(ch)

                    def check_range(name, var, start, stop):
                        start_, stop_ = map(int, var.split(':'))
                        if start_ < start:
                            fmt = "{} range start for {}/{}/{} is less than the coordinate frame, setting to minimum"
                            print(fmt.format(name, collection, experiment, channel))
                            start_ = start
                        if stop_ > stop:
                            fmt = "{} range stop for {}/{}/{} is greater than the coordinate frame, setting to maximum"
                            print(fmt.format(name, collection, experiment, channel))
                            stop_ = stop
                        return '{}:{}'.format(start_, stop_)

                    if args.x_range:
                        x = check_range('X', args.x_range, coord.x_start, coord.x_stop)
                    else:
                        x = (coord.x_start, coord.x_stop, args.min, args.max)

                    if args.y_range:
                        y = check_range('Y', args.y_range, coord.y_start, coord.y_stop)
                    else:
                        y = (coord.y_start, coord.y_stop, args.min, args.max)

                    if args.z_range:
                        z = check_range('Z', args.z_range, coord.z_start, coord.z_stop)
                    else:
                        z = (coord.z_start, coord.z_stop, args.min, args.max)

                    # Arguments to gen_url
                    results.append((args.hostname,
                                    collection,
                                    experiment,
                                    channel,
                                    0, x, y, z, None))
    except Exception as e:
        print("Error generating URLs: {}".format(e))

    return results
Esempio n. 40
0
# limitations under the License.

from intern.remote.boss import BossRemote
from intern.resource.boss.resource import *

# Script to create a group and give it read access to a channel

# #### SETUP
grp_name = 'my_team'
collection_name = "my_col"
experiment_name = "my_exp"
coord_frame_name = "my_coord"
channel_name = "my_chan"
# #### SETUP

rmt = BossRemote("./boss.cfg")

print('Creating group . . .')
try:
    rmt.create_group(grp_name)
except Exception as e:
    # Assume group already exists if an exception raised.
    print(e)

# Get the resources
collection = CollectionResource(collection_name)
collection = rmt.get_project(collection)
experiment = ExperimentResource(experiment_name, collection_name, coord_frame_name)
experiment = rmt.get_project(experiment)
channel = ChannelResource(channel_name, collection_name, experiment_name)
channel = rmt.get_project(channel)