Exemplo n.º 1
0
def gen_mapped_prior_stim_sites(subject_id, localization, paths,
                                setup_status) -> FilePaths:
    """ Map prior stim site locations into this subject's coordinate space

    Parameters
    ----------
    subject_id: str
        Subject ID
    localization: str
        Localization number to use
    paths: :class:`cml_pipelines.paths.FilePaths`
        Container for various file paths needed by the task
    setup_status: bool
        Status of the setup task. Indicates that this task is dependent on
        setup completing successfully

    Returns
    -------
    exp_paths: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by this task

    """
    subject_localization = _combine_subject_localization(
        subject_id, localization)
    output_file = build_prior_stim_location_mapping(subject_localization,
                                                    paths.base, paths.image)

    exp_paths = FilePaths(root="/", prior_stim=output_file)

    return exp_paths
Exemplo n.º 2
0
    def test_initialize(self, root, basedir):
        paths = FilePaths(root, base=basedir)

        basedir = basedir.lstrip("/").rstrip("/")

        assert paths.root == os.path.expanduser(root)
        assert paths.base == os.path.join(os.path.expanduser(root), basedir)
        return
Exemplo n.º 3
0
def avg_hcp_to_subject(subject_id: str, localization: int, paths: FilePaths,
                       setup_status: bool) -> FilePaths:
    """ Convert HCP atlas annotation file into subject-specific space

    Parameters
    ----------
    subject_id: str
        ID of subject
    localization: str
        Localization number to use
    paths: :class:`cml_pipelines.paths.FilePaths`
        Container for various file paths produced by the task
    setup_status: bool
        True if the setup task completed successfully. The presence of this
        boolean in the function signature is used to notify the pipeline
        framework that this task depends on the result of the setup task

    Returns
    -------
    exp_files: :class:`cml_pipelines.paths.FilePaths`
        File path container object containing files produced by this task

    """

    subject_localization = _combine_subject_localization(
        subject_id, localization)

    subprocess.run(" ".join([
        "mri_surf2surf", "--srcsubject fsaverage_temp",
        "--sval-annot HCP-MMP1.annot",
        "--trgsubject {}".format(subject_localization),
        "--trgsurfval HCP-MMP1.annot", "--hemi lh"
    ]),
                   shell=True,
                   check=True)

    subprocess.run(" ".join([
        "mri_surf2surf", "--srcsubject fsaverage_temp",
        "--sval-annot HCP-MMP1.annot",
        "--trgsubject {}".format(subject_localization),
        "--trgsurfval HCP-MMP1.annot", "--hemi rh"
    ]),
                   shell=True,
                   check=True)

    shutil.copy(os.path.join(paths.base, "label/lh.HCP-MMP1.annot"),
                paths.cortex)
    shutil.copy(os.path.join(paths.base, "label/rh.HCP-MMP1.annot"),
                paths.cortex)

    exp_files = FilePaths(root="/",
                          rh_hcp=os.path.join(paths.cortex,
                                              "rh.HCP-MMP1.annot"),
                          lh_hcp=os.path.join(paths.cortex,
                                              "lh.HCP-MMP1.annot"))

    return exp_files
Exemplo n.º 4
0
 def setup_class(cls):
     cls.paths = FilePaths(datafile("R1291M_1/"),
                           base="",
                           cortex="surf/roi/",
                           image="imaging/autoloc/",
                           tal="tal/",
                           output="blender_scene/",
                           avg_prior_stim="")
     cls.subject_id = "R1291M"
     cls.localization = 1
Exemplo n.º 5
0
 def test_gen_blender_scene(self):
     prior_stim_output = FilePaths(root="/",
                                   prior_stim=os.path.join(
                                       self.paths.base,
                                       "prior_stim/R1291M_1_allcords.csv"))
     # Most of the FilePaths objects that are passed are only used to
     # denote dependencies and are not actually used, which is why it is
     # okay to pass self.paths multiple times for testing
     returned_paths = gen_blender_scene(self.subject_id, self.localization,
                                        self.paths, True, prior_stim_output,
                                        self.paths, self.paths, self.paths)
     assert os.path.exists(returned_paths.blender_file)
