KamitaniLab / bdpy

Python package for brain decoding analysis (BrainDecoderToolbox2 data format, machine learning analysis, functional MRI)
MIT License
33 stars 22 forks source link

Refactor test modules #66

Closed ganow closed 8 months ago

ganow commented 1 year ago

Summary

ganow commented 1 year ago

Moved /test/test_ml.py to /test/ml/test_learning.py and prepared the model parameter files for testing ModelTest class. commit hash: 4ce8a2f

Current problem Since *.mat is specified in .gitignore, we cannot commit the data files. (how should we do?)

ganow commented 1 year ago

Moved /test/test_ml.py to /test/ml/test_learning.py and prepared the model parameter files for testing ModelTest class. commit hash: 4ce8a2f

Current problem Since *.mat is specified in .gitignore, we cannot commit the data files. (how should we do?)

I have used the script as follows to prepare model parameter files:

import os

import numpy as np
from sklearn.linear_model import LinearRegression
from fastl2lir import FastL2LiR

from bdpy.ml import ModelTraining

X = np.random.rand(100, 500)
Y1dim = np.random.rand(100, 50)
Y4dim = np.random.rand(100, 8, 4, 4)

def run(model_type, format_, chunked):
    key = f'{model_type}-{"chunk" if chunked else "nochunk"}-{format_}'

    if model_type == 'lir':
        model = LinearRegression()
    elif model_type == 'fastl2lir':
        model = FastL2LiR()
    else:
        raise ValueError(f'Unknown model type: {model_type}')

    Y = Y4dim if chunked else Y1dim

    train = ModelTraining(model, X, Y)
    train.id = key
    if model_type == 'fastl2lir':
        train.model_parameters = {'alpha': 100, 'n_feat': 100}
    train.dtype = np.float32
    if chunked:
        train.chunk_axis = 1
    train.save_format = 'bdmodel' if format_ == 'bd' else 'pickle'
    train.save_path = os.path.join('test/data/test_models', key)

    train.run()

run('lir', 'pkl', False)

model_formats = ['pkl', 'bd']
chunked_options = [False, True]

for model_format in model_formats:
    for chunked in chunked_options:
        run('fastl2lir', model_format, chunked)
github-actions[bot] commented 1 year ago

Coverage

Coverage Report
FileStmtsMissCoverMissing
bdpy/bdata
   bdata.py39819451%79, 104, 109, 113, 118, 122, 132–135, 193, 237–243, 258–268, 283–284, 321, 325, 330–368, 418–424, 432–433, 438–439, 456–463, 481–482, 488, 522, 554, 565, 578, 611–620, 632, 647, 683, 706–714, 721–754, 764, 776–783, 788–794, 799–827, 832–853, 859–893, 897–899, 903–905, 910–919
   featureselector.py641281%62–67, 69–74
   metadata.py67199%84
   utils.py1133767%71, 82, 85–86, 95, 127–173, 201, 246, 258, 263
bdpy/dataform
   datastore.py1078521%59–75, 90–93, 97–98, 102–113, 116–119, 122–127, 131–132, 137–158, 190–197, 222–259, 262–265
   features.py29216145%29–32, 43–46, 90–92, 101–103, 107, 111, 115, 119, 154–158, 165–194, 211–212, 226–230, 268, 282, 299–313, 317, 321, 325, 329, 333, 337, 341, 345, 349, 353, 358–385, 389–409, 413–453, 456, 461–468, 482–484, 487–490, 493–496, 499–503, 506–507, 527–540
   pd.py9544%25–27, 43–44
   sparse.py67790%29, 52–58, 74, 109, 123
   utils.py12120%3–18
bdpy/dataset
   utils.py45450%3–98
bdpy/distcomp
   distcomp.py921880%33, 35, 49, 53, 55, 66–70, 74, 76, 81–82, 89–93, 97
bdpy/dl
   caffe.py60600%4–129
bdpy/dl/torch
   base.py432444%31–41, 48, 54, 60, 63, 73–83, 90, 96, 102, 105
   models.py33222632%147–168, 296–315, 326–330, 344–349, 441–493, 514–516, 527–586, 610–613, 624–683, 707–710, 721–770, 789–792, 803–852, 871–874
   torch.py1096045%49, 60, 81, 100, 107, 110, 172–202, 205, 208–220, 223–258
