首页 > 解决方案 > 脚本使用 .nrrd 文件但保存了 .npy

问题描述

我解释我的问题:

背景:我使用该文件find_borders.py来纠正这样的小脑图像的特定部分。

从这种情况:

原始图像

对此: 在此处输入图像描述

文件find_borders.py帮助我使用 GUI 更正保存的文件,如annotation_corrected5.npy

下一步将使用使用此工具创建的everyslice 来构建这样的 3D 结构(这是另一个区域,而不是主题区域):

另一个地区

现在:原始文件是一个名为 .nrrd 的文件annotations.nrrd。但我修改的最后一次保存将始终是.npy文件。

我只是尝试从以下位置调用 .npy 文件:

ann, h = nrrd.read(data_path+"data/annotations.nrrd")

至:

ann, h = np.load(data_path+"data/annotation_corrected5.npy")

但它返回:

Traceback (most recent call last):
  File "/Users/*****/github/full_mouse_cerebellum/compute_results.py", line 29, in <module>
    ann, h = np.load(data_path+"data/annotation_corrected5.npy")
ValueError: too many values to unpack (expected 2)

所以我尝试纠正:

ann = np.load(data_path+"data/annotation_corrected5.npy")

h = np.load(data_path+"data/annotation_corrected5.npy")

并且代码运行。

问题将出在带有“RuntimeWarning”的错误行上:

dens cell  (528, 320, 456)
orientation:  (3, 528, 320, 456)
(528, 320, 456)
[10677, 10676, 10675]
10677
molecular layer
Purkinje layer
/Users/*******/github/full_mouse_cerebellum/compute_results.py:91: RuntimeWarning: invalid value encountered in double_scalars
  cell_densities.append(number_cells[-1]/volumes[-1])
/Users/*******/github/full_mouse_cerebellum/compute_results.py:92: RuntimeWarning: invalid value encountered in double_scalars
  neuron_densities.append(number_neurons[-1]/volumes[-1])
granular layer
['Crus 1, molecular layer', 'Crus 1, Purkinje layer', 'Crus 1, granular layer', 'Crus 1']
i_molecular:  0
Volume molecular layer: 3.246390625
Volume purkinje layer: 0.0
Volume granular layer: 2.9016875
2937.5
0.0
[12.5 12.5 12.5 ... 62.5 62.5 62.5]
Mean down layer:  0.0
Mean up layer:  0.0
/Users/*******/github/full_mouse_cerebellum/compute_results.py:145: RuntimeWarning: invalid value encountered in true_divide
  + down_layer_distance)
Volume Stellate layer: 0.0
Volume Basket layer: 3.246390625
Total volume: 6.148078125
Voxels Granular layer: 185708
Voxels Purkinje layer: 0
Voxels Stellate layer: 0
Voxels Basket layer: 207769
Traceback (most recent call last):
  File "/Users/*******/github/full_mouse_cerebellum/compute_results.py", line 173, in <module>
    print("Density stellate cells - max: ", np.amax(density_stellate_all),", min: ", np.amin(density_stellate_all))
  File "<__array_function__ internals>", line 6, in amax
  File "/Users/*******/opt/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 2706, in amax
    keepdims=keepdims, initial=initial, where=where)
  File "/Users/*******/opt/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation maximum which has no identity

第一部分是正确的(显然),但我无法解决这个问题。

我想转换.npy.nrrd直接绕过这个问题。

可能是个好主意吗?

如果是:有人可以帮助我进行转换吗?

非常感谢您的耐心。

标签: pythonnumpy

解决方案


解决了:

在最后一行代码中,我写了这个:

nrrd.write(join(DATA_FOLDER, "annotation_corrected5.nrrd"), ann)
print(ann.shape)

因此,文件 .npy 将保存在 .nrrd 文件中。


推荐阅读