-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuroDataResource.py
71 lines (57 loc) · 2.91 KB
/
NeuroDataResource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from intern.remote.boss import BossRemote
from intern.resource.boss.resource import ChannelResource, ExperimentResource, CoordinateFrameResource
import configparser
class NeuroDataResource:
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._get_coord_frame_details()
def _get_coord_frame_details(self):
exp_resource = ExperimentResource(self.experiment, self.collection)
coord_frame = self._bossRemote.get_project(exp_resource).coord_frame
coord_frame_resource = CoordinateFrameResource(coord_frame)
data = self._bossRemote.get_project(coord_frame_resource)
self.max_dimensions = (data.z_stop, data.y_stop, data.x_stop)
self.voxel_size = (data.z_voxel_size, data.y_voxel_size, data.x_voxel_size)
def _get_channel(self, chan_name):
"""
Helper that gets a fully initialized ChannelResource for an *existing* channel.
Args:
chan_name (str): Name of channel.
coll_name (str): Name of channel's collection.
exp_name (str): Name of channel's experiment.
Returns:
(intern.resource.boss.ChannelResource)
"""
chan = ChannelResource(chan_name, self.collection, self.experiment)
return self._bossRemote.get_project(chan)
def assert_channel_exists(self, channel):
return channel in self.channels
def get_cutout(self, chan, zRange=None, yRange=None, xRange=None):
if chan not in self.channels:
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')
return
if xRange[1] > self.max_dimensions[2] or yRange[1] > self.max_dimensions[1] or zRange[1] > self.max_dimensions[0]:
raise Exception("Dimensions given are larger than max dimensions.")
if xRange[0] < 0 or yRange[0] < 0 or zRange[0] < 0:
raise Exception("Dimensions given are smaller than min dimensions.")
channel_resource = self._get_channel(chan)
datatype = channel_resource.datatype
data = self._bossRemote.get_cutout(channel_resource,
0,
xRange,
yRange,
zRange)
#Datatype check. Recast to original datatype if data is float64
if data.dtype == datatype:
return data
else:
return data.astype(datatype)