-
Notifications
You must be signed in to change notification settings - Fork 0
/
boldli.py
74 lines (62 loc) · 2.09 KB
/
boldli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import print_function
import numpy as np
# For loading/saving the images
from nipy.core.api import Image
from nipy import load_image, save_image
from nipype.interfaces import dcmstack
class ImageManipulatingLibrary:
##
# Load the image
#
# @param fn The string specifying the path to the file
#
# @returns img The image sequence as an Image object
# @returns coords The coordinate system for the image sequence
def loadBOLD(fn):
img = load_image(fn)
coords = img.coordmap
return img, coords
##
# Isolate a single volume from the sequence
#
# @param seq The image sequence as an Image object
# @param volNum An int specifying the volume to isolate
#
# @returns vol The isolated volume
def isolateVolume(seq, volNum=0):
# Check the shape of the image
print(seq.shape)
if len(seq.shape) == 4:
# Pull out the volume of interest from the sequence
vol = seq[:,:,:, volNum].get_data()[:,:,:, None]
# Make sure that the volume only has 3 dimensions
vol = np.squeeze(vol)
elif len(seq.shape) == 3:
vol = seq.get_data()
else:
print("The volume must have 3 or 4 dimensions. Given volume has shape:", seq.shape)
return vol
##
# Convert a 4D numpy array to an Image object
#
# @param seq The image sequence as a 4D numpy array
# @param coords The coordinates for the image sequence
#
# @returns seqImg The sequence as an Image object
def convertArrayToImage(seq, coords):
# Condense the replicated sequence
seqStack = np.stack(seq, axis=-1)
# Convert the image sequence into an Image
seqImg = Image(seqStack, coords)
return seqImg
##
# Save the standardized image sequence
#
# @param seq The standardized image sequence
# @param fn The location to save the image sequence to
def saveBOLD(seq, fn):
save_image(seq, fn)
def main():
print("Image Manipulating Library")
if __name__ == "__main__":
main()