Exemplo n.º 6
0
def gen_blender_scene(subject_id: str, localization: int, paths: FilePaths,
                      build_site_status, prior_stim_paths: FilePaths,
                      split_hcp_files: FilePaths, split_dk_files: FilePaths,
                      electrode_coord_files: FilePaths) -> FilePaths:
    """ Creates the Blender-based 3D brain standalone brain visualization

    Parameters
    ----------
    subject_id: str
        Subject ID
    localization: str
        Localization number to use
    paths: :class:`cml_pipelines.paths.FilePaths`
        File path container for files needed by this task
    build_site_status: bool
        Status of the setup_standalone_blender_scene task, indicating a
        dependency on this task
    prior_stim_paths: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the
        gen_mapped_prior_stim_sites task, indicating a dependency on this task
    split_hcp_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the split_hcp_surface task,
        indicating a dependency on this task
    split_dk_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the split_dk_surface,
        indicating a dependency on this task
    electrode_coord_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the save_coords_for_blender
        task, indicating a dependency on this task

    Returns
    -------
    exp_output: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by this task

    """
    prior_stim_sites = prior_stim_paths.prior_stim
    subject_localiztion = _combine_subject_localization(
        subject_id, localization)
    subprocess.run([
        "/usr/global/blender-2.78c-linux-glibc219-x86_64/blender", "-b",
        datafile("iEEG_surface_template/empty.blend"), "-b", "--python",
        code_files("create_scene.py"), "--", subject_localiztion, paths.cortex,
        paths.tal, paths.output, prior_stim_sites
    ],
                   check=True)
    exp_output = FilePaths(root="/",
                           blender_file=os.path.join(paths.output,
                                                     'iEEG_surface.json'))

    return exp_output
Exemplo n.º 7
0
def freesurfer_to_wavefront(paths: FilePaths, setup_status: bool) -> FilePaths:
    """ Convert Freesurfer brain piece models to wavefront format

    Parameters
    ----------
    paths: :class:`cml_pipelines.paths.FilePaths
        File path container
    setup_status: bool
        If true, setup step was succesful. Indicates that this task is
        dependent on the setup completing successfully

    Returns
    -------
    exp_files: :class:`cml_pipelines.paths.FilePaths`
        File path container with files produced by the task

    """
    subprocess.run("mris_convert " + os.path.join(paths.cortex, "lh.pial ") +
                   os.path.join(paths.cortex, "lh.pial.asc"),
                   shell=True,
                   check=True)
    subprocess.run("mris_convert " + os.path.join(paths.cortex, "rh.pial ") +
                   os.path.join(paths.cortex, "rh.pial.asc"),
                   shell=True,
                   check=True)
    shutil.move(os.path.join(paths.cortex, "lh.pial.asc"),
                os.path.join(paths.cortex, "lh.pial.srf"))
    shutil.move(os.path.join(paths.cortex, "rh.pial.asc"),
                os.path.join(paths.cortex, "rh.pial.srf"))

    subprocess.run(" ".join([
        bin_files("srf2obj"),
        os.path.join(paths.cortex, "lh.pial.srf "), ">",
        os.path.join(paths.cortex, "lh.pial.obj")
    ]),
                   shell=True,
                   check=True)

    subprocess.run(" ".join([
        bin_files("srf2obj"),
        os.path.join(paths.cortex, "rh.pial.srf"), ">",
        os.path.join(paths.cortex, "rh.pial.obj")
    ]),
                   shell=True,
                   check=True)

    exp_files = FilePaths(root="/",
                          rh_obj=os.path.join(paths.cortex, "rh.pial.obj"),
                          lh_obj=os.path.join(paths.cortex, "lh.pial.obj"))

    return exp_files
Exemplo n.º 8
0
def test_gen_avg_brain():
    """ Black-box test for generating average brain """

    paths = FilePaths(datafile("average"),
                      avg_roi="surf/roi/",
                      output="blender_scene/",
                      avg_prior_stim="")

    generate_average_brain(paths=paths, blender=True, force_rerun=True)

    assert os.path.exists(paths.output + '/iEEG_surface.blend')
    assert os.path.exists(paths.output + '/iEEG_surface.bin')
    assert os.path.exists(paths.output + '/iEEG_surface.json')
    assert os.path.exists(paths.root + "/fsaverage_joel_allcords.csv")

    if os.path.exists(paths.output):
        shutil.rmtree(paths.output, ignore_errors=True)
    if os.path.exists(paths.root + "fsaverage_joel_allcords.csv"):
        os.remove(paths.root + "fsaverage_joel_allcords.csv")
