Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test modules #66

Merged
merged 32 commits into from
Dec 18, 2023
Merged

Refactor test modules #66

merged 32 commits into from
Dec 18, 2023

Conversation

ganow
Copy link
Contributor

@ganow ganow commented Jun 20, 2023

Summary

  • Fixed one testing module such that it can work on the current implementation
    • 4ce8a2f: changed the behavior of a test code itself since the behavior of ModelTraining/ModelTest seemed to be changed (model.pkl -> model.pkl.gz)
  • Resolved dependencies on data files not contained in the repository.
    • Almost all commits are done by just adding necessary data in this repository
    • 2505d61: removed the dependency on pre-computed Features data by creating a mock function that generates a fake random data
  • reduce the file size -> leave files uncommitted due to lack of time
    • epi.mat
  • The structure of the test files was reorganized to the typical form used in many OSS.
    • Changed the test directory from test to tests
    • Test files in the same directory hierarchy (tests/path/to/test_<src>.py) are now responsible for their corresponding source file (bdpy/path/to/<src>.py).

[2, 2, 0, 0],
[3, 3, 3, 0]])

testdata = load_array('data/array_jl_dense_v1.mat', key='a')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the test code that assumes the existence of some mat files, which are not included in this repository. Do you know what type of dataset we need? > @ShuntaroAoki

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref. to the original code:

def test_load_array_jl(self):
data = np.array([[1, 0, 0, 0],
[2, 2, 0, 0],
[3, 3, 3, 0]])
testdata = load_array('data/array_jl_dense_v1.mat', key='a')
np.testing.assert_array_equal(data, testdata)
testdata = load_array('data/array_jl_sparse_v1.mat', key='a')
np.testing.assert_array_equal(data, testdata)

@ganow
Copy link
Contributor Author

ganow commented Jun 20, 2023

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
Copy link
Contributor Author

ganow commented Jun 20, 2023

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
Copy link

github-actions bot commented Jun 27, 2023

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 💤 5 ❌ 0 🔥 9.859s ⏱️

@ganow
Copy link
Contributor Author

ganow commented Jun 27, 2023

Question

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

  • data/array_jl_dense_v1.mat
  • data/testdata-2d.pkl.gz
  • data/testdata-2d-nan.pkl.gz
  • ./data/mri/epi.mat

@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
Copy link
Contributor Author

ganow commented Jun 27, 2023

Working log on 6/27

  • merged the current dev branch (ad3f588) to use GitHub workflow.

@ganow ganow changed the base branch from main to dev June 27, 2023 06:02
@ganow ganow changed the title [WIP] Refactor test modules Refactor test modules Dec 14, 2023
@ganow ganow marked this pull request as ready for review December 14, 2023 09:05
@ganow
Copy link
Contributor Author

ganow commented Dec 14, 2023

@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.

@ShuntaroAoki ShuntaroAoki merged commit d9eb337 into dev Dec 18, 2023
0 of 6 checks passed
@ShuntaroAoki ShuntaroAoki deleted the refactor-test-modules branch January 27, 2024 03:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants