Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 500da48

Browse files
committed
Added post-processing steps
+ Post-processing: mirror hemisphere, split coordinates, resample + Import VoxelData directly everywhere in utils + Use flatmap_util.load_flatmap to load flatmap in utils
1 parent 4e9f3b5 commit 500da48

File tree

29 files changed

+275
-26
lines changed

29 files changed

+275
-26
lines changed

flatmap/code/utils/flat_hexgrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hexalattice.hexalattice as hex
22
from voxcell import VoxelData
33
from scipy.spatial import KDTree
4+
import flatmap_util as fmutil
45
import numpy as np
56
import math
67
import sys
@@ -18,9 +19,8 @@
1819
hex_n = int(sys.argv[4])
1920

2021
# load flatmap
21-
fmap = VoxelData.load_nrrd(flatmap_nrrd)
22+
fmap, msk = fmutil.load_flatmap(flatmap_nrrd)
2223
vshape = fmap.shape[0:3]
23-
msk = fmap.raw[:,:,:,0] > -1
2424

2525
# generate hexagonal grid
2626
hexgrid = hex.create_hex_grid(hex_n,hex_n,hex_diam)[0] + (0.5,0.5)

flatmap/code/utils/flatmap_util.py

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

33

44
def load_flatmap(file):
5-
import voxcell
6-
vd = voxcell.VoxelData.load_nrrd(file)
5+
from voxcell import VoxelData
6+
vd = VoxelData.load_nrrd(file)
77

88
# mask for mapped values
99
fx_msk = vd.raw[:,:,:,0] > -1

flatmap/code/utils/get_dimensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import voxcell as vc
1+
from voxcell import VoxelData
22
import numpy as np
33
import sys
44

55
nrrd_file = sys.argv[1]
66
output_file = sys.argv[2]
77

8-
vd = vc.VoxelData.load_nrrd(nrrd_file)
8+
vd = VoxelData.load_nrrd(nrrd_file)
99
n_notnan = np.sum(~np.isnan(vd.raw))
1010
shape_str = " ".join([str(x) for x in vd.raw.shape])
1111
voxdim_str = " ".join(["{:.8f}".format(x) for x in vd.voxel_dimensions])

flatmap/code/utils/make_flatmap.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import voxcell as vc
1+
from voxcell import VoxelData
22
import numpy as np
33
import sys
44

@@ -25,7 +25,7 @@
2525
if not flipx in [0, 1]:
2626
raise ValueError('Flip vertical must be 0 or 1')
2727

28-
vd = vc.VoxelData.load_nrrd(atlas_nrrd)
28+
vd = VoxelData.load_nrrd(atlas_nrrd)
2929

3030
fmap = np.full(list(vd.raw.shape) + [2], -1, dtype=np.float32)
3131

@@ -57,6 +57,4 @@
5757
pos[:,1] = fmax - pos[:,1] # y -> -y
5858

5959
fmap[tuple(np.uint64(w).T)] = pos
60-
out = vc.VoxelData(fmap,vd.voxel_dimensions,vd.offset)
61-
62-
out.save_nrrd(output_nrrd)
60+
vd.with_data(fmap).save_nrrd(output_nrrd)

flatmap/code/utils/mirror_hemi.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from voxcell import VoxelData
2+
import flatmap_util as fmutil
3+
import numpy as np
4+
import sys
5+
6+
flatmap_nrrd = sys.argv[1]
7+
hemi_nrrd = sys.argv[2]
8+
output_nrrd = sys.argv[3]
9+
10+
xmax = 1.0
11+
if len(sys.argv) > 4:
12+
xmax = float(sys.argv[4])
13+
14+
# load
15+
fmap, _ = fmutil.load_flatmap(flatmap_nrrd)
16+
hemi = VoxelData.load_nrrd(hemi_nrrd)
17+
18+
# define masks
19+
msk = (hemi.raw == 1)
20+
msko = (hemi.raw == 2)
21+
w = np.where(msk)
22+
wo = np.where(msko)
23+
mw = np.array(w).T
24+
mwo = np.array(w).T
25+
mwo[:,2] = fmap.shape[2] - 1 - mwo[:,2]
26+
27+
# check exact symmetry
28+
assert(np.sum(msko[tuple(mwo.T)]) == np.sum(msko[wo]))
29+
30+
# mirror data: [] [0,1] -> [1,0] [0,1]
31+
fmap.raw[tuple(mwo.T)] = fmap.raw[tuple(mw.T)]
32+
33+
# shift right X coordinate: [1,0] [0,1] -> [1,0] [1,2]
34+
mwx = np.array(w).T
35+
mwx = np.vstack((mwx.T,np.zeros(mwx.shape[0]))).T.astype(int)
36+
newx = fmap.raw[tuple(mwx.T)]
37+
newx[newx > -1] = newx[newx > -1] + xmax
38+
fmap.raw[tuple(mwx.T)] = newx
39+
40+
# reflect left X coordinate: [1,0] [1,2] -> [0,1] [1,2]
41+
mwxo = np.array(wo).T
42+
mwxo = np.vstack((mwxo.T,np.zeros(mwxo.shape[0]))).T.astype(int)
43+
newxo = fmap.raw[tuple(mwxo.T)]
44+
newxo[newxo > -1] = xmax - newxo[newxo > -1]
45+
fmap.raw[tuple(mwxo.T)] = newxo
46+
47+
# save
48+
fmap.save_nrrd(output_nrrd)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import voxcell as vc
1+
from voxcell import VoxelData
22
import numpy as np
33
import nrrd
44
import sys
55

66
input_nrrd = sys.argv[1]
77
output_base = sys.argv[2]
88

9-
vd = vc.VoxelData.load_nrrd(input_nrrd)
9+
vd = VoxelData.load_nrrd(input_nrrd)
1010
nrrd.write('{}.nhdr'.format(output_base), vd.raw, detached_header='{}.bin.gz'.format(output_base))

flatmap/code/utils/plot_heatmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def plot_heatmap(array, flatpix, cmap=colorcet.gray, span=None):
5353

5454

5555
if __name__ == "__main__":
56-
import voxcell as vc
56+
from voxcell import VoxelData
5757
from parse_cmap import parse_cmap
5858

5959
cmap = parse_cmap(args.colormap)
6060

6161
LOGGER.info('Loading input data "{}"'.format(args.input_nrrd))
62-
vd = vc.VoxelData.load_nrrd(args.input_nrrd)
62+
vd = VoxelData.load_nrrd(args.input_nrrd)
6363
flatpix = vd.shape[0]
6464
array = vd.raw
6565

flatmap/code/utils/resample.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from voxcell import VoxelData
2+
import flatmap_util as fmutil
3+
import numpy as np
4+
import sys
5+
6+
flatmap_nrrd = sys.argv[1]
7+
rmask_nrrd = sys.argv[2]
8+
output_nrrd = sys.argv[3]
9+
10+
interp = True
11+
if len(sys.argv) > 4:
12+
interp = True if sys.argv[4] in ["true", "True"] else\
13+
(False if sys.argv[4] in ["false", "False"] else int(sys.argv[4]))
14+
15+
fmap, _ = fmutil.load_flatmap(flatmap_nrrd)
16+
rmask = VoxelData.load_nrrd(rmask_nrrd)
17+
18+
w = np.where(rmask.raw)
19+
idx = np.array(w).T
20+
pos = rmask.indices_to_positions(idx)
21+
flatpos = fmutil.lookup(pos, fmap, True, interp)
22+
newfmap = np.full(rmask.shape + (2,), [-1,-1], dtype='float32')
23+
newfmap[w] = flatpos
24+
rmask.with_data(newfmap).save_nrrd(output_nrrd)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import flatmap_util as fmutil
2+
import sys
3+
4+
flatmap_nrrd = sys.argv[1]
5+
output_prefix = sys.argv[2]
6+
7+
fmap, _ = fmutil.load_flatmap(flatmap_nrrd)
8+
fmap.with_data(fmap.raw[:,:,:,0]).save_nrrd('{}/flat_x.nrrd'.format(output_prefix))
9+
fmap.with_data(fmap.raw[:,:,:,1]).save_nrrd('{}/flat_y.nrrd'.format(output_prefix))

flatmap/workflow/applications/config.mk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ FLATVIEW_ANNOTATIONS_FILE := flatview_annotations.png
1616
VOLUME_DECOMPOSITION_NRRD_FILE := hexgrid.nrrd
1717
STREAMLINE_DERIVED_DEPTH_FILE := depth.nrrd
1818
STREAMLINE_DERIVED_THICKNESS_FILE := thickness.nrrd
19-
FLATVIEW_DATA_INPUT_FILE := data.nrrd
20-
FLATVIEW_MORPHO_INPUT_FILE := morphology.json

0 commit comments

Comments
 (0)