Skip to content

Commit

Permalink
Add version check and recover fsinfo, if not using the fixed lapy ver…
Browse files Browse the repository at this point in the history
…sion.

Update the names of the command line arguments (--file and --backup)
To reduce unnecessary file duplication, only save noorient, if the surface was actually not oriented.
  • Loading branch information
dkuegler committed Aug 12, 2024
1 parent 170435e commit e818b44
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
8 changes: 3 additions & 5 deletions recon_surf/recon-surf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,10 @@ for hemi in lh rh; do
cmd="recon-all -subject $subject -hemi $hemi -fix -no-isrunning $hiresflag $fsthreads"
RunIt "$cmd" $LF $CMDF

# rename the freesurfer preaparc surface
cmd="mv $sdir/$hemi.orig.premesh $sdir/$hemi.orig.premesh.noorient"
RunIt "$cmd" $LF $CMDF

# fix the surfaces if they are corrupt
cmd="$python ${binpath}rewrite_oriented_surface.py -i $sdir/$hemi.orig.premesh.noorient -o $sdir/$hemi.orig.premesh"
cmd="$python ${binpath}rewrite_oriented_surface.py --file $sdir/$hemi.orig.premesh --backup $sdir/$hemi.orig.premesh.noorient"
RunIt "$cmd" $LF $CMDF
cmd="$python ${binpath}rewrite_oriented_surface.py --file $sdir/$hemi.orig --backup $sdir/$hemi.orig.noorient"
RunIt "$cmd" $LF $CMDF

cmd="recon-all -subject $subject -hemi $hemi -autodetgwstats -white-preaparc -cortex-label -no-isrunning $hiresflag $fsthreads"
Expand Down
64 changes: 43 additions & 21 deletions recon_surf/rewrite_oriented_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import shutil
# IMPORTS
import sys
import argparse
Expand All @@ -36,18 +36,19 @@ def make_parser() -> argparse.ArgumentParser:
"correctly oriented) under a given name",
)
parser.add_argument(
"--input", "-i",
"--file", "-f",
type=Path,
dest="input_surf",
help="path to input surface",
dest="file",
help="path to surface to check and fix",
required=True,
)
parser.add_argument(
"--output", "-o",
"--backup",
type=Path,
dest="output_surf",
help="path to output surface",
required=True,
dest="backup",
help="if the surface is corrupted, create a backup of the original surface. "
"Default: no backup.",
default=None,
)
parser.add_argument(
"--version",
Expand All @@ -57,16 +58,27 @@ def make_parser() -> argparse.ArgumentParser:
return parser


def resafe_surface(insurf: Path | str, outsurf: Path | str) -> None:
def resafe_surface(
surface_file: Path | str,
surface_backup: Path | str | None = None,
) -> bool:
"""
Take path to insurf and rewrite it to outsurf thereby fixing improperly oriented triangles.
Take path to surface_file and rewrite it to fix improperly oriented triangles.
If the surface is not oriented and surface_backup is set, rename the old
surface_file to surface_backup. Else just overwrite with the corrected surface.
Parameters
----------
insurf : Path, str
surface_file : Path, str
Path and name of input surface.
outsurf : Path, str
surface_backup : Path, str, optional
Path and name of output surface.
Returns
-------
bool
Whether the surface was rewritten.
"""
import getpass
try:
Expand All @@ -77,26 +89,36 @@ def resafe_surface(insurf: Path | str, outsurf: Path | str) -> None:
from os import environ
environ.setdefault("USERNAME", "UNKNOWN")

triamesh = lapy.TriaMesh.read_fssurf(str(insurf))
triamesh = lapy.TriaMesh.read_fssurf(str(surface_file))
fsinfo = triamesh.fsinfo

# make sure the triangles are oriented (normals pointing to the same direction
if not triamesh.is_oriented():
if surface_backup is not None:
print(f"Renaming {surface_file} to {surface_backup}")
shutil.move(surface_file, surface_backup)

print("Surface was not oriented, flipping corrupted normals.")
triamesh.orient_()
else:
print("Surface was oriented.")

triamesh.write_fssurf(str(outsurf))
from packaging.version import Version
if Version(lapy.__version__) <= Version("1.0.1"):
print(f"lapy version {lapy.__version__}<=1.0.1 detected, fixing fsinfo.")
triamesh.fsinfo = fsinfo

triamesh.write_fssurf(str(surface_file))
return True
else:
print("Surface was already oriented.")
return False


if __name__ == "__main__":
# Command Line options are error checking done here
parser = make_parser()
args = parser.parse_args()
surf_in = args.input_surf
surf_out = args.output_surf

print(f"Reading in surface: {surf_in} ...")
resafe_surface(surf_in, surf_out)
print(f"Outputting surface as: {surf_out}")
print(f"Reading in surface: {args.file} ...")
if resafe_surface(args.file, args.backup):
print(f"Outputting surface as: {args.file}")
sys.exit(0)

0 comments on commit e818b44

Please sign in to comment.