Exemplo n.º 9
0
def setup_paths(subject_id: str,
                localization: str,
                rhino_root: Optional[str] = "/"):
    """
        Helper function to produce a `cml_pipelines.paths.FilePaths` object
        with production paths for building a subject-specific 3D brain
        visualization

    Parameters
    ----------
    subject_id: str
        ID of subject to generate the brain visualization
    localization: str
        Localization number as a string to use. Typically this is 0.
    rhino_root: str, default: "/"
        Mount point for RHINO
    """
    subject_localization = _combine_subject_localization(
        subject_id, localization)
    subject_num = _extract_subject_num(subject_id)

    # Common paths used throughout the pipeline
    BASE = "/data10/eeg/freesurfer/subjects/{}".format(subject_localization)
    CORTEX = "/data10/eeg/freesurfer/subjects/{}/surf/roi".format(
        subject_localization)
    CONTACT = "/data10/RAM/subjects/{}/tal/coords".format(subject_localization)
    TAL = "/data10/RAM/subjects/{}/tal".format(subject_localization)
    IMAGE = "/data10/RAM/subjects/{}/imaging/autoloc/".format(
        subject_localization)
    OUTPUT = "/reports/r1/subjects/{}/reports/iEEG_surface".format(subject_num)

    paths = FilePaths(
        rhino_root,
        base=BASE,
        cortex=CORTEX,
        contact=CONTACT,
        tal=TAL,
        image=IMAGE,
        output=OUTPUT,
        avg_prior_stim=
        "data10/eeg/freesurfer/subjects/fsaverage_joel/prior_stim/")
    return paths
Exemplo n.º 10
0
def setup_avg_paths(rhino_root: Optional[str] = "/") -> FilePaths:
    """ Create default paths for building average brain visualization

    Parameters
    ----------
    rhino_root: str
        Mount point for RHINO

    Returns
    -------
    paths: :class:`cml_pipelines.paths.FilePaths`
        File path container
    """
    paths = FilePaths(
        rhino_root,
        avg_prior_stim=
        "data10/eeg/freesurfer/subjects/fsaverage_joel/prior_stim/",
        output="reports/r1/subjects/avg/iEEG_surface",
        avg_roi="data10/eeg/freesurfer/subjects/average/surf/roi/")
    return paths
Exemplo n.º 11
0
 def setup_class(cls):
     cls.paths = FilePaths(datafile("R1291M_1/"), base="",
                           workdir="prior_stim/",
                           baselinedir="prior_stim_baseline/",
                           imagedir="imaging/autoloc/")
     cls.subject_id = "R1291M_1"
Exemplo n.º 12
0
def split_hcp_surface(paths: FilePaths, hcp_subj_files: FilePaths,
                      fs_to_wav_files: FilePaths) -> FilePaths:
    """ Creates individual brain objects based on the HCP atlas

    Parameters
    ----------
    paths: :class:`cml_pipelines.paths.FilePaths`
        File path container for files needed by this task
    hcp_subj_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the avg_hcp_to_subject task,
        indicating a dependency on this task
    fs_to_wav_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the freesurfer_to_wavefront
        task, indicating a dependency on this task

    Returns
    -------
    exp_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by this task

    """
    subprocess.run(" ".join([
        bin_files("annot2dpv"),
        os.path.join(paths.cortex, "lh.HCP-MMP1.annot"),
        os.path.join(paths.cortex, "lh.HCP-MMP1.annot.dpv")
    ]),
                   shell=True,
                   check=True)
    subprocess.run(" ".join([
        bin_files("annot2dpv"),
        os.path.join(paths.cortex, "rh.HCP-MMP1.annot"),
        os.path.join(paths.cortex, "rh.HCP-MMP1.annot.dpv")
    ]),
                   shell=True,
                   check=True)
    subprocess.run(" ".join([
        bin_files("splitsrf"),
        os.path.join(paths.cortex, "lh.pial.srf"),
        os.path.join(paths.cortex, "lh.HCP-MMP1.annot.dpv"),
        os.path.join(paths.cortex, "lh.hcp")
    ]),
                   shell=True,
                   check=True)
    subprocess.run(" ".join([
        bin_files("splitsrf"),
        os.path.join(paths.cortex, "rh.pial.srf"),
        os.path.join(paths.cortex, "rh.HCP-MMP1.annot.dpv"),
        os.path.join(paths.cortex, "rh.hcp")
    ]),
                   shell=True,
                   check=True)

    hcp_surfaces = glob.glob(os.path.join(paths.cortex, "*.hcp.*.srf"))
    for surface in hcp_surfaces:
        subprocess.run(" ".join([
            bin_files("srf2obj"), surface, ">",
            surface.replace(".srf", ".obj")
        ]),
                       shell=True,
                       check=True)

    exp_files = FilePaths(root="/",
                          lh_hcp=os.path.join(paths.cortex, "lh.hcp.0001.obj"),
                          rh_hcp=os.path.join(paths.cortex, "rh.hcp.0001.obj"))

    return exp_files