bdpy/evals
   metrics.py956729%49–53, 59–61, 82–112, 118–159, 172–179
bdpy/feature
   feature.py30293%69–70
bdpy/fig
   __init__.py440%6–9
   draw_group_image_set.py90900%3–182
   fig.py88880%16–164
   makeplots.py3363360%1–729
   tile_images.py59590%1–193
bdpy/ml
   crossvalidation.py592754%47–48, 113–114, 117–118, 138, 164–196
   learning.py3089669%43–44, 48, 52, 59, 91–104, 109–125, 128, 158–170, 184–209, 293, 309, 313–315, 318–319, 329, 339–340, 345–346, 356–364, 367–368, 376, 411–418, 439, 452, 460, 469, 501–503, 542, 554, 557, 565, 573, 578, 599
   model.py14012014%29–39, 54–70, 86–144, 156–169, 184–222, 225, 230–250, 254–258, 271–285
   searchlight.py161319%32–51
bdpy/mri
   fmriprep.py4974519%25–34, 38, 44–62, 65–75, 78–89, 92–160, 163–194, 230–360, 367–380, 384, 388–390, 394, 398–400, 410–434, 437–454, 457–464, 471–472, 475–491, 494, 498, 502–815, 819–831, 842–862
   glm.py403610%46–95
   image.py241921%29–54
   load_epi.py281836%36–50, 56–63, 82–88
   load_mri.py191616%16–36
   roi.py2482346%37–100, 122–148, 165–235, 241–314, 320–387, 399–466, 473–499
   spm.py15813912%26–155, 162–166, 170, 174–179, 183–300
bdpy/opendata
   __init__.py110%1
   openneuro.py2102100%1–329
bdpy/pipeline
   config.py36294%37–38
bdpy/preproc
   interface.py521669%111–123, 148–157
   preprocessor.py1296947%35, 44, 112–114, 121–128, 138–189, 196–227
   select_top.py22195%56
bdpy/recon
   utils.py55550%4–146
bdpy/recon/torch
   __init__.py110%1
   icnn.py1611610%15–478
bdpy/stats
   corr.py43393%57, 68, 102
bdpy/util
   info.py473623%19–79
   utils.py36878%60, 116–121, 140–142
TOTAL4919332532% 

Tests Skipped Failures Errors Time
114 0 :zzz: 5 :x: 0 :fire: 9.859s :stopwatch:
ganow commented 1 year ago

Question

The following four files were not found in the current bdpy repository.

@ShuntaroAoki Could you commit them to this branch (or could you tell me how to create these files)? I can handle the modifications to make the current test codes work with the files you commit.

Log of the testing

==================================================================== short test summary info =====================================================================
FAILED tests/dataform/test_sparse.py::TestSparse::test_load_array_jl - FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = 'data/array_jl_dense_v1.mat', errno = 2, error message = 'No such file or direc...
FAILED tests/evals/test_metrics.py::TestMetrics::test_2d - FileNotFoundError: [Errno 2] No such file or directory: 'data/testdata-2d.pkl.gz'
FAILED tests/evals/test_metrics.py::TestMetrics::test_2d_nan - FileNotFoundError: [Errno 2] No such file or directory: 'data/testdata-2d-nan.pkl.gz'
ERROR tests/test_mri.py::TestMri::test_add_load_epi_pass0001 - FileNotFoundError: [Errno 2] No such file or directory: './data/mri/epi.mat'
ERROR tests/test_mri.py::TestMri::test_get_roiflag_pass0001 - FileNotFoundError: [Errno 2] No such file or directory: './data/mri/epi.mat'
ERROR tests/test_mri.py::TestMri::test_get_roiflag_pass0002 - FileNotFoundError: [Errno 2] No such file or directory: './data/mri/epi.mat'
===================================================== 3 failed, 104 passed, 70 warnings, 3 errors in 32.52s ======================================================
ganow commented 1 year ago

Working log on 6/27

ganow commented 8 months ago

@ShuntaroAoki I've finished all of the refactoring of the test modules except the epi files. I would appreciate a review when you have time. Thank you.