<commit_before>import os
import struct
import tempfile
import warnings
import shlex
import subprocess as sp

import numpy as np

from . import db
from . import anat

def get_paths(subject, hemi, type="patch"):
    base = os.path.join(os.environ['SUBJECTS_DIR'], subject)
    if type == "patch":
        return os.path.join(base, "surf", hemi+".{name}.patch.3d")
    elif type == "surf":
        return os.path.join(base, "surf", hemi+".{name}")
    elif type == "curv":
        return os.path.join(base, "surf", hemi+".curv{name}")

def autorecon(subject, type="all"):
    types = { 
        'all':'autorecon-all',
        '1':"autorecon1",
        '2':"autorecon2",
        '3':"autorecon3",
        'cp':"autorecon2-cp",
        'wm':"autorecon2-wm",
        'pia':"autorecon2-pial"}

    times = {
        'all':"12 hours", 
        '2':"6 hours", 
        'cp':"8 hours", 
        'wm':"4 hours"
        }
    if str(type) in times:
        resp = raw_input("recon-all will take approximately %s to run! Continue? "%times[str(type)])
        if resp.lower() not in ("yes", "y"):
            return
            
    cmd = "recon-all -s {subj} -{cmd}".format(subj=subject, cmd=types[str(type)])
    sp.check_call(shlex.split(cmd))

def flatten(subject, hemi, patch):
    resp = raw_input('Flattening takes approximately 2 hours! Continue? ')
    if resp.lower() in ('y', 'yes'):
        inpath = get_paths(subject, hemi).format(name=patch)
        outpath = get_paths(subject, hemi).format(name=patch+".flat")
        cmd = "mris_flatten -O fiducial {inpath} {outpath}".format(inpath=inpath, outpath=outpath)
        sp.check_call(shlex.split(cmd))
    else:
        print("Not going to flatten...")

def import_subj(subject, sname=None):
    if sname is None:
        sname = subject
    db.surfs.makeSubj(sname)
    make_fiducial(subject)

    import nibabel
    surfs = os.path.join(db.filestore, sname, "surfaces", "{name}_{hemi}.npz")
    anats = os.path.join(db.filestore, sname, "anatomicals", "{name}.{type}")
    fspath = os.path.join(os.environ['SUBJECTS_DIR'], subject, 'mri')
    curvs = os.path.join(os.environ['SUBJECTS_DIR'], subject, 'surf', '{hemi}.{name}')

    #import anatomicals
    for fsname, name in dict(T1="raw", aseg="aseg").items():
        path = os.path.join(fspath, "{fsname}.mgz").format(fsname=fsname)
        out = anats.format(subj=sname, name=name, type='nii.gz')
        cmd = "mri_convert {path} {out}".format(path=path, out=out)
        sp.call(shlex.split(cmd))

    #Freesurfer uses FOV/2 for center, let's set the surfaces to use the magnet isocenter
    trans = nibabel.load(out).get_affine()[:3, -1]
    surfmove = trans - np.sign(trans) * [128, 128, 128]

    #import surfaces
    for fsname, name in [('smoothwm',"wm"), ('pial',"pia"), ('inflated',"inflated"), ('inflated',"flat")]:
        for hemi in ("lh", "rh"):
            pts, polys, _ = get_surf(subject, hemi, fsname)
            fname = surfs.format(subj=sname, name=name, hemi=hemi)
            np.savez(fname, pts=pts + surfmove, polys=polys)

    for curv, anat in dict(sulc="sulcaldepth", thickness="thickness", curv="curvature").items():
        lh, rh = [parse_curv(curvs.format(hemi=hemi, name=curv)) for hemi in ['lh', 'rh']]
        np.savez(anats.format(subj=sname, name=anat, type='npz'), left=-lh, right=-rh)

def import_flat(subject, patch, sname=None):
    if sname is None:
        sname = subject
    surfs = os.path.join(db.filestore, sname, "surfaces", "flat_{hemi}.npz")
    for hemi in ['lh', 'rh']:
        pts, polys, _ = get_surf(subject, hemi, "patch", patch+".flat")
        flat = pts[:,[1, 0, 2]]
        flat[:,1] = -flat[:,1]
        fname = surfs.format(hemi=hemi)
        print("saving to %s"%fname)
        #np.savez(fname, pts=flat, polys=polys)

def make_fiducial(subject):
    for hemi in ['lh', 'rh']:
        spts, polys, _ = get_surf(subject, hemi, "smoothwm")
        ppts, _, _ = get_surf(subject, hemi, "pial")
        fname = get_paths(subject, hemi, "surf").format(name="fiducial")
        write_surf(fname, (spts + ppts) / 2, polys)

def parse_surf(filename):
    with open(filename, 'rb') as fp:<commit_msg>update freesurfer to import surfinfo<commit_after>import os
import struct
import tempfile
import warnings
import shlex
import subprocess as sp

import numpy as np

from . import db
from . import anat