Exemplo n.º 13
0
def split_dk_surface(paths: FilePaths,
                     fs_to_wav_files: FilePaths) -> FilePaths:
    """ Creates individual brain objects based on Desikan-Killiany atlas

    Parameters
    ----------
    paths: :class:`cml_pipelines.paths.FilePaths`
        File path container for files needed by this task
    fs_to_wav_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by the freesurfer_to_wavefront,
        which also indicates a dependency on this task

    Returns
    -------
    exp_files: :class:`cml_pipelines.paths.FilePaths`
        File path container for files produced by this task

    """
    subprocess.run(" ".join([
        bin_files("annot2dpv"),
        os.path.join(paths.cortex, "rh.aparc.annot"),
        os.path.join(paths.cortex, "rh.aparc.annot.dpv")
    ]),
                   shell=True,
                   check=True)
    subprocess.run(" ".join([
        bin_files("annot2dpv"),
        os.path.join(paths.cortex, "lh.aparc.annot"),
        os.path.join(paths.cortex, "lh.aparc.annot.dpv")
    ]),
                   shell=True,
                   check=True)

    subprocess.run(" ".join([
        bin_files("splitsrf"),
        os.path.join(paths.cortex, "rh.pial.srf"),
        os.path.join(paths.cortex, "rh.aparc.annot.dpv"),
        os.path.join(paths.cortex, "rh.pial_roi")
    ]),
                   shell=True,
                   check=True)

    subprocess.run(" ".join([
        bin_files("splitsrf"),
        os.path.join(paths.cortex, "lh.pial.srf"),
        os.path.join(paths.cortex, "lh.aparc.annot.dpv"),
        os.path.join(paths.cortex, "lh.pial_roi")
    ]),
                   shell=True,
                   check=True)

    # Mapping between the default numeric number and the name of the brain
    # region
    surf_num_dict = {
        "0001": "Unmeasured.obj",
        "0002": "BanksSuperiorTemporal.obj",
        "0003": "CACingulate.obj",
        "0004": "MiddleFrontalCaudal.obj",
        "0005": "Cuneus.obj",
        "0006": "Entorhinal.obj",
        "0007": "Fusiform.obj",
        "0008": "InferiorParietal.obj",
        "0009": "InferiorTemporal.obj",
        "0010": "Isthmus.obj",
        "0011": "LateralOccipital.obj",
        "0012": "OrbitalFrontal.obj",
        "0013": "Lingual.obj",
        "0014": "MedialOrbitalFrontal.obj",
        "0015": "MiddleTemporal.obj",
        "0016": "Parahippocampal.obj",
        "0017": "ParacentralLobule.obj",
        "0018": "InfFrontalParsOpercularis.obj",
        "0019": "InfFrontalParsOrbitalis.obj",
        "0020": "InfFrontalParsTriangularis.obj",
        "0021": "Pericalcarine.obj",
        "0022": "Post-Central.obj",
        "0023": "PosteriorCingulate.obj",
        "0024": "Pre-Central.obj",
        "0025": "PreCuneus.obj",
        "0026": "RACingulate.obj",
        "0027": "MiddleFrontalRostral.obj",
        "0028": "SuperiorFrontal.obj",
        "0029": "SuperiorParietal.obj",
        "0030": "SuperiorTemporal.obj",
        "0031": "Supra-Marginal.obj",
        "0032": "FrontalPole.obj",
        "0033": "TemporalPole.obj",
        "0034": "TransverseTemporal.obj",
        "0035": "Insula.obj"
    }

    base_input_file = paths.cortex + "/{hemisphere}.pial_roi.{surface_num}.srf"
    base_output_file = paths.cortex + "/{hemisphere}.{surface_name}"
    exp_files = FilePaths(root="/")
    for hemisphere in ["lh", "rh"]:
        for surface in surf_num_dict.keys():
            subprocess.run(" ".join([
                bin_files("srf2obj"),
                base_input_file.format(hemisphere=hemisphere,
                                       surface_num=surface), ">",
                base_output_file.format(hemisphere=hemisphere,
                                        surface_name=surf_num_dict[surface])
            ]),
                           shell=True,
                           check=True)
            # Adding the output file to the set of paths
            setattr(
                exp_files, "".join([hemisphere, surface]),
                base_output_file.format(hemisphere=hemisphere,
                                        surface_name=surf_num_dict[surface]))

    return exp_files
Exemplo n.º 14
0
 def setup_class(cls):
     cls.test_paths = FilePaths("/mnt/mount_point/",
                                dir1="/base/",
                                dir2="dir2/")