Skip to content

Commit

Permalink
works with python 3.8 not 3.7 anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
xiuliren committed Jan 5, 2022
1 parent 605e07f commit ab8fdef
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: python
python:
- '3.7'
- '3.8'
- '3.9'
- '3.10'

#jobs:
# allow_failures:
Expand Down
71 changes: 68 additions & 3 deletions chunkflow/lib/synapses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import json
from copy import deepcopy
from functools import cached_property
from collections import defaultdict

import numpy as np
import h5py
Expand Down Expand Up @@ -102,9 +104,47 @@ def from_json(cls, fname: str, resolution: tuple = None):
@classmethod
def from_h5(cls, fname: str, resolution: tuple = None):
with h5py.File(fname, 'r') as hf:
pre = np.asarray(hf['pre'])
confidence = np.asarray(hf['confidence'])
return cls(pre, pre_confidence=confidence, resolution=resolution)

pre = np.asarray(hf['pre'], dtype=np.int32)

if resolution is None and 'resolution' in hf.keys():
resolution = np.asarray(hf['resolution'])

if 'post' in hf.keys():
post = np.asarray(hf['post'], dtype=np.int32)
else:
post = None

if 'pre_confidence' in hf.keys():
pre_confidence = np.asarray(hf['pre_confidence'])
else:
pre_confidence = None

if 'post_confidence' in hf.keys():
post_confidence = np.asarray(hf['post_confidence'])
else:
post_confidence = None

return cls(pre, post=post, pre_confidence=pre_confidence,
post_confidence=post_confidence, resolution=resolution)

def to_h5(self, fname: str) -> None:
assert fname.endswith(".h5") or fname.endswith(".hdf5")
with h5py.File(fname, "w") as hf:

hf['pre'] = self.pre

if self.post is not None:
hf['post'] = self.post

if self.resolution is not None:
hf['resolution'] = self.resolution

if self.pre_confidence is not None:
hf['pre_confidence'] = self.pre_confidence

if self.post_confidence is not None:
hf['post_confidence'] = self.post_confidence

@classmethod
def from_file(cls, fname: str, resolution: tuple = None):
Expand All @@ -119,6 +159,10 @@ def from_file(cls, fname: str, resolution: tuple = None):
@property
def pre_num(self):
return self.pre.shape[0]

@property
def post_num(self):
return self.post.shape[0]

@property
def pre_with_physical_coordinate(self):
Expand All @@ -143,3 +187,24 @@ def post_with_physical_coordinate(self):
post[:, 1:] *= self.resolution
return post

@cached_property
def pre_index2post_indices(self):
pi2pi = defaultdict(list)
for idx in range(self.pre_num):
# find the post synapses for this presynapse
post_indices = np.argwhere(self.post[:, 0]==idx)
pi2pi[idx].append(post_indices)

return pi2pi

@cached_property
def distances_from_pre_to_post(self):
distances = np.zeros((self.post_num,), dtype=float)
for post_idx in range(self.post_num):
post = self.post[post_idx, 1:]
pre_idx = self.post[post_idx, 0]
pre = self.pre[pre_idx]
distances[post_idx] = np.linalg.norm(pre - post)
return distances


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
python_requires='>=3',
zip_safe=False,
Expand Down

0 comments on commit ab8fdef

Please sign in to comment.