Exemplo n.º 1
0
def gif_lobes_from_excel_sheets():
    """
    sort the gif parcellations as per excel gif sheet lobes.
    e.g. GIF FL = GIF Frontal Lobe - has a list of gif parcellations
    which we want to see in 3D slicer, using the GUI
    """
    _, _, excel_path, _ = file_paths()
    GIF_SHEET_NAMES = gif_sheet_names()

    lobes_mapping = {}

    for gif_lobe in GIF_SHEET_NAMES:
        gif_parcellations = pd.read_excel(
            excel_path,
            header=None,
            usecols="A:B",
            sheet_name=gif_lobe,
            engine="openpyxl",
        )
        gif_parcellations.dropna(axis=0, how='any', inplace=True)
        gif_parcellations.dropna(axis=1, how='all', inplace=True)
        gifs = gif_parcellations.astype({1: 'uint16'})
        gifs = gifs.iloc[:, 1]
        lobes_mapping[gif_lobe] = gifs.values

    return lobes_mapping
 def use_SemioBrainBeta_notDummyData(self):
     """
     factor function.
     Uses the beta version callled syst_review_single_table.
     Note the arguments for MEGA_ANALYSIS differ for the beta version.
     """
     # load the Beta SemioBrain Database:
     repo_dir, resources_dir, SemioBrainBeta_data_path, SemioDict_path = \
         file_paths(dummy_data=False, Beta=True)
     SemioBrainBeta_df, _, _, _, _, _, _ = MEGA_ANALYSIS(
         excel_data=SemioBrainBeta_data_path,
         n_rows=2500,
         usecols="A:DH",
         header=1,
         exclude_data=False,
         plot=True,
     )
     return SemioBrainBeta_df
import unittest

import pandas as pd

from mega_analysis.crosstab.file_paths import file_paths
from mega_analysis.crosstab.gif_sheet_names import gif_sheet_names
from mega_analysis.crosstab.mega_analysis.exclusions import exclusions
from mega_analysis.crosstab.mega_analysis.MEGA_ANALYSIS import MEGA_ANALYSIS
from mega_analysis.semiology import (  # semiology_dict_path,
    QUERY_SEMIOLOGY, Laterality, Semiology, gif_lat_file, map_df_dict)
from mega_analysis.crosstab.hierarchy_class import Hierarchy

# define paths: note dummy data has a tab called test_counts
# a hand crafted test fixture count
repo_dir, resources_dir, dummy_data_path, dummy_semiology_dict_path = \
    file_paths(dummy_data=True)

# Define the gif sheet names
gif_sheet_names = gif_sheet_names()

# Read Excel file only three times at initialisation
test_df, _, _, _, _, _, _ = MEGA_ANALYSIS(
    excel_data=dummy_data_path,
    n_rows=100,
    usecols="A:DH",
    header=1,
    exclude_data=False,
    plot=True,
)

map_df_dict = pd.read_excel(
def summary_semio_loc_df_from_scripts(normalise=True):
    """
    Lots of copy pasting from scripts/figures.py in kd_figures-v3 branch.

    returns query_results which is a nested dictionary
        full
        spontaneous
        topology
            {semiologies}
                query_inspection
                num_query_loc
                num_query_lat
    """

    # Define paths
    repo_dir, resources_dir, excel_path, semiology_dict_path = file_paths()

    Semio2Brain_Database = excel_path
    with open(semiology_dict_path) as f:
        SemioDict = yaml.load(f, Loader=yaml.FullLoader)

    region_names = all_localisations()
    semiology_list = list(recursive_items(SemioDict))

    (original_df,
     df_ground_truth, df_study_type,
     num_database_articles, num_database_patients, num_database_lat, num_database_loc) = \
        MEGA_ANALYSIS(Semio2Brain_Database,
                      exclude_data=True)

    # -----------------------------------
    redistribution_spec = {
        'FT': ['FL', 'INSULA', 'Lateral Temporal', 'TL'],
        'TO': ['Lateral Temporal', 'TL', 'OL'],
        'TP': ['Lateral Temporal', 'TL', 'PL'],
        'FTP': ['INSULA', 'Lateral Temporal', 'TL', 'FL', 'PL'],
        'TPO Junction': ['Lateral Temporal', 'TL', 'PL', 'OL'],
        'PO': ['PL', 'OL'],
        'FP': ['FL', 'PL'],
        'Perisylvian': ['INSULA', 'Lateral Temporal', 'TL', 'FL', 'PL'],
        'Sub-Callosal Cortex': ['Ant Cing (frontal, genu)', 'CING']
    }
    redistributed_df = copy.deepcopy(original_df)
    # probably not needed as used exclude_data True when calling M_A
    redistributed_df = exclude_postictals(redistributed_df)

    for from_region, destination_regions in redistribution_spec.items():
        for destination in destination_regions:
            redistributed_df[destination] = original_df[destination].fillna(
                0) + original_df[from_region].fillna(0)
    redistributed_df = redistributed_df.drop(redistribution_spec.keys(),
                                             'columns')
    # -----------------------------------

    # region_names_re = region_names
    # region_names_re['top_level'] = ['TL',
    #                                 'FL',
    #                                 'CING',
    #                                 'PL',
    #                                 'OL',
    #                                 'INSULA',
    #                                 'Hypothalamus',
    #                                 'Cerebellum', ]
    # region_names_re['top_level_all_other'] = ['Cerebellum']

    df = copy.deepcopy(redistributed_df)
    df_SS = exclude_ET(df)
    df_SS = exclude_cortical_stimulation(df_SS)
    df_TS = exclude_spontaneous_semiology(df)

    all_dfs = {
        'full': df,
        'spontaneous': df_SS,
        'topology': df_TS,
    }

    query_results = {}
    for key, df in all_dfs.items():
        if normalise:
            df, _ = normalise_top_level_localisation_cols(df, Bayesian=True)
        query_results[key] = query_semiology_wrapper_from_scripts(
            df, semiology_list, semiology_dict_path)

    return query_results