Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] removes memory leak issue by removing c code and using numpy/scipy functions #9

Merged
merged 24 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
64db745
remove c-extension: minimal gain and memory leaks
bpinsard Aug 9, 2022
5c91b36
cleanup wip
bpinsard Aug 7, 2023
dd2f582
add regression tests for siemens_B to check that numpy legendre are e…
bpinsard Aug 23, 2023
d80a60f
fix type change, keep float32
bpinsard Aug 23, 2023
704bc0d
force float32
bpinsard Aug 23, 2023
6d8c492
adjust test precision
bpinsard Aug 23, 2023
66ddc6c
cleanup
bpinsard Jan 9, 2024
0d44a01
fix tests by replacing the custom n-d meshgrid with numpy one
bpinsard Jan 9, 2024
53d1267
more cleanup of no longer used code
bpinsard Jan 9, 2024
ac02f6a
other suggestions from code review
bpinsard Jan 10, 2024
6a38f8c
Apply suggestions from code review
bpinsard Jan 10, 2024
9bd8691
more cleanup, remove unnecessary fill operation, use numpy built-in f…
bpinsard Jan 10, 2024
f40cfd0
test all dimension of the displacement field
bpinsard Jan 10, 2024
b10f681
reg_test on all 3 dims, factor computation, more cleanup
bpinsard Jan 10, 2024
7688720
fix test data loading and packaging
bpinsard Jan 10, 2024
c43c3fa
setup cleanup
bpinsard Jan 10, 2024
aefe4e5
minor opt: do not need filling with zeros
bpinsard Jan 10, 2024
2475605
rewriting parser for robustness/readability
bpinsard Jan 12, 2024
e74adbb
fix regression test data for parser error
bpinsard Jan 12, 2024
4a3c420
harmonize naming
bpinsard Jan 15, 2024
7af0829
fix setup.py after rebase
bpinsard Jan 17, 2024
201e172
back to python2
bpinsard Jan 17, 2024
1ab0ff0
more python2 downgrading, tests pass in py2 venv
bpinsard Jan 17, 2024
f001ba0
fix deprec warning for regexp
bpinsard Jan 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 54 additions & 111 deletions gradunwarp/core/coeffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,137 +78,80 @@ def get_siemens_coef(cfile):
# allegra is slightly different
if cfile.startswith('allegra'):
coef_array_sz = 15
ax = np.zeros((coef_array_sz, coef_array_sz))
ay = np.zeros((coef_array_sz, coef_array_sz))
az = np.zeros((coef_array_sz, coef_array_sz))
bx = np.zeros((coef_array_sz, coef_array_sz))
by = np.zeros((coef_array_sz, coef_array_sz))
bz = np.zeros((coef_array_sz, coef_array_sz))
txt_var_map = {'Alpha_x': ax,
'Alpha_y': ay,
'Alpha_z': az,
'Beta_x': bx,
'Beta_y': by,
'Beta_z': bz}

txt_var_map = create_txt_var_map(coef_array_sz)

coef_file_parse(cfile, txt_var_map)

return Coeffs(ax, ay, az, bx, by, bz, R0_m)
return Coeffs(
txt_var_map['alpha_y'],
txt_var_map['alpha_z'],
txt_var_map['alpha_x'],
txt_var_map['beta_y'],
txt_var_map['beta_x'],
txt_var_map['beta_z'],
R0_m)


def get_ge_coef(cfile):
''' Parse the GE .coef file.
'''
ax = np.zeros((ge_cas, ge_cas))
ay = np.zeros((ge_cas, ge_cas))
az = np.zeros((ge_cas, ge_cas))
bx = np.zeros((ge_cas, ge_cas))
by = np.zeros((ge_cas, ge_cas))
bz = np.zeros((ge_cas, ge_cas))
txt_var_map = {'Alpha_x': ax,
'Alpha_y': ay,
'Alpha_z': az,
'Beta_x': bx,
'Beta_y': by,
'Beta_z': bz}
txt_var_map = create_txt_var_map(coef_array_sz)

coef_file_parse(cfile, txt_var_map)

return Coeffs(ax, ay, az, bx, by, bz, R0_m)
return Coeffs(
txt_var_map['alpha_y'],
txt_var_map['alpha_z'],
txt_var_map['alpha_x'],
txt_var_map['beta_y'],
txt_var_map['beta_x'],
txt_var_map['beta_z'],
R0_m)

def grad_file_parse(gfile, txt_var_map):
''' a separate function because GE and Siemens .coef files
have similar structure

modifies txt_var_map in place
'''
gf = open(gfile, 'r')
line = next(gf)
# skip the comments
while not line.startswith('#*] END:'):
line = next(gf)

