-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_static.py
198 lines (165 loc) · 5.79 KB
/
build_static.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
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
import os
import shutil
import json
import numpy as np
from jinja2 import Environment, FileSystemLoader
from app import get_cases_from_files
def create_3d_data():
try:
# Load NIfTI files
image_path = os.path.join('static', 'images', 'image.nii.gz')
label_path = os.path.join('static', 'images', 'label.nii.gz')
if not os.path.exists(image_path) or not os.path.exists(label_path):
raise FileNotFoundError('NIfTI files not found')
import nibabel as nib
# Load image and label data
image_nii = nib.load(image_path)
label_nii = nib.load(label_path)
# Get the data arrays
image_data = image_nii.get_fdata()
label_data = label_nii.get_fdata()
# Normalize image data to [0, 1]
image_min = np.min(image_data)
image_max = np.max(image_data)
image_data = (image_data - image_min) / (image_max - image_min)
# Convert to float32
image_data = image_data.astype(np.float32)
label_data = label_data.astype(np.float32)
# Sample the data to reduce size (take every 2nd point)
stride = 2
image_data = image_data[::stride, ::stride, ::stride]
label_data = label_data[::stride, ::stride, ::stride]
# Get dimensions after sampling
width, height, depth = image_data.shape
# Convert to points format
threshold = 0.1
points = []
labels = []
for x in range(width):
for y in range(height):
for z in range(depth):
val = float(image_data[x,y,z])
if val > threshold:
points.append({
'x': int(x),
'y': int(y),
'z': int(z),
'v': val
})
if label_data[x,y,z] > 0:
labels.append({
'x': int(x),
'y': int(y),
'z': int(z)
})
# Create the data structure
data = {
'dimensions': {
'width': int(width),
'height': int(height),
'depth': int(depth)
},
'image': points,
'label': labels
}
print(f'Successfully loaded 3D data with shape: {image_data.shape}')
print(f'Image range: [{np.min(image_data)}, {np.max(image_data)}]')
return data
except Exception as e:
print(f'Error loading 3D data: {str(e)}')
# Return a small test cube as fallback
size = 16
points = []
labels = []
# Create a simple cube
for x in range(4, 12):
for y in range(4, 12):
for z in range(4, 12):
points.append({
'x': x,
'y': y,
'z': z,
'v': 1.0
})
labels.append({
'x': x,
'y': y,
'z': z
})
return {
'dimensions': {
'width': size,
'height': size,
'depth': size
},
'image': points,
'label': labels
}
def build_static_site():
# Create build directory
if os.path.exists('build'):
shutil.rmtree('build')
os.makedirs('build')
os.makedirs(os.path.join('build', 'api'))
# Copy static files
shutil.copytree('static', 'build/static')
# Setup Jinja2 environment
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('index.html')
# Get cases data
cases = get_cases_from_files()
# Create a simple url_for replacement
def url_for(endpoint, filename=None):
if endpoint == 'static':
return f'static/{filename}'
return ''
# Render main template
html_content = template.render(
cases=cases,
url_for=url_for
)
# Write index.html
with open('build/index.html', 'w', encoding='utf-8') as f:
f.write(html_content)
# Generate and write 3D data
data = create_3d_data()
# Save data with pretty printing for debugging
api_paths = [
os.path.join('build', 'api', 'get_3d_data'),
os.path.join('build', 'get_3d_data'),
os.path.join('build', 'RCMed', 'api', 'get_3d_data')
]
# Create RCMed/api directory
os.makedirs(os.path.join('build', 'RCMed', 'api'), exist_ok=True)
# Write data to all paths
for path in api_paths:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w') as f:
json.dump(data, f, indent=2)
print("Generated test 3D data with dimensions:", data['dimensions'])
print("Number of points:", len(data['image']))
print("Number of labels:", len(data['label']))
# Calculate value range from points
if data['image']:
values = [p['v'] for p in data['image']]
print("Data ranges - Image:",
f"min={min(values)}, max={max(values)}")
# Create 404 page
with open('build/404.html', 'w', encoding='utf-8') as f:
f.write('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>RCMed - Medical Image Analysis</title>
<script>
window.location.href = "/RCMed/";
</script>
</head>
<body>
<p>Redirecting to <a href="/RCMed/">RCMed</a>...</p>
</body>
</html>
''')
if __name__ == '__main__':
build_static_site()