<gh_stars>1-10
"""
Test cases for model fitting code in classify_utilities.py
"""
import pytest
import numpy as np
import pandas as pd

import mpmp.test_config as tcfg
from mpmp.data_models.tcga_data_model import TCGADataModel
from mpmp.prediction.cross_validation import run_cv_stratified
import mpmp.utilities.data_utilities as du

@pytest.fixture
def data_model(data_type):
    """Load data model and sample info data"""
    # passing arguments to fixtures (like data_type here), then using them
    # in tests isn't widely documented in pytest, but seems to work
    # see, e.g. https://stackoverflow.com/a/60148972
    tcga_data = TCGADataModel(training_data=data_type,
                              debug=True, test=True)
    sample_info_df = du.load_sample_info(train_data_type=data_type)
    return tcga_data, sample_info_df

@pytest.mark.parametrize('data_type', [tcfg.test_data_types[0]])
def test_simple(data_model):
    """Just test that the data model loads correctly with test option"""
    assert data_model is not None

@pytest.mark.parametrize('data_type', tcfg.test_data_types)
@pytest.mark.parametrize('gene_info', tcfg.stratified_gene_info)
def test_stratified_classification(data_model, data_type, gene_info):
    """Regression test for prediction using stratified cross-validation"""
    tcga_data, sample_info_df = data_model
    gene, classification = gene_info
    tcga_data.process_data_for_gene(gene,
                                    classification,
                                    gene_dir=None)
    results = run_cv_stratified(tcga_data,
                                'gene',
                                gene,
                                data_type,
                                sample_info_df,
                                num_folds=4,
                                standardize_columns=True,
                                shuffle_labels=False)
    metrics_df = pd.concat(results['gene_metrics'])
    results_file = tcfg.test_stratified_results.format(data_type, gene)
    old_results = np.loadtxt(results_file)
    # make sure our results haven't changed; i.e. regression testing for model
    # if a change to model output is intentional, the saved results can be
    # regenerated by running mpmp/scripts/generate_test_data.py
    assert np.allclose(metrics_df['auroc'].values, old_results)



"""
Test cases for model fitting code in classify_utilities.py
"""
import pytest
import numpy as np
import pandas as pd

import mpmp.test_config as tcfg
from mpmp.data_models.tcga_data_model import TCGADataModel
from mpmp.prediction.cross_validation import run_cv_stratified
import mpmp.utilities.data_utilities as du

@pytest.fixture
def data_model(data_type):
    """Load data model and sample info data"""
    # passing arguments to fixtures (like data_type here), then using them
    # in tests isn't widely documented in pytest, but seems to work
    # see, e.g. https://stackoverflow.com/a/60148972
    tcga_data = TCGADataModel(training_data=data_type,
                              debug=True, test=True)
    sample_info_df = du.load_sample_info(train_data_type=data_type)
    return tcga_data, sample_info_df

@pytest.mark.parametrize('data_type', [tcfg.test_data_types[0]])
def test_simple(data_model):
    """Just test that the data model loads correctly with test option"""
    assert data_model is not None

@pytest.mark.parametrize('data_type', tcfg.test_data_types)
@pytest.mark.parametrize('gene_info', tcfg.stratified_gene_info)
def test_stratified_classification(data_model, data_type, gene_info):
    """Regression test for prediction using stratified cross-validation"""
    tcga_data, sample_info_df = data_model
    gene, classification = gene_info
    tcga_data.process_data_for_gene(gene,
                                    classification,
                                    gene_dir=None)
    results = run_cv_stratified(tcga_data,
                                'gene',
                                gene,
                                data_type,
                                sample_info_df,
                                num_folds=4,
                                standardize_columns=True,
                                shuffle_labels=False)
    metrics_df = pd.concat(results['gene_metrics'])
    results_file = tcfg.test_stratified_results.format(data_type, gene)
    old_results = np.loadtxt(results_file)
    # make sure our results haven't changed; i.e. regression testing for model
    # if a change to model output is intentional, the saved results can be
    # regenerated by running mpmp/scripts/generate_test_data.py
    assert np.allclose(metrics_df['auroc'].values, old_results)
Test cases for model fitting code in classify_utilities.py Load data model and sample info data # passing arguments to fixtures (like data_type here), then using them # in tests isn't widely documented in pytest, but seems to work # see, e.g. https://stackoverflow.com/a/60148972 Just test that the data model loads correctly with test option Regression test for prediction using stratified cross-validation # make sure our results haven't changed; i.e. regression testing for model # if a change to model output is intentional, the saved results can be # regenerated by running mpmp/scripts/generate_test_data.py