# get R0
line = next(gf)
line = next(gf)
line = next(gf)
R0_m = float(line.strip().split()[0])

# go to the data
line = next(gf)
line = next(gf)
line = next(gf)
line = next(gf)
line = next(gf)
line = next(gf)
line = next(gf)

xmax = 0
ymax = 0

while 1:
lindex = line.find('(')
rindex = line.find(')')
if lindex == -1 and rindex == -1:
break
arrindex = line[lindex+1:rindex]
xs, ys = arrindex.split(',')
x = int(xs)
y = int(ys)
if x > xmax:
xmax = x
if y > ymax:
ymax = y
if line.find('A') != -1 and line.find('x') != -1:
txt_var_map['Alpha_x'][x,y] = float(line.split()[-2])
if line.find('A') != -1 and line.find('y') != -1:
txt_var_map['Alpha_y'][x,y] = float(line.split()[-2])
if line.find('A') != -1 and line.find('z') != -1:
txt_var_map['Alpha_z'][x,y] = float(line.split()[-2])
if line.find('B') != -1 and line.find('x') != -1:
txt_var_map['Beta_x'][x,y] = float(line.split()[-2])
if line.find('B') != -1 and line.find('y') != -1:
txt_var_map['Beta_y'][x,y] = float(line.split()[-2])
if line.find('B') != -1 and line.find('z') != -1:
txt_var_map['Beta_z'][x,y] = float(line.split()[-2])
try:
line = next(gf)
except StopIteration:
break

# just return R0_m but also txt_var_map is returned
with open(gfile, 'r') as gf:
for line in gf:
re_search = re.search("(?P<no>\d+)[\s]+(?P<aorb>[AB])[\s]*\(\s*(?P<x>\d+),\s*(?P<y>\d+)\)\s+(?P<spectrum>[\-]?\d+\.\d+)\s+(?P<axis>[xyz])", line)
if re_search:
re_res = re_search.groupdict()
alphabeta = 'alpha' if re_res['aorb'] == 'A' else 'beta'
x, y = int(re_res['x']),int(re_res['y'])
field = "%s_%s"%(alphabeta, re_res['axis'])
txt_var_map[field][x, y] = float(re_res['spectrum'])
xmax, ymax = max(x, xmax), max(y, ymax)
else:
re_search = re.search("(?P<R0>\d+\.\d+) m = R0", line)
if re_search:
R0_m = float(re_search.group('R0'))
return R0_m, (xmax, ymax)

def create_txt_var_map(coef_array_sz):
txt_var_map = {}
for ab in ['alpha', 'beta']:
for axis in 'xyz':
txt_var_map['%s_%s'%(ab,axis)] = np.zeros((coef_array_sz,)*2)
return txt_var_map

def get_siemens_grad(gfile):
''' Parse the siemens .grad file
'''
coef_array_sz = siemens_cas
# allegra is slightly different
if gfile.startswith('coef_AC44'):
coef_array_sz = 15
ax = np.zeros((coef_array_sz, coef_array_sz))
ay = np.zeros((coef_array_sz, coef_array_sz))
az = np.zeros((coef_array_sz, coef_array_sz))
bx = np.zeros((coef_array_sz, coef_array_sz))
by = np.zeros((coef_array_sz, coef_array_sz))
bz = np.zeros((coef_array_sz, coef_array_sz))
txt_var_map = {'Alpha_x': ax,
'Alpha_y': ay,
'Alpha_z': az,
'Beta_x': bx,
'Beta_y': by,
'Beta_z': bz}
coef_array_sz = 15 if gfile.startswith('coef_AC44') else siemens_cas

R0_m, max_ind = grad_file_parse(gfile, txt_var_map)
ind = max(max_ind)

# pruned alphas and betas
ax = ax[:ind+1, :ind+1]
ay = ay[:ind+1, :ind+1]
az = az[:ind+1, :ind+1]
bx = bx[:ind+1, :ind+1]
by = by[:ind+1, :ind+1]
bz = bz[:ind+1, :ind+1]

return Coeffs(ax, ay, az, bx, by, bz, R0_m)
txt_var_map = create_txt_var_map(coef_array_sz)
print(txt_var_map)

R0_m, max_ind = grad_file_parse(gfile, txt_var_map)
ind = max(max_ind) + 1

return Coeffs(
txt_var_map['alpha_x'][:ind, :ind],
txt_var_map['alpha_y'][:ind, :ind],
txt_var_map['alpha_z'][:ind, :ind],
txt_var_map['beta_x'][:ind, :ind],
txt_var_map['beta_y'][:ind, :ind],
txt_var_map['beta_z'],
R0_m)
198 changes: 0 additions & 198 deletions gradunwarp/core/interp3_ext.c

This file was deleted.

Loading
Loading