示例#1
0
def create_parameter_grid(samples, predictors):
    """Creates a parameter grid for the test function.

    Returns
    -------
    ParameterGrid
        All pairings of parameters to be run through the SLM class.
    """
    model = [
        FixedEffect(1) + FixedEffect(np.random.rand(samples, predictors),
                                     names=["y1", "y2", "y3"])
    ]

    Y_idx = [1, 2, 3]
    contrast = [np.random.rand(samples), FixedEffect(np.random.rand(samples))]
    surf = [None, read_surface_gz(fetch_surf_fsaverage()["pial_left"])]
    mask = [None, np.random.rand(10242) > 0.1]
    correction = [None, ["rft", "fdr"]]
    two_tailed = [False, True]
    param_grid = ParameterGrid({
        "Y_idx": Y_idx,
        "model": model,
        "contrast": contrast,
        "surf": surf,
        "mask": mask,
        "correction": correction,
        "two_tailed": two_tailed,
    })
    return param_grid
示例#2
0
def generate_tests():
    pial_fs5 = datasets.fetch_surf_fsaverage()["pial_left"]
    pial_surf = read_surface_gz(pial_fs5)
    tri = np.array(get_cells(pial_surf)) + 1

    np.random.seed(0)
    data = {"tri": tri, "Y": np.random.uniform(-1, 1, (1, 10242)), "FWHM": 3}
    O = generate_smooth_out(data)
    params2files(data, O, 1)
示例#3
0
def load_training_data(n):
    brainstat_dir = os.path.dirname(brainstat.__file__)
    data_dir = os.path.join(brainstat_dir, "tutorial")

    tutorial_data = fetch_tutorial_data(n_subjects=n, data_dir=data_dir)
    age = tutorial_data["demographics"]["AGE"].to_numpy()
    iq = tutorial_data["demographics"]["IQ"].to_numpy()

    # Reshape the thickness files such that left and right hemispheres are in the same row.
    files = np.reshape(np.array(tutorial_data["image_files"]), (-1, 2))

    thickness = np.zeros((n, 10242))
    for i in range(n):
        thickness[i, :] = np.squeeze(nib.load(files[i, 0]).get_fdata())
    mask = np.all(thickness != 0, axis=0)

    pial = read_surface_gz(fetch_surf_fsaverage()["pial_left"])
    return (pial, mask, age, iq, thickness)
def generate_test_data():
    pial_fs5 = datasets.fetch_surf_fsaverage()["pial_left"]
    pial_surf = read_surface_gz(pial_fs5)
    real_tri = np.array(get_cells(pial_surf))

    np.random.seed(0)

    mygrid = [
        {
            "Y": [
                np.random.randint(1, 10, size=(1, 20)),
                np.random.randint(1, 10, size=(2, 20)),
                np.random.randint(2, 10, size=(3, 20, 4)),
            ],
            "mask": [None,
                     np.random.choice(a=[False, True], size=(20, ))],
        },
        {
            "Y": [real_tri],
            "mask": [
                None,
                np.random.choice(a=[False, True], size=(real_tri.shape[1]))
            ],
        },
    ]

    myparamgrid = ParameterGrid(mygrid)

    # Here wo go!
    # Tests 1-4 : Y is 2D or 3D arrays type int, mask is None or random bool
    # Tests 6-8 : Y is pial_fs5 trinagles, mask is None or random bool

    test_num = 0
    for params in myparamgrid:
        test_num += 1
        I = {}
        for key in params.keys():
            I[key] = params[key]
        D = generate_mesh_normalize_out(I)
        params2files(I, D, test_num)
示例#5
0
n = 20
tutorial_data = fetch_tutorial_data(n_subjects=n, data_dir=data_dir)
age = tutorial_data["demographics"]["AGE"].to_numpy()
iq = tutorial_data["demographics"]["IQ"].to_numpy()

# Reshape the thickness files such that left and right hemispheres are in the same row.
files = np.reshape(np.array(tutorial_data["image_files"]), (-1, 2))

# We'll use only the left hemisphere in this tutorial.
thickness = np.zeros((n, 10242))
for i in range(n):
    thickness[i, :] = np.squeeze(nib.load(files[i, 0]).get_fdata())
mask = np.all(thickness != 0, axis=0)

pial_left = read_surface_gz(fetch_surf_fsaverage()["pial_left"])

###################################################################
# Next, we can create a BrainStat linear model by declaring these variables as
# terms. The term class requires two things: 1) an array or scalar, and 2) a
# variable name for each column. Lastly, we can create the model by simply
# adding the terms together.

from brainstat.stats.terms import Term

term_intercept = Term(1, names="intercept")
term_age = Term(age, "age")
term_iq = Term(iq, "iq")
model = term_intercept + term_age + term_iq

###################################################################
示例#6
0
def generate_tests():
    # Test 01
    # ['tri'] will be a np array, shape (4, 3), int64
    np.random.seed(0)
    rand_dict = {}
    rand_dict["tri"] = np.random.randint(1, int(10), size=(4, 3))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 1)

    # Test 02
    # ['tri'] :np array, shape (4, 3), int64
    # ['resl'] :np array, shape (8, 6), float64
    np.random.seed(0)
    rand_dict = {}
    n_vertices = 6
    rand_dict["tri"] = np.random.randint(1, n_vertices, size=(4, 3))
    rand_dict["resl"] = np.random.random_sample((8, n_vertices))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 2)

    # Test 03
    # ['tri'] :np array, shape (4, 3), int64
    # ['resl'] :np array, shape (8, 6), float64
    # ['mask'] :np array, shape (5,), bool
    np.random.seed(0)
    rand_dict = {}
    n_vertices = 6
    rand_dict["tri"] = np.random.randint(1, n_vertices, size=(4, 3))
    rand_dict["resl"] = np.random.random_sample((8, n_vertices))
    rand_dict["mask"] = np.random.choice(
        a=[True, False], size=(rand_dict["tri"].max(),)
    )
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 3)

    # Test 04
    # ['lat'] :np array, shape (10, 10, 10), float64
    np.random.seed(0)
    rand_dict = {}
    rand_dict["lat"] = np.ones((10, 10, 10))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 4)

    # Test 05
    # ['lat'] :np array, shape (10, 10, 10), bool
    np.random.seed(0)
    rand_dict = {}
    rand_dict["lat"] = np.random.choice(a=[False, True], size=(10, 10, 10))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 5)

    # Test 06
    # ['tri] :np array, shape (1000,3)
    # ['mask'] :np array, shape (['tri'].max(),), bool
    np.random.seed(0)
    rand_dict = {}
    rand_dict["tri"] = np.random.randint(1, n_vertices, size=(1000, 3))
    rand_dict["mask"] = np.random.choice(
        a=[True, False], size=(rand_dict["tri"].max(),)
    )
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 6)

    # Test 07
    # ['lat'] :np array, shape (10, 10, 10), bool
    # ['resl'] :np array, shape (1359, 1), float64
    np.random.seed(0)
    rand_dict = {}
    rand_dict["lat"] = np.random.choice(a=[False, True], size=(10, 10, 10))
    rand_dict["resl"] = np.random.random_sample((1359, 1))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 7)

    # Test 08
    # ['tri] :np array, shape (1000,3)
    # ['mask'] :np array, shape (499,), bool
    np.random.seed(1)
    rand_dict = {}
    rand_dict["tri"] = np.random.randint(1, 499, size=(1000, 3))
    rand_dict["mask"] = np.random.choice(
        a=[True, False], size=(rand_dict["tri"].max(),)
    )
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 8)

    # Test 09
    # ['lat'] :np array, shape (10, 10, 10), bool
    # ['resl'] :np array, shape (1198, 1), float64
    np.random.seed(1)
    rand_dict = {}
    rand_dict["lat"] = np.random.choice(a=[False, True], size=(10, 10, 10))
    rand_dict["resl"] = np.random.random_sample((1198, 1))
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 9)

    # Test 10
    # ['tri'] is pial_fs5, shape (20480, 3)
    pial_fs5 = datasets.fetch_surf_fsaverage()["pial_left"]
    pial_surf = read_surface_gz(pial_fs5)
    n_vertices = get_points(pial_surf).shape[0]
    rand_dict = {}
    rand_dict["tri"] = np.array(get_cells(pial_surf)) + 1
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 10)

    # Test 11
    # ['tri'] :pial_fs5, shape (20480, 3)
    # ['mask'] :np array, shape (['tri'].max(),), bool
    np.random.seed(0)
    rand_dict = {}
    rand_dict["tri"] = np.array(get_cells(pial_surf)) + 1
    rand_dict["mask"] = np.random.choice(
        a=[True, False], size=(rand_dict["tri"].max(),)
    )
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 11)

    # Test 12
    # ['tri'] :pial_fs5, shape (20480, 3) --> shuffle
    # ['mask'] :np array, shape (['tri'].max(),), bool
    np.random.seed(5)
    rand_dict = {}
    rand_dict["tri"] = np.array(get_cells(pial_surf)) + 1
    np.random.shuffle(rand_dict["tri"])
    rand_dict["mask"] = np.random.choice(
        a=[True, False], size=(rand_dict["tri"].max(),)
    )
    In, Out = generate_random_slm(rand_dict)
    params2files(In, Out, 12)
def generate_test_data():
    ## Fetch global data and settings.
    # Global test settings
    basename = "xstatp"
    test_num = 1
    np.random.seed(0)

    # Fetch surface data.
    pial_fs5 = datasets.fetch_surf_fsaverage()["pial_left"]
    pial_surf = read_surface_gz(pial_fs5)
    n_vertices = get_points(pial_surf).shape[0]

    ## Define the test parameter grids.
    # Variable types to test
    var_types = {
        "t": [np.float64],
        "df": [int, np.uint16],
        "k": [int, np.uint8],
        "resl": [np.float64],
        "tri": [np.int64],
    }
    type_parameter_grid = ParameterGrid(var_types)

    # Optional variable test.
    var_optional = {
        "dfs": [None, np.random.randint(1, 100, (1, n_vertices))],
        "cluster_threshold": [0.1, 2],
        "mask": [None, np.random.rand(n_vertices) > 0.1],
        "k": [1, 3],
    }

    # Nonsense variables to add.
    var_nonsense = ["X", "coef", "SSE", "c", "ef", "sd"]

    ## Generate test data
    # Variable type tests
    for params in type_parameter_grid:
        slm = generate_random_slm(pial_surf)
        for key in list(params.keys()):
            attr = getattr(slm, key)
            setattr(slm, key, params[key](attr))
        slm2files(slm, basename, test_num)
        test_num += 1

    # Additional variable tests.
    additional_parameter_grid = ParameterGrid(var_optional)
    for params in additional_parameter_grid:
        slm = generate_random_slm(pial_surf)
        for key in list(params.keys()):
            setattr(slm, key, params[key])
        slm2files(slm, basename, test_num)
        test_num += 1

    # Nonsense variable tests.
    slm = generate_random_slm(pial_surf)
    slm.dfs = np.random.randint(1, 100, (1, n_vertices))
    slm.mask = np.random.rand(n_vertices) > 0.1
    for key in var_nonsense:
        if getattr(slm, key) is None:
            setattr(
                slm,
                key,
                np.random.rand(np.random.randint(1, 10),
                               np.random.randint(1, 10)),
            )
    slm2files(slm, basename, test_num)
    test_num += 1
def generate_test_data():
    np.random.seed(0)

    # these are the sizes of array, which will be looped in Parameter Grid
    mygrid_xy = [{"x": [3], "y": [1]}, {"x": [6], "y": [6]}, {"x": [5], "y": [2]}]
    myparamgrid_xy = ParameterGrid(mygrid_xy)

    # Here wo go!
    # Tests 1-12: randomly generated arrays for all input params
    test_num = 0
    for params_xy in list(myparamgrid_xy):
        x = params_xy["x"]
        y = params_xy["y"]
        mygrid = [
            {
                "X": [np.random.rand(x, y)],
                "df": [int(y - 1)],
                "coef": [
                    np.random.rand(y, x),
                ],
                "SSE": [np.random.rand(1, x)],
                "contrast": [np.random.rand(1, y), np.random.rand(1, 1)],
                "dr": [None, int(y + 1)],
            }
        ]
        # here goes the actual Parameter Grid
        myparamgrid = ParameterGrid(mygrid)
        for params in myparamgrid:
            test_num += 1
            I = {}
            for key in params.keys():
                if params[key] is not None:
                    I[key] = params[key]
            D = generate_t_test_out(I)
            params2files(I, D, test_num)

    # get some real data for the triangle coordinates
    pial_fs5 = datasets.fetch_surf_fsaverage()["pial_left"]
    surf = read_surface_gz(pial_fs5)
    tri = np.array(get_cells(surf))

    realgrid = [
        {
            "X": [np.random.randint(0, 1000, size=(20, 9))],
            "df": [np.random.randint(1, 9)],
            "coef": [np.random.rand(9, int(tri.shape[0])) - 0.7],
            "SSE": [np.random.rand(1, int(tri.shape[0]))],
            "tri": [tri],
            "resl": [np.random.rand(int(tri.shape[0]), 1)],
            "contrast": [np.random.randint(21, 48, size=(20, 1))],
        }
    ]

    # Test 13: triangle coordinates are real, from pial_fs5, rest is random
    myrealgrid = ParameterGrid(realgrid)
    for params in myrealgrid:
        test_num += 1
        I = {}
        for key in params.keys():
            if params[key] is not None:
                I[key] = params[key]
        D = generate_t_test_out(I)
        params2files(I, D, test_num)