def get_paths(subject, hemi, type="patch"):
    base = os.path.join(os.environ['SUBJECTS_DIR'], subject)
    if type == "patch":
        return os.path.join(base, "surf", hemi+".{name}.patch.3d")
    elif type == "surf":
        return os.path.join(base, "surf", hemi+".{name}")
    elif type == "curv":
        return os.path.join(base, "surf", hemi+".curv{name}")

def autorecon(subject, type="all"):
    types = { 
        'all':'autorecon-all',
        '1':"autorecon1",
        '2':"autorecon2",
        '3':"autorecon3",
        'cp':"autorecon2-cp",
        'wm':"autorecon2-wm",
        'pia':"autorecon2-pial"}

    times = {
        'all':"12 hours", 
        '2':"6 hours", 
        'cp':"8 hours", 
        'wm':"4 hours"
        }
    if str(type) in times:
        resp = raw_input("recon-all will take approximately %s to run! Continue? "%times[str(type)])
        if resp.lower() not in ("yes", "y"):
            return
            
    cmd = "recon-all -s {subj} -{cmd}".format(subj=subject, cmd=types[str(type)])
    sp.check_call(shlex.split(cmd))

def flatten(subject, hemi, patch):
    resp = raw_input('Flattening takes approximately 2 hours! Continue? ')
    if resp.lower() in ('y', 'yes'):
        inpath = get_paths(subject, hemi).format(name=patch)
        outpath = get_paths(subject, hemi).format(name=patch+".flat")
        cmd = "mris_flatten -O fiducial {inpath} {outpath}".format(inpath=inpath, outpath=outpath)
        sp.check_call(shlex.split(cmd))
    else:
        print("Not going to flatten...")

def import_subj(subject, sname=None):
    if sname is None:
        sname = subject
    db.surfs.makeSubj(sname)

    import nibabel
    surfs = os.path.join(db.filestore, sname, "surfaces", "{name}_{hemi}.npz")
    anats = os.path.join(db.filestore, sname, "anatomicals", "{name}.nii.gz")
    surfinfo = os.path.join(db.filestore, sname, "surface-info", "{name}.npz")
    fspath = os.path.join(os.environ['SUBJECTS_DIR'], subject, 'mri')
    curvs = os.path.join(os.environ['SUBJECTS_DIR'], subject, 'surf', '{hemi}.{name}')

    if not os.path.exists(curvs.format(hemi="lh", name="fiducial")):
        make_fiducial(subject)

    #import anatomicals
    for fsname, name in dict(T1="raw", aseg="aseg").items():
        path = os.path.join(fspath, "{fsname}.mgz").format(fsname=fsname)
        out = anats.format(subj=sname, name=name)
        cmd = "mri_convert {path} {out}".format(path=path, out=out)
        sp.call(shlex.split(cmd))

    #Freesurfer uses FOV/2 for center, let's set the surfaces to use the magnet isocenter
    trans = nibabel.load(out).get_affine()[:3, -1]
    surfmove = trans - np.sign(trans) * [128, 128, 128]

    #import surfaces
    for fsname, name in [('smoothwm',"wm"), ('pial',"pia"), ('inflated',"inflated"), ('inflated',"flat")]:
        for hemi in ("lh", "rh"):
            pts, polys, _ = get_surf(subject, hemi, fsname)
            fname = surfs.format(subj=sname, name=name, hemi=hemi)
            np.savez(fname, pts=pts + surfmove, polys=polys)

    #import surfinfo
    for curv, info in dict(sulc="sulcaldepth", thickness="thickness", curv="curvature").items():
        lh, rh = [parse_curv(curvs.format(hemi=hemi, name=curv)) for hemi in ['lh', 'rh']]
        np.savez(surfinfo.format(subj=sname, name=info), left=-lh, right=-rh)

def import_flat(subject, patch, sname=None):
    if sname is None:
        sname = subject
    surfs = os.path.join(db.filestore, sname, "surfaces", "flat_{hemi}.npz")
    for hemi in ['lh', 'rh']:
        pts, polys, _ = get_surf(subject, hemi, "patch", patch+".flat")
        flat = pts[:,[1, 0, 2]]
        flat[:,1] = -flat[:,1]
        fname = surfs.format(hemi=hemi)
        print("saving to %s"%fname)
        #np.savez(fname, pts=flat, polys=polys)

def make_fiducial(subject):
    for hemi in ['lh', 'rh']:
        spts, polys, _ = get_surf(subject, hemi, "smoothwm")
        ppts, _, _ = get_surf(subject, hemi, "pial")
        fname = get_paths(subject, hemi, "surf").format(name="fiducial")
        write_surf(fname, (spts + ppts) / 2, polys)

def parse_surf(filename):
    with open(filename, 'rb') as fp:
{'fraction_non_alphanumeric': 0.17414341222182975, 'fraction_numerical': 0.008477569763334511, 'mean_word_length': 3.119301648884578, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4698081', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3156, 'n_words': 676}