def test_aggregate(mock_save_results, mock_get_results, mock_fetch_data): # run partial jobs inputs_1 = t.inputs_regression(limit_from=0, limit_to=8, include_nominal=True) mock_fetch_data.return_value = inputs_1 intermediate() output_1 = mock_save_results.call_args[0][0] inputs_2 = t.inputs_regression(limit_from=8, limit_to=20, include_nominal=True) mock_fetch_data.return_value = inputs_2 intermediate() output_2 = mock_save_results.call_args[0][0] mock_get_results.side_effect = [ mock.MagicMock(data=output_1, error=''), mock.MagicMock(data=output_2, error=''), ] # run computations aggregate(['1', '2']) output_agg = json.loads(mock_save_results.call_args[0][0]) beta_agg = {k: v['coef'] for k, v in output_agg.items()} # calculate coefficients from single-node regression mock_fetch_data.return_value = t.inputs_regression(limit_to=20, include_nominal=True) main() output_single = json.loads(mock_save_results.call_args[0][0]) beta_single = {k: v['coef'] for k, v in output_single.items()} assert t.round_dict(beta_agg) == t.round_dict(beta_single)
def test_main_signular_matrix(mock_exit, mock_save_error, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(limit_to=1, include_nominal=False) main() mock_save_error.assert_called_with( 'Too many factors (4) for too little data (1). Use less covariables or different design.' )
def test_main(mock_save_results, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(include_nominal=False) main() result = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(result) == { 'Residual': { 'F': 'NaN', 'PR(>F)': 'NaN', 'df': 1.0, 'mean_sq': 0.019, 'sum_sq': 0.019 }, 'minimentalstate': { 'F': 12.831, 'PR(>F)': 0.173, 'df': 1.0, 'mean_sq': 0.248, 'sum_sq': 0.248 }, 'subjectage': { 'F': 0.958, 'PR(>F)': 0.507, 'df': 1.0, 'mean_sq': 0.019, 'sum_sq': 0.019 }, 'subjectage:minimentalstate': { 'F': 0.096, 'PR(>F)': 0.808, 'df': 1.0, 'mean_sq': 0.002, 'sum_sq': 0.002 } }
def test_compute_heatmap_continuous(mock_save_results, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(include_nominal=True) compute('correlation_heatmap') results = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(results) == { 'data': [{ 'type': 'heatmap', 'x': ['lefthippocampus', 'subjectage', 'minimentalstate'], 'xaxis': 'x1', 'y': ['minimentalstate', 'subjectage', 'lefthippocampus'], 'yaxis': 'y1', 'z': [[0.959, -0.343, 1.0], [-0.254, 1.0, -0.343], [1.0, -0.254, 0.959]], 'zmax': 1, 'zmin': -1 }], 'layout': { 'title': 'Correlation heatmap' } }
def test_main_empty(mock_save_results, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(include_nominal=True, limit_to=0) main() output = json.loads(mock_save_results.call_args[0][0]) assert output == {}
def test_compute_histograms(): inputs = t.inputs_regression(include_integer=True, include_nominal=True) dep_var = inputs['data']['independent'][-2] assert dep_var['name'] == 'minimentalstate' histograms_results = compute_histograms(dep_var, inputs['data']['independent'], 3) assert histograms_results == [{ 'chart': { 'type': 'column' }, 'label': 'Histogram', 'series': [{ 'data': [0, 2, 3], 'name': 'all' }], 'title': { 'text': 'MMSE Total scores histogram' }, 'xAxis': { 'categories': ['0 - 10', '10 - 20', '20 - 30'] }, 'yAxis': { 'allowDecimals': False, 'min': 0, 'title': { 'text': 'Number of participants' } } }, { 'chart': { 'type': 'column' }, 'label': 'Histogram - Age Group', 'series': [{ 'data': [0, 1, 2], 'name': '-50y' }, { 'data': [0, 1, 1], 'name': '50-59y' }], 'title': { 'text': 'MMSE Total scores histogram by Age Group' }, 'xAxis': { 'categories': ['0 - 10', '10 - 20', '20 - 30'] }, 'yAxis': { 'allowDecimals': False, 'min': 0, 'title': { 'text': 'Number of participants' } } }]
def test_aggregate_single(mock_save_results, mock_get_results, mock_fetch_data): """Aggregation on single node should give same results as ordinary linear regression.""" # run partial jobs inputs = t.inputs_regression(limit_from=0, limit_to=20) mock_fetch_data.return_value = inputs intermediate() output = mock_save_results.call_args[0][0] mock_get_results.side_effect = [ mock.MagicMock(data=output, error=''), ] # run computations aggregate(['1']) output_agg = json.loads(mock_save_results.call_args[0][0]) # calculate coefficients from single-node regression mock_fetch_data.return_value = t.inputs_regression(limit_to=20) main() output_single = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(output_agg) == t.round_dict(output_single)
def test_intermediate_stats_real(mock_save_results, mock_fetch_data): # input data with some null values data = t.inputs_regression(include_nominal=True, add_null=True) mock_fetch_data.return_value = data intermediate_stats() results = json.loads(mock_save_results.call_args[0][0]) assert len(results['data']) == 12 res = sorted(results['data'], key=lambda d: (d['index'], tuple(d['group']))) assert t.round_dict(res[:2]) == [{ 'index': 'agegroup', 'label': 'Age Group', 'type': 'polynominal', 'group': ['-50y'], 'group_variables': ['Age Group'], 'count': 3, 'unique': 1, 'top': '-50y', 'frequency': { '-50y': 3, '50-59y': 0 }, 'null_count': 0 }, { 'index': 'agegroup', 'label': 'Age Group', 'type': 'polynominal', 'group': ['50-59y'], 'group_variables': ['Age Group'], 'count': 2, 'unique': 1, 'top': '50-59y', 'frequency': { '50-59y': 2, '-50y': 0 }, 'null_count': 0 }]
def test_intermediate_stats(mock_save_results, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(include_nominal=True) intermediate_stats() results = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(results) == { 'X^T * X': [[44.891, 1055.621, 317.732], [1055.621, 25462.015, 7279.928], [317.732, 7279.928, 2354.0]], 'columns': ['lefthippocampus', 'subjectage', 'minimentalstate'], 'count': 5, 'crosstab': [{ 'agegroup': '-50y', 'count': 3 }, { 'agegroup': '50-59y', 'count': 2 }], 'means': [2.987, 70.86, 20.8], 'nominal_columns': ['agegroup'] }
def test_main_empty_input(mock_exit, mock_save_error, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(limit_to=0) main() mock_exit.assert_called_once_with(1) mock_save_error.assert_called_once()
def test_main(mock_parameters, mock_save_results, mock_get_results, mock_fetch_data): # create mock objects from database mock_fetch_data.return_value = t.inputs_regression(include_nominal=False, limit_to=5) mock_get_results.return_value = None main() results = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(results) == { 'profile': 'tabular-data-resource', 'name': 'hinmine-features', 'data': [{ 'f_1': 0.0, 'f_2': 0.51, 'f_3': 0.496, 'f_4': 0.49, 'f_5': 0.504, 'id': 0.0 }, { 'f_1': 0.509, 'f_2': 0.0, 'f_3': 0.496, 'f_4': 0.492, 'f_5': 0.503, 'id': 1.0 }, { 'f_1': 0.506, 'f_2': 0.506, 'f_3': 0.0, 'f_4': 0.484, 'f_5': 0.504, 'id': 2.0 }, { 'f_1': 0.505, 'f_2': 0.508, 'f_3': 0.49, 'f_4': 0.0, 'f_5': 0.497, 'id': 3.0 }, { 'f_1': 0.508, 'f_2': 0.508, 'f_3': 0.498, 'f_4': 0.486, 'f_5': 0.0, 'id': 4.0 }], 'schema': { 'fields': [{ 'name': 'f_1', 'type': 'float' }, { 'name': 'f_2', 'type': 'float' }, { 'name': 'f_3', 'type': 'float' }, { 'name': 'f_4', 'type': 'float' }, { 'name': 'f_5', 'type': 'float' }], 'primaryKey': 'id' } }
def test_main(mock_save_results, mock_fetch_data): mock_fetch_data.return_value = t.inputs_regression(include_nominal=True) main() output = json.loads(mock_save_results.call_args[0][0]) assert t.round_dict(fx.output_regression()) == t.round_dict(output)