def as_normalized(observations: np.ndarray, coordinates: ty.Coordinates, labels=None) -> ty.Dataset: """Create a dummy dataset from transformed spectra""" dimensionality = observations.shape[1] if len( observations.shape) > 1 else 0 artificial_mz = np.arange(dimensionality) normalized = ty.Dataset(observations, coordinates, artificial_mz, labels) return normalized
import aspect._export as ex class DatasetNameTest(unittest.TestCase): def test_extracts_dataset_name(self): name = 'some-dataset' analysis_root = os.path.join(DATA_ROOT, name, 'tSNE', 'blah') found = ex.dataset_name(analysis_root) self.assertEqual(name, found) SPECTRA = [[4]] COORDINATES = ty.Coordinates([1], [2], [3]) MZ = [5] LABELS = [6] DATASET = ty.Dataset(SPECTRA, COORDINATES, MZ, LABELS) def returns(value): return MagicMock(return_value=value) def throws(exception): return MagicMock(side_effect=exception) @patch.object(ex, ex.dataset_name.__name__, new=returns('data')) @patch.object(ex, ex.load_dataset.__name__, new=returns(DATASET)) class GetMetadataTest(unittest.TestCase): def test_loads_coordinates_and_labels_of_analysed_dataset(self): metadata = ex.get_metadata('blah')
def test_throws_on_mismatching_labels(self): with self.assertRaises(ValueError): ty.Dataset(spectra=[[1, 2], [3, 4]], coordinates=self.coordinates, mz=[1.1, 2.2], labels=[9])
import unittest from unittest.mock import MagicMock, mock_open, patch from io import StringIO import numpy as np import numpy.testing as npt import spdata.types as ty import spdata.reader as rd import spdata.common as cmn import data_utils COORDINATES = ty.Coordinates(x=[1], y=[2], z=[3]) DATASET = ty.Dataset([[4, 5]], COORDINATES, [6, 7], [8]) SPECTRA = DATASET.spectra def mock_iterable_open(mock=None, read_data=''): iterable_mock = mock_open(mock, read_data) iterable_mock.return_value.__iter__ = lambda self: self iterable_mock.return_value.__next__ = lambda self: next(iter(self.readline, '')) return iterable_mock class AsNormalizedTest(unittest.TestCase): def test_throws_on_sizes_mismatch(self): with self.assertRaises(ValueError): data_utils.as_normalized(np.array([[1], [2]]), COORDINATES) with self.assertRaises(ValueError):
def test_throws_on_missing_mzs(self): with self.assertRaises(ValueError): ty.Dataset(spectra=[[1, 2], [3, 4]], coordinates=self.coordinates, mz=[1.1])