# Path: ifcb/data/matlab.py
# def bin2mat(b, mat_path):
#     # ADC data
#     adc = np.array(b.adc)
#     # warning: reads all images into memory
#     roi_numbers = sorted(b.images)
#     images = [ b.images[r] for r in roi_numbers ]
#     savemat(mat_path, {
#         PID_VAR: str(b.lid), # remove non-bin parts of pid
#         HEADERS_VAR: b.headers,
#         ADC_VAR: adc,
#         ROI_NUMBERS_VAR: roi_numbers,
#         IMAGES_VAR: images
#     }, long_field_names=True)
# 
# class MatBin(BaseBin):
#     def __init__(self, mat_path):
#         self._mat = loadmat(mat_path, squeeze_me=True)
#         self.pid = Pid(self._mat[PID_VAR])
#         self.adc = pd.DataFrame(self._mat[ADC_VAR]);
#         self.adc.index += 1 # 1-based indexes
#         self.images = _MatBinImages(self._mat)
#         rec = self._mat[HEADERS_VAR]
#         rec_names = rec.dtype.names
#         self.headers = { n : rec[n].item() for n in rec_names }
# 
# Path: ifcb/tests/utils.py
# def withfile(method):
#     """decorator that adds a named temporary file argument"""
#     @wraps(method)
#     def wrapper(*args, **kw):
#         with test_file() as f:
#             args = args + (f,)
#             return method(*args, **kw)
#     return wrapper
# 
# Path: ifcb/tests/data/fileset_info.py
# def list_test_bins():
#     return list(_dd())
# 
# Path: ifcb/tests/data/bins.py
# def assert_bin_equals(in_bin, out_bin):
#     # test keys
#     assert set(in_bin) == set(out_bin)
#     for k in in_bin.keys():
#         assert k in in_bin, 'inconsistent dict behavior'
#         assert k in out_bin, 'key membership does not match'
#     assert 0 not in in_bin, 'zero-based index'
#     assert 0 not in out_bin, 'zero-based index'
#     # test ADC data
#     for k in in_bin.keys():
#         for a, b in zip(in_bin[k], out_bin[k]):
#             assert np.isclose(a, b, equal_nan=True)
#     # pid
#     assert in_bin.pid == out_bin.pid, 'pid mismatch'
#     assert in_bin.lid == out_bin.lid, 'lid mismatch'
#     # timestamp
#     assert in_bin.timestamp == out_bin.timestamp, 'timestamp mismatch'
#     # schema
#     assert in_bin.schema == out_bin.schema, 'schema mismatch'
#     # headers
#     assert set(in_bin.headers) == set(out_bin.headers), 'header keys mismatch'
#     for k in in_bin.headers.keys():
#         # headers should all have same names and values
#         assert in_bin.headers[k] == out_bin.headers[k], 'header values mismatch'
#     # images
#     assert set(in_bin.images) == set(out_bin.images), 'image target numbers mismatch'
#     for k in in_bin.images:
#         assert np.all(in_bin.images[k] == out_bin.images[k]), 'image data mismatch'


import unittest
from ifcb.data.matlab import bin2mat, MatBin
from ifcb.tests.utils import withfile
from .fileset_info import list_test_bins
from .bins import assert_bin_equals




class TestMatBin(unittest.TestCase):
    @withfile
    def test_roundtrip(self, path):

# Path: ifcb/data/matlab.py
# def bin2mat(b, mat_path):
#     # ADC data
#     adc = np.array(b.adc)
#     # warning: reads all images into memory
#     roi_numbers = sorted(b.images)
#     images = [ b.images[r] for r in roi_numbers ]
#     savemat(mat_path, {
#         PID_VAR: str(b.lid), # remove non-bin parts of pid
#         HEADERS_VAR: b.headers,
#         ADC_VAR: adc,
#         ROI_NUMBERS_VAR: roi_numbers,
#         IMAGES_VAR: images
#     }, long_field_names=True)
# 
# class MatBin(BaseBin):
#     def __init__(self, mat_path):
#         self._mat = loadmat(mat_path, squeeze_me=True)
#         self.pid = Pid(self._mat[PID_VAR])
#         self.adc = pd.DataFrame(self._mat[ADC_VAR]);
#         self.adc.index += 1 # 1-based indexes
#         self.images = _MatBinImages(self._mat)
#         rec = self._mat[HEADERS_VAR]
#         rec_names = rec.dtype.names
#         self.headers = { n : rec[n].item() for n in rec_names }
# 
# Path: ifcb/tests/utils.py
# def withfile(method):
#     """decorator that adds a named temporary file argument"""
#     @wraps(method)
#     def wrapper(*args, **kw):
#         with test_file() as f:
#             args = args + (f,)
#             return method(*args, **kw)
#     return wrapper
# 
# Path: ifcb/tests/data/fileset_info.py
# def list_test_bins():
#     return list(_dd())
# 
# Path: ifcb/tests/data/bins.py
# def assert_bin_equals(in_bin, out_bin):
#     # test keys
#     assert set(in_bin) == set(out_bin)
#     for k in in_bin.keys():
#         assert k in in_bin, 'inconsistent dict behavior'
#         assert k in out_bin, 'key membership does not match'
#     assert 0 not in in_bin, 'zero-based index'
#     assert 0 not in out_bin, 'zero-based index'
#     # test ADC data
#     for k in in_bin.keys():
#         for a, b in zip(in_bin[k], out_bin[k]):
#             assert np.isclose(a, b, equal_nan=True)
#     # pid
#     assert in_bin.pid == out_bin.pid, 'pid mismatch'
#     assert in_bin.lid == out_bin.lid, 'lid mismatch'
#     # timestamp
#     assert in_bin.timestamp == out_bin.timestamp, 'timestamp mismatch'
#     # schema
#     assert in_bin.schema == out_bin.schema, 'schema mismatch'
#     # headers
#     assert set(in_bin.headers) == set(out_bin.headers), 'header keys mismatch'
#     for k in in_bin.headers.keys():
#         # headers should all have same names and values
#         assert in_bin.headers[k] == out_bin.headers[k], 'header values mismatch'
#     # images
#     assert set(in_bin.images) == set(out_bin.images), 'image target numbers mismatch'
#     for k in in_bin.images:
#         assert np.all(in_bin.images[k] == out_bin.images[k]), 'image data mismatch'


# Path: ifcb/tests/data/test_mat.py
import unittest
from ifcb.data.matlab import bin2mat, MatBin
from ifcb.tests.utils import withfile
from .fileset_info import list_test_bins
from .bins import assert_bin_equals





class TestMatBin(unittest.TestCase):
    @withfile
    def test_roundtrip(self, path):

        for out_bin in list_test_bins():