Skip to content

Commit

Permalink
Merge pull request #30 from Neuroinflab/dev/27_fsaverage_to_mesh
Browse files Browse the repository at this point in the history
forward solver refactor to allow arbitrary coefficient mfem solving
  • Loading branch information
mdovgialo authored Sep 12, 2024
2 parents ddba11d + d2cbd16 commit 7d5054e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 10 additions & 4 deletions extras/draw_cross_section_nifty.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pandas as pd
import pylab as pb

from nibabel.affines import apply_affine

def create_meshgrid_from_affine(affine, data):
x, y, z, comp = data.shape
Expand All @@ -21,10 +21,10 @@ def create_meshgrid_from_affine(affine, data):


def main():
parser = argparse.ArgumentParser(description="Draw crossection nifty files")
parser = argparse.ArgumentParser(description="Draw crossection nifty files, works well only with no skew or rotation, rectilinear affine")
parser.add_argument("files", nargs='+', type=str, help="nifty files")
parser.add_argument("-x", type=int, help="Slice at X coordinate (in voxels)", default=130)
parser.add_argument("-y", type=int, help="Slice at Y coordinate (in voxels)", default=118)
parser.add_argument("-x", type=int, help="Slice at X coordinate (in meters)", default=0)
parser.add_argument("-y", type=int, help="Slice at Y coordinate (in meters)", default=0)

args = parser.parse_args()

Expand All @@ -36,6 +36,12 @@ def main():
name = os.path.basename(file)
correction = nibabel.load(file)
vol_data = correction.get_fdata()

inv_affine = np.linalg.inv(correction.affine)

x_vox, y_vox, z_vox = apply_affine(inv_affine, [args.x, args.y, 0])
slice_of_interest = np.s_[int(x_vox), int(y_vox), :]

meshgrid = create_meshgrid_from_affine(correction.affine, vol_data) / 1000 # mm to meters
data_slice = vol_data[slice_of_interest]
data_x = meshgrid[slice_of_interest]
Expand Down
14 changes: 9 additions & 5 deletions src/kesi/mfem_solver/forward_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def __init__(self, meshfile, conductivities, boundary_value=0, additional_refine
self.solution_interpolated = None
self.interpolator = interpolator

def solve(self, xyz, csd):
"""
saves solution into self.solution as MFEM gridfunction and self.solution_interpolated for sampling at any point
"""
coeff = csd_distribution_coefficient(xyz, csd)
def solve_coeff(self, coeff):
solution = mfem_solve_mesh(csd_coefficient=coeff,
mesh=self.mesh,
boundary_potential=self.boundary_value,
Expand All @@ -64,6 +60,14 @@ def solve(self, xyz, csd):
self.solution_interpolated = self.interpolator(verts_triangulation, sol)
return solution

def solve(self, xyz, csd):
"""
saves solution into self.solution as MFEM gridfunction and self.solution_interpolated for sampling at any point
"""
coeff = csd_distribution_coefficient(xyz, csd)
return self.solve_coeff(coeff)


def sample_solution_probe(self, x, y, z):
return self.solution_interpolated([x, y, z])[0]

Expand Down

0 comments on commit 7d5054e

Please sign in to comment.