Skip to content

Commit a14868a

Browse files
authored
Merge pull request #13 from CBroz1/dj
blackify
2 parents 66453b6 + 9ef8326 commit a14868a

File tree

5 files changed

+206
-128
lines changed

5 files changed

+206
-128
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.
44

5-
## 0.0.0a1 - unreleased
5+
## 0.1.0 - 2022-05-10
66
### Added
7-
+
7+
+ Table architecture reflecting precursor projects
8+
+ Loaders for CCF atlas and channel locations json files
9+
+ Adopted black formatting into code base

element_electrode_localization/coordinate_framework.py

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def activate(schema_name, *, create_schema=True, create_tables=True):
2323
:param create_tables: when True (default), create tables in the database if
2424
they do not yet exist.
2525
"""
26-
schema.activate(schema_name, create_schema=create_schema,
27-
create_tables=create_tables)
26+
schema.activate(
27+
schema_name, create_schema=create_schema, create_tables=create_tables
28+
)
2829

2930
# ----------------------------- Table declarations ----------------------
3031

@@ -73,8 +74,8 @@ class Voxel(dj.Part):
7374

7475
@classmethod
7576
def retrieve_acronym(self, acronym):
76-
""" Retrieve the DataJoint translation of the CCF acronym"""
77-
return re.sub(r'(?<!^)(?=[A-Z])', '_', acronym).lower()
77+
"""Retrieve the DataJoint translation of the CCF acronym"""
78+
return re.sub(r"(?<!^)(?=[A-Z])", "_", acronym).lower()
7879

7980
@classmethod
8081
def voxel_query(self, x=None, y=None, z=None):
@@ -84,9 +85,9 @@ def voxel_query(self, x=None, y=None, z=None):
8485
:param z: z coordinate
8586
"""
8687
if not any(x, y, z):
87-
raise ValueError('Must specify at least one dimension')
88+
raise ValueError("Must specify at least one dimension")
8889
# query = self.Voxel # TODO: add utility function name lookup
89-
raise NotImplementedError('Coming soon')
90+
raise NotImplementedError("Coming soon")
9091

9192

9293
@schema
@@ -101,8 +102,9 @@ class ParentBrainRegion(dj.Lookup):
101102
# ---- HELPERS ----
102103

103104

104-
def load_ccf_annotation(ccf_id, version_name, voxel_resolution,
105-
nrrd_filepath, ontology_csv_filepath):
105+
def load_ccf_annotation(
106+
ccf_id, version_name, voxel_resolution, nrrd_filepath, ontology_csv_filepath
107+
):
106108
"""
107109
:param ccf_id: unique id to identify a new CCF dataset to be inserted
108110
:param version_name: CCF version
@@ -122,73 +124,88 @@ def load_ccf_annotation(ccf_id, version_name, voxel_resolution,
122124
https://community.brain-map.org/t/allen-mouse-ccf-accessing-and-using-related-data-and-tools/359
123125
(particularly the ontology file downloadable as CSV)
124126
"""
125-
ccf_key = {'ccf_id': ccf_id}
127+
ccf_key = {"ccf_id": ccf_id}
126128
if CCF & ccf_key:
127-
print(f'CCF ID {ccf_id} already exists!')
129+
print(f"CCF ID {ccf_id} already exists!")
128130
return
129131

130132
nrrd_filepath = pathlib.Path(nrrd_filepath)
131133
ontology_csv_filepath = pathlib.Path(ontology_csv_filepath)
132134

133135
def to_snake_case(s):
134-
return re.sub(r'(?<!^)(?=[A-Z])', '_', s).lower()
136+
return re.sub(r"(?<!^)(?=[A-Z])", "_", s).lower()
135137

136138
ontology = pd.read_csv(ontology_csv_filepath)
137139

138140
stack, hdr = nrrd.read(nrrd_filepath.as_posix()) # AP (x), DV (y), ML (z)
139141

140-
log.info('.. loaded atlas brain volume of shape '
141-
+ f'{stack.shape} from {nrrd_filepath}')
142-
143-
ccf_key = {'ccf_id': ccf_id}
144-
ccf_entry = {**ccf_key,
145-
'ccf_version': version_name,
146-
'ccf_resolution': voxel_resolution,
147-
'ccf_description': (f'Version: {version_name}'
148-
+ f' - Voxel resolution (uM): {voxel_resolution}'
149-
+ f' - Volume file: {nrrd_filepath.name}'
150-
+ ' - Region ontology file: '
151-
+ ontology_csv_filepath.name)
152-
}
142+
log.info(
143+
".. loaded atlas brain volume of shape " + f"{stack.shape} from {nrrd_filepath}"
144+
)
145+
146+
ccf_key = {"ccf_id": ccf_id}
147+
ccf_entry = {
148+
**ccf_key,
149+
"ccf_version": version_name,
150+
"ccf_resolution": voxel_resolution,
151+
"ccf_description": (
152+
f"Version: {version_name}"
153+
+ f" - Voxel resolution (uM): {voxel_resolution}"
154+
+ f" - Volume file: {nrrd_filepath.name}"
155+
+ " - Region ontology file: "
156+
+ ontology_csv_filepath.name
157+
),
158+
}
153159

154160
with dj.conn().transaction:
155161
CCF.insert1(ccf_entry)
156162
BrainRegionAnnotation.insert1(ccf_key)
157-
BrainRegionAnnotation.BrainRegion.insert([
158-
dict(ccf_id=ccf_id,
159-
acronym=to_snake_case(r.acronym),
160-
region_id=r.id,
161-
region_name=r.safe_name,
162-
color_code=r.color_hex_triplet) for _, r in ontology.iterrows()])
163+
BrainRegionAnnotation.BrainRegion.insert(
164+
[
165+
dict(
166+
ccf_id=ccf_id,
167+
acronym=to_snake_case(r.acronym),
168+
region_id=r.id,
169+
region_name=r.safe_name,
170+
color_code=r.color_hex_triplet,
171+
)
172+
for _, r in ontology.iterrows()
173+
]
174+
)
163175

164176
# Process voxels per brain region
165177
for idx, (region_id, r) in enumerate(tqdm(ontology.iterrows())):
166178
dj.conn().ping()
167179
region_id = int(region_id)
168180

169-
log.info('.. loading region {} ({}/{}) ({})'
170-
.format(region_id, idx, len(ontology), r.safe_name))
181+
log.info(
182+
".. loading region {} ({}/{}) ({})".format(
183+
region_id, idx, len(ontology), r.safe_name
184+
)
185+
)
171186

172187
# extracting filled volumes from stack in scaled [[x,y,z]] shape,
173-
vol = (np.array(np.where(stack == region_id)).T * voxel_resolution)
174-
vol = pd.DataFrame(vol, columns=['x', 'y', 'z'])
188+
vol = np.array(np.where(stack == region_id)).T * voxel_resolution
189+
vol = pd.DataFrame(vol, columns=["x", "y", "z"])
175190

176191
if not vol.shape[0]:
177-
log.info('.. region {} volume: shape {} - skipping'
178-
.format(region_id, vol.shape))
192+
log.info(
193+
".. region {} volume: shape {} - skipping".format(
194+
region_id, vol.shape
195+
)
196+
)
179197
continue
180198
else:
181-
log.info('.. region {} volume: shape {}'.format(
182-
region_id, vol.shape))
199+
log.info(".. region {} volume: shape {}".format(region_id, vol.shape))
183200

184-
vol['ccf_id'] = [ccf_key['ccf_id']] * len(vol)
201+
vol["ccf_id"] = [ccf_key["ccf_id"]] * len(vol)
185202
CCF.Voxel.insert(vol)
186203

187-
vol['acronym'] = [to_snake_case(r.acronym)] * len(vol)
204+
vol["acronym"] = [to_snake_case(r.acronym)] * len(vol)
188205
BrainRegionAnnotation.Voxel.insert(vol)
189206

190-
log.info('.. done.')
207+
log.info(".. done.")
191208

192209

193210
def load_parent_regions(ccf_id):
194-
raise NotImplementedError('Coming soon')
211+
raise NotImplementedError("Coming soon")

0 commit comments

Comments
 (0)