-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHDF5_python_extraction.py
More file actions
226 lines (167 loc) · 6.84 KB
/
Copy pathHDF5_python_extraction.py
File metadata and controls
226 lines (167 loc) · 6.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import glob
import os
from tkinter.filedialog import askdirectory
import datetime
import h5py
import numpy as np
from PIL import Image
from tifffile import imwrite
#obtain path to hdf5 file
def collect_paths(directory):
folder = askdirectory()
return glob.glob(os.path.join(folder, "*hdf5"))
#extraxt metadata: filename, timestamp, fPath
def extract_metadata(fPaths):
file_info = []
for fp in fPaths:
try:
with h5py.File(fp, "r") as f:
meta_data = f["ImagingSessionMetaData"]
value = meta_data["Value"][()]
value_string = b''.join(value).decode()
timestamp = int(value_string[:17])
file_info.append({
"filename": os.path.basement(fp),
"timestamp": timestamp,
"path": fp
})
print(f"Metadata extracted successfully!")
except Exception:
print(f"Metadata extraction failed: {fp}")
return sorted(file_info, key=lambda x: x["timestamp"])
#extract subject ID
def extract_subject_ID(fp):
try:
with h5py.File(fp, 'r') as f:
notes_data = f["Notes"]["Value"][()]
notes_string = b''.join(notes_data).decode()
notes_split = notes_string.split('"')
subject_ID = notes_split[11] #MatLab indicated subject ID may be at idex 32 instead of 12 with larger note fields
notes_success = 1 #Might try an if statement to address this if applicable
if subject_ID == "notes":
print(f"No notes recorded in Notes Field: {fp}")
notes_success = 0
return subject_ID
except Exception:
print(f"Notes Field failed for file: {fp}")
notes_success = 0
return None
def loop_eyes():
return [0, 1] #OD, OS
def loop_vid():
return [0, 1, 2]
def loop_frame(num_frames):
return range(num_frames)
#extraxt msb and lsb
def extract_msb_and_lsb(fp, a, b, c):
frame_name = f"/ImageFrame_{a}_1_{b}_{c}"
with h5py.File(fp, 'r') as f:
frame_data = f[frame_name][()].astype(np.uint16)
msb = frame_data[:, :, 0].astype(np.uint16) - 8
msb = msb * 256
lsb = frame_data[:, :, 1].astype(np.uint16)
gray_frame = msb + lsb
gray_frame = np.flipud(gray_frame)
gray_frame = gray_frame[8:, :] # now (472, 640)
return gray_frame
# data is 11 bits and must be converted to 16
#reconstuct frames from funky rgb to 11-bit frames
#apply invert to frames
#frame subtraction, autodetection: detects dark frames, averages the frames, and subtract the average from the entire video
#save tiff file
#save avi file
#run the main process
folder = askdirectory()
fPaths = glob.glob(os.path.join(folder, '*.hdf5'))
file_info = []
#defines info extraction from files
def extract_info_chronologically():
file_info.clear()
for fp in fPaths:
with h5py.File(fp, 'r') as f: #read hdf5 file
try:
meta_data = f["ImagingSessionMetaData"] #find the image session metadata in the file
value = meta_data["Value"][()] #find the value in the metadata
value_string = b''.join(value).decode() #combine array of bytes into a string and convert to python string
timestamp = int(value_string[:17]) #take the first 17 character, this is the datetime
filename = os.path.basename(fp) #retrieve filename
#list structure
file_info.append({
"filename" : filename,
"timestamp" : timestamp,
"path" : fp
})
except:
print("Extraction failed. Imaging Session Meta Data not found. ")
file_info_sorted = sorted(file_info, key=lambda x: x["timestamp"]) #sort by timestamp
print("Extraction completed successfully.")
return file_info_sorted
extract_info_chronologically()
def load_files():
file_info_sorted = extract_info_chronologically()
for item in file_info_sorted:
fp = item["path"]
filename = item["filename"]
date_timestamp = os.path.getmtime(fp)
date_string = datetime.datetime.fromtimestamp(date_timestamp).strftime('%Y_%m_%d')
print(fp, filename, date_string)
def subject_ID():
file_info = load_files()
with h5py.File(fp, 'r') as f:
try:
notes_data = f["Notes"]["Value"][()]
notes_string = b''.join(notes_data).decode()
notes_split = notes_string.split('"')
subject_ID = notes_split[11] # MatLab indicated subject ID may be at idex 32 instead of 12 with larger note fields
notes_success = 1 # Might try an if statement to address this if applicable
if subject_ID == "notes":
print(f"No notes recorded in Notes Field: {fp}")
notes_success = 0
except Exception:
print(f"Notes Field failed for file: {fp}")
notes_success = 0
#defines conversion function for the frame
def convert_funky_rgb_frame_to_11_bit_frame(frame):
frame = frame.astype('uint16')
g = frame[:, :, 1] # 8 least significant bits, all rows/columns but green channel
r = frame[:, :, 0] # 8 most significant bits, all rows/columns bur red channel
r = r - 8
r = r * 256
combined = r + g
return combined
frames = np.zeros((480, 640), dtype='uint16')
good_frame = []
alist = []
with h5py.File('ec00615c-fa36-4732-b9dc-72c7901e7f5e.hdf5', "r") as f:
# Print keys
# print("Keys: %s" % f.keys())
""" instead of hardcoding the 210 frames
have the system read the number of frames present in the file first
then range will be this variable
num_frames = lens(f.keys)
for i in range(num_frames):
"""
first = 1
for i in range(0,210):
# get first object name/key
a_group_key = list(f.keys())[i]
# get the object type for a_group_key
# print(type(f[a_group_key]))
# If a_group_key is a group name,
# this gets the object names in the group and returns as a list
#data = list(f[a_group_key])
# If a_group_key is a dataset name,
# this gets the dataset values and returns as a list
#data = list(f[a_group_key])
# preferred methods to get dataset values:
#ds_obj = f[a_group_key] # returns as a h5py dataset object
ds_arr = f[a_group_key][()] # returns as a numpy array
good_frame = convert_funky_rgb_frame_to_11_bit_frame(ds_arr)
# x = good_frame[:,0]
# y = good_frame[0,:]
alist.append(good_frame)
img = Image.fromarray(good_frame)
Image.fromarray(good_frame.astype('uint16'), mode=None).save('pic2_2.tif')
imwrite('multipage.tif', alist)
# img.show()
print('hi')