-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_mdoc.py
57 lines (40 loc) · 1.33 KB
/
sort_mdoc.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
#!/usr/bin/python
# # -*- coding: utf-8 -*-
# require mdocfile from Alister Burt
# pip install mdocfile
# 2023/08/24, Huy Bui
# Note: if your mdoc file has more entry, you need to put in similarly in the current section
import sys
import mdocfile
from pathlib import Path
from mdocfile.data_models import Mdoc, MdocGlobalData, MdocSectionData
inputmdoc = sys.argv[1]
df = mdocfile.read(inputmdoc)
df2 = df.sort_values('TiltAngle', ignore_index=True)
df2['ZValue'] = df2.index
outputmdoc = inputmdoc.replace(".mdoc", "_sorted.mdoc")
print(f'Writing {outputmdoc}')
global_data = MdocGlobalData (
DataMode = df2['DataMode'].iloc[0],
ImageSize = df2['ImageSize'].iloc[0],
PixelSpacing = df2['PixelSpacing'].iloc[0],
Voltage = df2['Voltage'].iloc[0]
)
mdoc = Mdoc(
titles=df2['titles'].iloc[0],
global_data=global_data,
section_data=[ ]
)
for ind in df2.index:
current_section = MdocSectionData(
ZValue=df2['ZValue'].iloc[ind],
TiltAngle=df2['TiltAngle'].iloc[ind],
StagePosition=df2['StagePosition'].iloc[ind],
ExposureDose=df2['ExposureDose'].iloc[ind],
SubFramePath=df2['SubFramePath'].iloc[ind],
DateTime=df2['DateTime'].iloc[ind],
NumSubFrames=df2['NumSubFrames'].iloc[ind],
)
mdoc.section_data.append(current_section)
with open(outputmdoc, mode='w+') as file:
file.write(mdoc.to_string())