In [1]:
import argparse
from intern.remote.boss import BossRemote
from intern.resource.boss.resource import *
from intern.utils.parallel import block_compute
import configparser
import requests
import numpy as np
from numpy import genfromtxt
import shutil
import blosc
from IPython.core.debugger import set_trace
import sys
import os
import itertools
from functools import partial
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import cpu_count
import csv
import datetime
import seaborn as sns
from intern.utils.parallel import block_compute
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
CONFIG_FILE = 'config.ini'

In [2]:
COLL_NAME      = 'collman' 
EXP_NAME       = 'M247514_Rorb_1_Site3Align2_EM'
ANNO_NAME      = 'm247514_Site3Annotation_MN_global'
COORD_FRAME    = 'collman_M247514_Rorb_1_Site3Align2_EM_m247514_Site3Annotation_MN_global'
CONFIG_FILE    = 'config.ini'
OUTPUT         = 'testCollman14v2Cubes.csv'

#CHAN_NAMES = ['Synapsin647']
#CHAN_NAMES = ['DAPI1st', 'DAPI2nd', 'DAPI3rd', 'GABA488', 'GAD647',
#        'gephyrin594', 'GS594', 'MBP488', 'NR1594', 'PSD95_488',
#        'Synapsin647', 'VGluT1_647']
#CHAN_NAMES = ['synapsin', 'PSDr']

In [26]:
bf = [5,108,108] # in z,y,x order
                                                                                                     
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)
                                                                                                     
cf = CoordinateFrameResource(str(COLL_NAME + '_' + EXP_NAME))
cfr = rem.get_project(cf)
anno_res = ChannelResource(ANNO_NAME, COLL_NAME, EXP_NAME, 'annotation', datatype='uint64')

ex = {'x': cfr.x_stop, 'y': cfr.y_stop, 'z': cfr.z_stop}

blocks = block_compute(0,ex['x'],0,ex['y'],0,ex['z'],
           origin = (0,0,0), block_size = (512, 512, 16))
                                                                                                     
rid = []
for b in blocks:
    tmp = rem.get_ids_in_region(anno_res, 0, b[0], b[1], b[2], [0,1])
    if tmp != []:
        rid = rid + tmp
                                                                                                     
u = np.unique(np.asarray(rid))
u_old = u
                                                                                                     
## bounding box for annotation_i
bb = [rem.get_bounding_box(anno_res, 0,ui, 'tight') for ui in u]
                                                                                                     
for i in range(len(bb)):
    bb[i]["id"] = u[i]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    378             try:  # Python 2.7, use buffering of HTTP responses
--> 379                 httplib_response = conn.getresponse(buffering=True)
    380             except TypeError:  # Python 2.6 and older, Python 3

TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-26-e069d56f2607> in <module>()
     22 rid = []
     23 for b in blocks:
---> 24     tmp = rem.get_ids_in_region(anno_res, 0, b[0], b[1], b[2], [0,1])
     25     if tmp != []:
     26         rid = rid + tmp

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/intern/remote/remote.py in get_ids_in_region(self, resource, resolution, x_range, y_range, z_range, time_range)
    234         """
    235         return self._volume.get_ids_in_region(
--> 236             resource, resolution, x_range, y_range, z_range, time_range)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/intern/service/boss/volume.py in wrapper(*args, **kwargs)
     35                     'ChannelResource not fully initialized.  Use intern.remote.BossRemote.get_channel({}, {}, {})'.format(
     36                         args[1].name, args[1].coll_name, args[1].exp_name))
---> 37         return fcn(*args, **kwargs)
     38 
     39     return wrapper

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/intern/service/boss/volume.py in get_ids_in_region(self, resource, resolution, x_range, y_range, z_range, time_range)
    163         return self.service.get_ids_in_region(
    164             resource, resolution, x_range, y_range, z_range, time_range,
--> 165             self.url_prefix, self.auth, self.session, self.session_send_opts)
    166 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/intern/service/boss/v1/volume.py in get_ids_in_region(self, resource, resolution, x_range, y_range, z_range, time_range, url_prefix, auth, session, send_opts)
    306 
    307         prep = session.prepare_request(req)
--> 308         resp = session.send(prep, **send_opts)
    309 
    310         if resp.status_code == 200:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    616 
    617         # Send the request
--> 618         r = adapter.send(request, **kwargs)
    619 
    620         # Total elapsed time of the request (approximately)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438                     decode_content=False,
    439                     retries=self.max_retries,
--> 440                     timeout=timeout
    441                 )
    442 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    598                                                   timeout=timeout_obj,
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 
    602             # If we're going to release the connection in ``finally:``, then

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    380             except TypeError:  # Python 2.6 and older, Python 3
    381                 try:
--> 382                     httplib_response = conn.getresponse()
    383                 except Exception as e:
    384                     # Remove the TypeError from the exception chain in Python 3;

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in getresponse(self)
   1329         try:
   1330             try:
-> 1331                 response.begin()
   1332             except ConnectionError:
   1333                 self.close()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in begin(self)
    295         # read until we get a non-100 response
    296         while True:
--> 297             version, status, reason = self._read_status()
    298             if status != CONTINUE:
    299                 break

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in _read_status(self)
    256 
    257     def _read_status(self):
--> 258         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    259         if len(line) > _MAXLINE:
    260             raise LineTooLong("status line")

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py in readinto(self, b)
    584         while True:
    585             try:
--> 586                 return self._sock.recv_into(b)
    587             except timeout:
    588                 self._timeout_occurred = True

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in recv_into(self, buffer, nbytes, flags)
   1000                   "non-zero flags not allowed in calls to recv_into() on %s" %
   1001                   self.__class__)
-> 1002             return self.read(nbytes, buffer)
   1003         else:
   1004             return socket.recv_into(self, buffer, nbytes, flags)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in read(self, len, buffer)
    863             raise ValueError("Read on closed or unwrapped SSL socket.")
    864         try:
--> 865             return self._sslobj.read(len, buffer)
    866         except SSLError as x:
    867             if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in read(self, len, buffer)
    623         """
    624         if buffer is not None:
--> 625             v = self._sslobj.read(len, buffer)
    626         else:
    627             v = self._sslobj.read(len)

KeyboardInterrupt: 

In [27]:
blocks[0]
b0 = ((0, ))


Out[27]:
((512, 1024), (512, 1024), (16, 32))

In [15]:
A = [(rem.get_cutout(
      anno_res, 0, bb[i]["x_range"], 
      bb[i]["y_range"], bb[i]["z_range"], 
      id_list = [bb[i]['id']]) == bb[i]['id']).astype(int) 
    for i in range(len(bb))] 
                                                                                                     
#Bmeans = [np.int32(np.round(np.mean(np.asarray(np.where(A[i] == True)),1))) for i in range(len(A))]
Bmeans = [np.uint32(np.round(np.mean(np.asarray(np.where(A[i] == 1)),1))) for i in range(len(A))]

Bglobal = []
for i in range(len(bb)):
    ad1 = np.asarray([bb[i]['z_range'][0], bb[i]['y_range'][0], bb[i]['x_range'][0]])
    Bglobal.append(Bmeans[i] + ad1)
    
ColMin = np.asarray(bf)
ColMax = np.asarray([ex['z'] - (bf[0] + 1),  # The z index is inclusive
                     ex['y'] - (bf[1] + 1), 
                     ex['x'] - (bf[2] + 1)])
                                                                                                     
m = [Bglobal[i] >= ColMin for i in range(len(Bglobal))]
M = [Bglobal[i] <= ColMax for i in range(len(Bglobal))]
mm = [np.all(m[i]) for i in range(len(m)) ]
MM = [np.all(M[i]) for i in range(len(M)) ]

In [18]:
A[0].shape


Out[18]:
(2, 65, 49)

In [20]:
Bglobal[0]


Out[20]:
array([  12, 1026,  882])

In [ ]: