def from_image(self, image, affine=None, method=None, fill=<NUM_LIT:0>, dtype=None,<EOL>native_to_vertex_matrix=None, weights=None):
if native_to_vertex_matrix is None:<EOL><INDENT>native_to_vertex_matrix = np.eye(<NUM_LIT:4>)<EOL><DEDENT>native_to_vertex_matrix = to_affine(native_to_vertex_matrix)<EOL>if pimms.is_str(image): image = load(image)<EOL>if is_image(image):<EOL><INDENT>if affine is None: affine = image.affine<EOL>image = image.get_data()<EOL><DEDENT>image = np.asarray(image)<EOL>if affine is None:<EOL><INDENT>from neuropythy.freesurfer import tkr_vox2ras<EOL>affine = np.dot(np.linalg.inv(native_to_vertex_matrix),<EOL>tkr_vox2ras(image.shape[<NUM_LIT:0>:<NUM_LIT:3>], (<NUM_LIT:1.0>, <NUM_LIT:1.0>, <NUM_LIT:1.0>)))<EOL>ijk0 = np.asarray(image.shape) * <NUM_LIT:0.5><EOL>affine = to_affine(([[-<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:0>],[<NUM_LIT:0>,<NUM_LIT:0>,-<NUM_LIT:1>],[<NUM_LIT:0>,<NUM_LIT:1>,<NUM_LIT:0>]], ijk0), <NUM_LIT:3>)<EOL><DEDENT>else: affine = to_affine(affine, <NUM_LIT:3>)<EOL>affine = np.dot(native_to_vertex_matrix, affine)<EOL>affine = npla.inv(affine)<EOL>if method is not None: method = method.lower()<EOL>if method is None or method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>method = '<STR_LIT>' if np.issubdtype(image.dtype, np.inexact) else '<STR_LIT>'<EOL><DEDENT>if dtype is None: dtype = image.dtype<EOL>xyz = affine.dot(np.vstack((self.coordinates, np.ones(self.vertex_count))))[<NUM_LIT:0>:<NUM_LIT:3>]<EOL>res = np.full((self.vertex_count,) + image.shape[<NUM_LIT:3>:], fill, dtype=dtype)<EOL>if method == '<STR_LIT>':<EOL><INDENT>ijk = np.asarray(np.round(xyz), dtype=np.int)<EOL>ok = np.all((ijk >= <NUM_LIT:0>) & [ii < sh for (ii,sh) in zip(ijk, image.shape)], axis=<NUM_LIT:0>)<EOL>res[ok] = image[tuple(ijk[:,ok])]<EOL>return res<EOL><DEDENT>if weights is None: weights = np.ones(image.shape)<EOL>elif pimms.is_str(weights): weights = load(weights).get_data()<EOL>elif is_image(weights): weights = weights.get_data()<EOL>else: weights = np.asarray(weights)<EOL>mins = np.floor(xyz)<EOL>maxs = np.ceil(xyz)<EOL>ok = np.all((mins >= <NUM_LIT:0>) & [ii < sh for (ii,sh) in zip(maxs, image.shape[<NUM_LIT:0>:<NUM_LIT:3>])], axis=<NUM_LIT:0>)<EOL>(mins,maxs,xyz) = [x[:,ok] for x in (mins,maxs,xyz)]<EOL>voxs = np.asarray([mins,<EOL>[mins[<NUM_LIT:0>], mins[<NUM_LIT:1>], maxs[<NUM_LIT:2>]],<EOL>[mins[<NUM_LIT:0>], maxs[<NUM_LIT:1>], mins[<NUM_LIT:2>]],<EOL>[mins[<NUM_LIT:0>], maxs[<NUM_LIT:1>], maxs[<NUM_LIT:2>]],<EOL>[maxs[<NUM_LIT:0>], mins[<NUM_LIT:1>], mins[<NUM_LIT:2>]],<EOL>[maxs[<NUM_LIT:0>], mins[<NUM_LIT:1>], maxs[<NUM_LIT:2>]],                           <EOL>[maxs[<NUM_LIT:0>], maxs[<NUM_LIT:1>], mins[<NUM_LIT:2>]],<EOL>maxs],<EOL>dtype=np.int)<EOL>wgts_tri = np.asarray([np.prod(<NUM_LIT:1> - np.abs(xyz - row), axis=<NUM_LIT:0>) for row in voxs])<EOL>wgts_wgt = np.asarray([weights[tuple(row)] for row in voxs])<EOL>if len(wgts_wgt.shape) > len(wgts_tri.shape):<EOL><INDENT>for _ in range(len(wgts_wgt.shape) - len(wgts_tri.shape)):<EOL><INDENT>wgts_tri = np.expand_dims(wgts_tri, -<NUM_LIT:1>)<EOL><DEDENT><DEDENT>wgts = wgts_tri * wgts_wgt<EOL>wgts *= zinv(np.sum(wgts, axis=<NUM_LIT:0>))<EOL>vals = np.asarray([image[tuple(row)] for row in voxs])<EOL>res[ok] = np.sum(wgts * vals, axis=<NUM_LIT:0>)<EOL>return res<EOL>
mesh.from_image(image) interpolates the given 3D image array at the values in the given 
  mesh's coordinates and yields the property that results. If image is given as a string,
  this function will attempt to load it as an mgh/mgz file or a nifti file.

The following options may be used:
  * affine (default: None) may specify the affine transform that aligns the vertex
    coordinates with the image (vertex-to-voxel transform). If None, then uses a
    FreeSurfer-like transformby default. Note that if image is an MGHImage or a Nifti1Image,
    then the included affine transform included in the header will be used by default if
    None is given.
  * method (default: None) may specify either 'linear' or 'nearest'; if None, then the
    interpolation is linear when the image data is real and nearest otherwise.
  * fill (default: 0) values filled in when a vertex falls outside of the image.
  * native_to_vertex_matrix (default: None) may optionally give a final transformation that
    converts from native subject orientation encoded in images to vertex positions.
  * weights (default: None) may optionally provide an image whose voxels are weights to use
    during the interpolation; these weights are in addition to trilinear weights and are
    ignored in the case of nearest interpolation.
  * native_to_vertex_matrix (default: None) specifies a matrix that aligns the surface
    coordinates with their subject's 'native' orientation; None is equivalnet to the
    identity matrix.