# Path: freesas/sasio.py
# def parse_ascii_data(
#     input_file_text: List[str], number_of_columns: int
# ) -> ndarray:
#     """
#     Parse data from an ascii file into an N column numpy array
# 
#     :param input_file_text: List containing one line of input data per element
#     :param number_of_columns: Expected number of lines in the data file
#     :return: numpy array with 3 column (q,I,err)
#     """
#     data = []
#     for line in input_file_text:
#         split = line.split()
#         if len(split) == number_of_columns:
#             try:
#                 data.append([float(x) for x in split])
#             except ValueError as err:
#                 if "could not convert string to float" in err.args[0]:
#                     pass
#                 else:
#                     raise
#     if data == []:
#         raise ValueError
#     return array(data)
# 
# def load_scattering_data(filename: PathType) -> ndarray:
#     """
#     Load scattering data q, I, err into a numpy array.
# 
#     :param filename: ASCII file, 3 column (q,I,err)
#     :return: numpy array with 3 column (q,I,err)
#     """
#     try:
#         data = loadtxt(filename)
#     except OSError:
#         raise OSError("File could not be read.")
#     except ValueError:
#         try:
#             with open(filename) as data_file:
#                 text = data_file.readlines()
#         except OSError:
#             raise OSError("File could not be read.")
#         else:
#             try:
#                 data = parse_ascii_data(text, number_of_columns=3)
#             except ValueError:
#                 raise ValueError(
#                     "File does not seem to be " "in the format q, I, err. "
#                 )
#     return data
# 
# def convert_inverse_angstrom_to_nanometer(
#     data_in_inverse_angstrom: ndarray,
# ) -> ndarray:
#     """
#     Convert data with q in 1/Å to 1/nm.
# 
#     :param data_in_inverse_angstrom: numpy array in format
#                                      (q_in_inverse_Angstrom,I,err)
#     :return: numpy array with 3 column (q_in_inverse_nm,I,err)
#     """
#     q_in_angstrom, intensity, err = data_in_inverse_angstrom.T
#     return array([q_in_angstrom * 10.0, intensity, err]).T


import unittest
import logging
from sys import version_info
from numpy import array, allclose
from ..sasio import parse_ascii_data, load_scattering_data, \
                    convert_inverse_angstrom_to_nanometer
    from unittest.mock import mock_open, patch
    from .mock_open_38 import mock_open
    from unittest.mock import patch
             patch('numpy.DataSource.open', mocked_open), \
             self.assertRaises(ValueError,
                               msg="File with no float float float "
                                   "data cannot be loaded"):
            load_scattering_data("test")

    def test_convert_inverse_angstrom_to_nanometer(self):
        """
        Test conversion of data with q in 1/Å to 1/nm
        """
        input_data = array([[1.0, 1.0, 1.0],
                            [2.0, 2.0, 1.0],
                            [3.0, 3.0, 3.0]])
        expected_result = array([[10, 1.0, 1.0],
                                 [20, 2.0, 1.0],
                                 [30, 3.0, 3.0]])
        result = convert_inverse_angstrom_to_nanometer(input_data)
        self.assertTrue(allclose(result, expected_result, 1e-7),
                        msg="Converted to 1/nm from 1/Å")

    def test_unit_conversion_creates_new_array(self):
        """
        Test conversion of data does not change original data
        """
        input_data = array([[1.0, 1.0, 1.0],
                            [2.0, 2.0, 1.0],
                            [3.0, 3.0, 3.0]])
        expected_data = array([[1.0, 1.0, 1.0],
                               [2.0, 2.0, 1.0],
                               [3.0, 3.0, 3.0]])

# Path: freesas/sasio.py
# def parse_ascii_data(
#     input_file_text: List[str], number_of_columns: int
# ) -> ndarray:
#     """
#     Parse data from an ascii file into an N column numpy array
# 
#     :param input_file_text: List containing one line of input data per element
#     :param number_of_columns: Expected number of lines in the data file
#     :return: numpy array with 3 column (q,I,err)
#     """
#     data = []
#     for line in input_file_text:
#         split = line.split()
#         if len(split) == number_of_columns:
#             try:
#                 data.append([float(x) for x in split])
#             except ValueError as err:
#                 if "could not convert string to float" in err.args[0]:
#                     pass
#                 else:
#                     raise
#     if data == []:
#         raise ValueError
#     return array(data)
# 
# def load_scattering_data(filename: PathType) -> ndarray:
#     """
#     Load scattering data q, I, err into a numpy array.
# 
#     :param filename: ASCII file, 3 column (q,I,err)
#     :return: numpy array with 3 column (q,I,err)
#     """
#     try:
#         data = loadtxt(filename)
#     except OSError:
#         raise OSError("File could not be read.")
#     except ValueError:
#         try:
#             with open(filename) as data_file:
#                 text = data_file.readlines()
#         except OSError:
#             raise OSError("File could not be read.")
#         else:
#             try:
#                 data = parse_ascii_data(text, number_of_columns=3)
#             except ValueError:
#                 raise ValueError(
#                     "File does not seem to be " "in the format q, I, err. "
#                 )
#     return data
# 
# def convert_inverse_angstrom_to_nanometer(
#     data_in_inverse_angstrom: ndarray,
# ) -> ndarray:
#     """
#     Convert data with q in 1/Å to 1/nm.
# 
#     :param data_in_inverse_angstrom: numpy array in format
#                                      (q_in_inverse_Angstrom,I,err)
#     :return: numpy array with 3 column (q_in_inverse_nm,I,err)
#     """
#     q_in_angstrom, intensity, err = data_in_inverse_angstrom.T
#     return array([q_in_angstrom * 10.0, intensity, err]).T


# Path: freesas/test/test_sasio.py
import unittest
import logging
from sys import version_info
from numpy import array, allclose
from ..sasio import parse_ascii_data, load_scattering_data, \
                    convert_inverse_angstrom_to_nanometer
    from unittest.mock import mock_open, patch
    from .mock_open_38 import mock_open
    from unittest.mock import patch

             patch('numpy.DataSource.open', mocked_open), \
             self.assertRaises(ValueError,
                               msg="File with no float float float "
                                   "data cannot be loaded"):
            load_scattering_data("test")

    def test_convert_inverse_angstrom_to_nanometer(self):
        """
        Test conversion of data with q in 1/Å to 1/nm
        """
        input_data = array([[1.0, 1.0, 1.0],
                            [2.0, 2.0, 1.0],
                            [3.0, 3.0, 3.0]])
        expected_result = array([[10, 1.0, 1.0],
                                 [20, 2.0, 1.0],
                                 [30, 3.0, 3.0]])
        result = convert_inverse_angstrom_to_nanometer(input_data)
        self.assertTrue(allclose(result, expected_result, 1e-7),
                        msg="Converted to 1/nm from 1/Å")

    def test_unit_conversion_creates_new_array(self):
        """
        Test conversion of data does not change original data
        """
        input_data = array([[1.0, 1.0, 1.0],
                            [2.0, 2.0, 1.0],
                            [3.0, 3.0, 3.0]])
        expected_data = array([[1.0, 1.0, 1.0],
                               [2.0, 2.0, 1.0],
                               [3.0, 3.0, 3.0]])

        _ = convert_inverse_angstrom_to_nanometer(input_data)