Skip to content

Commit

Permalink
Update neuromorpholib
Browse files Browse the repository at this point in the history
  • Loading branch information
j6k4m8 committed Aug 27, 2018
1 parent f7e835a commit 172dd69
Show file tree
Hide file tree
Showing 7 changed files with 616 additions and 0 deletions.
233 changes: 233 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@

# Created by https://www.gitignore.io/api/node,code,macos,python

### Code ###
# Visual Studio Code - https://code.visualstudio.com/
.settings/
.vscode/
tsconfig.json
jsconfig.json

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### Python Patch ###
.venv/

### Python.VirtualEnv Stack ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json


# End of https://www.gitignore.io/api/node,code,macos,python
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# neuromorpholib


## Usage
A simple download of known dataset and name:
```python
from neuromorpholib import neuromorpho

nmo = neuromorpho.NeuroMorpho()
acc1 = nmo.download_swc("martone", "ACC1")
```

A query for all species=mouse neurons:
```python
mouse_neurons = nmo.search({"species": "mouse"})
```

Download a SWC for a mouse neuron:
```python
swc_demo = nmo.download_swc(
mouse_neurons[0]['archive'],
mouse_neurons[0]['neuron_name']
)
```
8 changes: 8 additions & 0 deletions neuromorpholib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Neuromorpholib is a python package for manipulation neuron morphology.
"""

from . import swc
from . import neuromorpho

__version__ = "0.0.1"
70 changes: 70 additions & 0 deletions neuromorpholib/neuromorpho/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/enb python3
import requests
import glob

from .. import swc


class NeuroMorpho:

def __init__(self, cache_location="~/.neuromorphocache/"):
self.cache = {}
self.cache_location = cache_location
self.base_url = "http://neuromorpho.org/"
self._permitted_fields = self.get_json("api/neuron/fields")['Neuron Fields']

def url(self, ext=""):
ext = ext.lstrip("/")
return self.base_url + ext

def get_json(self, ext):
res = requests.get(self.url(ext))
return res.json()

def search(self, query, page=0, limit=None):
for k, _ in query.items():
if k not in self._permitted_fields:
raise ValueError(
"Key {} is not a valid search parameter!\n".format(k) +
"Must be one of:\n{}".format(self._permitted_fields)
)
query_string = "&".join([
"fq={}:{}".format(k, v) for k, v in query.items()
])

listing = self.get_json("api/neuron/select/?" + query_string[1:] + "&page={}".format(page))
try:
results = listing["_embedded"]["neuronResources"]
print("Downloading page {} for {} neurons, ending in {}".format(page, len(results), results[-1]['neuron_name']))
neuron_listing = results
except KeyError:
return []

if (
"page" in listing and
"totalPages" in listing["page"] and
listing['page']['totalPages'] >= page
):
if limit is None or len(neuron_listing) < limit:
if limit is None:
neuron_listing += self.search(query, page=page+1)
else:
neuron_listing += self.search(query, page=page+1, limit=limit-50)
else:
return neuron_listing

return neuron_listing

def download_swc(self, archive, neuron_name, text_only=False):
ext = "dableFiles/{}/CNG%20version/{}.CNG.swc".format(
archive.lower(),
neuron_name
)
res = requests.get(self.url(ext))
if ("<html>" in res):
raise ValueError("Failed to fetch from {}.".format(ext))

if text_only:
return res.text

return swc.read_swc(res.text)
Loading

0 comments on commit 172dd69

Please sign in to comment.