Soma detection notebookΒΆ

This notebook demonstrates how to use brainlit.algorithms.detect_somas.find_somas to detect bright somas in brain volumes of \(100\mu m^3\).

[1]:
import boto3
import numpy as np
from io import BytesIO
from cloudvolume.lib import Bbox
from brainlit.utils.session import NeuroglancerSession
from brainlit.algorithms.detect_somas import find_somas
import matplotlib.pyplot as plt

brain = 1
mip = 1
/opt/buildhome/python3.7/lib/python3.7/site-packages/python_jsonschema_objects/__init__.py:53: UserWarning: Schema version http://json-schema.org/draft-04/schema not recognized. Some keywords and features may not be supported.
  self.schema["$schema"]
[2]:
s3 = boto3.resource("s3")
bucket = s3.Bucket("open-neurodata")

brain_name = f"brain{brain}"

brain_prefix = f"brainlit/{brain_name}"
segments_prefix = f"brainlit/{brain_name}_segments"
somas_prefix = f"brainlit/{brain_name}_somas"

brain_url = f"s3://open-neurodata/{brain_prefix}"
segments_url = f"s3://open-neurodata/{segments_prefix}"

volume_keys = [
    "4807349.0_3827990.0_2922565.75_4907349.0_3927990.0_3022565.75",
    "4873075.5_4753413.5_6851474.0_4973075.5_4853413.5_6951474.0",
    "4881146.0_4792378.5_7001708.5_4981146.0_4892378.5_7101708.5"
]

ngl_sess = NeuroglancerSession(
    mip=mip, url=brain_url, url_segments=segments_url, use_https=True
)
res = ngl_sess.cv_segments.scales[ngl_sess.mip]["resolution"]
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-575cd73f4452> in <module>
     18
     19 ngl_sess = NeuroglancerSession(
---> 20     mip=mip, url=brain_url, url_segments=segments_url, use_https=True
     21 )
     22 res = ngl_sess.cv_segments.scales[ngl_sess.mip]["resolution"]

/opt/build/repo/brainlit/utils/session.py in __init__(self, url, mip, url_segments, fill_missing, use_https)
     58         self.use_https = use_https
     59         self.cv = CloudVolume(
---> 60             url, parallel=False, fill_missing=fill_missing, use_https=self.use_https
     61         )
     62         if mip < 0 or mip >= len(self.cv.scales):

~/python3.7/lib/python3.7/site-packages/cloudvolume/cloudvolume.py in __new__(cls, cloudpath, mip, bounded, autocrop, fill_missing, cache, compress_cache, cdn_cache, progress, info, provenance, compress, compress_level, non_aligned_writes, parallel, delete_black_uploads, background_color, green_threads, use_https, max_redirects)
    195     path = strict_extract(cloudpath)
    196     if path.format in REGISTERED_PLUGINS:
--> 197       return REGISTERED_PLUGINS[path.format](**kwargs)
    198     else:
    199       raise UnsupportedFormatError(

~/python3.7/lib/python3.7/site-packages/cloudvolume/datasource/precomputed/__init__.py in create_precomputed(cloudpath, mip, bounded, autocrop, fill_missing, cache, compress_cache, cdn_cache, progress, info, provenance, compress, compress_level, non_aligned_writes, parallel, delete_black_uploads, background_color, green_threads, use_https, max_redirects)
     40       cloudpath, cache=cache,
     41       info=info, provenance=provenance,
---> 42       max_redirects=max_redirects
     43     )
     44

~/python3.7/lib/python3.7/site-packages/cloudvolume/datasource/precomputed/metadata.py in __init__(self, cloudpath, cache, info, provenance, max_redirects)
     50
     51     if info is None:
---> 52       self.refresh_info(max_redirects=max_redirects)
     53       if self.cache and self.cache.enabled:
     54         self.cache.check_info_validity()

~/python3.7/lib/python3.7/site-packages/cloudvolume/datasource/precomputed/metadata.py in refresh_info(self, max_redirects)
    170         return self.info
    171
--> 172     self.info = self.redirectable_fetch_info(max_redirects)
    173
    174     if self.cache:

~/python3.7/lib/python3.7/site-packages/cloudvolume/datasource/precomputed/metadata.py in redirectable_fetch_info(self, max_redirects)
    232
    233     for _ in range(max_redirects):
--> 234       info = self.fetch_info()
    235
    236       if 'redirect' not in info or not info['redirect']:

~/python3.7/lib/python3.7/site-packages/cloudvolume/datasource/precomputed/metadata.py in fetch_info(self)
    189     """
    190     with SimpleStorage(self.cloudpath) as stor:
--> 191       info = stor.get_json('info')
    192
    193     if info is None:

~/python3.7/lib/python3.7/site-packages/cloudvolume/storage/storage.py in get_json(self, file_path)
     60
     61   def get_json(self, file_path):
---> 62     content = self.get_file(file_path)
     63     if content is None:
     64       return None

~/python3.7/lib/python3.7/site-packages/cloudvolume/storage/storage.py in get_file(self, file_path, start, end)
    162     """
    163     content, encoding = self._interface.get_file(file_path, start=start, end=end)
--> 164     content = compression.decompress(content, encoding, filename=file_path)
    165     return content
    166

~/python3.7/lib/python3.7/site-packages/cloudvolume/compression.py in decompress(content, encoding, filename)
     38     raise
     39
---> 40   raise NotImplementedError(str(encoding) + ' is not currently supported. Supported Options: None, gzip')
     41
     42 def compress(content, method='gzip', compress_level=None):

NotImplementedError: utf-8 is not currently supported. Supported Options: None, gzip
[3]:
for volume_key in volume_keys:
    volume_coords = np.array(
        os.path.basename(volume_key).split("_")
    ).astype(float)
    volume_vox_min = np.round(np.divide(volume_coords[:3], res)).astype(int)
    volume_vox_max = np.round(np.divide(volume_coords[3:], res)).astype(int)

    bbox = Bbox(volume_vox_min, volume_vox_max)
    volume = ngl_sess.pull_bounds_img(bbox)

    somas_obj = s3.Object("open-neurodata", f"{somas_prefix}/{volume_key}").get()
    somas = np.load(BytesIO(somas_obj["Body"].read()))
    rel_soma_coords = np.array(
        [
            np.round(np.divide(c, res)).astype(int) - volume_vox_min
            for c in somas
        ]
    )

    label, rel_pred_centroids, out = find_somas(volume, res)

    _, axes = plt.subplots(1, 2)

    ax = axes[0]
    vol_proj = np.amax(volume, axis=2)
    ax.imshow(vol_proj, cmap="gray", origin="lower")
    ax.scatter(rel_soma_coords[:, 1], rel_soma_coords[:, 0], c="none", edgecolor="r", label="Ground truth")
    if label == 1:
        ax.scatter(rel_pred_centroids[:, 1], rel_pred_centroids[:, 0], c="b", alpha=0.5)
    ax.set_title("Volume")

    ax = axes[1]
    mask_proj = np.amax(out, axis=2)
    ax.imshow(mask_proj, cmap="jet", vmin=0, origin="lower")
    plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-ace9b1fc3e97> in <module>
      1 for volume_key in volume_keys:
      2     volume_coords = np.array(
----> 3         os.path.basename(volume_key).split("_")
      4     ).astype(float)
      5     volume_vox_min = np.round(np.divide(volume_coords[:3], res)).astype(int)

NameError: name 'os' is not defined