# 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
                        "#REMARK some stuff at the end",
                        ]
        expected_result = array([[1.0, 1.0, 1.0],
                                 [2.0, 2.0, 1.0],
                                 [3.0, 3.0, 3.0]])
        file_data = "\n".join(file_content)
        mocked_open = mock_open(read_data=file_data)
        with patch('builtins.open', mocked_open):
            with patch('numpy.DataSource.open', mocked_open):
                data = load_scattering_data("test")
        self.assertTrue(allclose(data, expected_result, 1e-7),
                        msg="Sunny data loaded correctly")

    def test_load_data_with_unescaped_header(self):
        """
        Test that an unescaped header does not hinder loading.
        """
        file_content = ["Test data for",
                        "file parsing",
                        "1 1 1",
                        "2.0 2.0 1.0",
                        "3 3 3",
                        ]
        expected_result = array([[1.0, 1.0, 1.0],
                                 [2.0, 2.0, 1.0],
                                 [3.0, 3.0, 3.0]])
        file_data = "\n".join(file_content)
        mocked_open = mock_open(read_data=file_data)
        with patch('builtins.open', mocked_open), \
             patch('numpy.DataSource.open', mocked_open):

# 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

                        "#REMARK some stuff at the end",
                        ]
        expected_result = array([[1.0, 1.0, 1.0],
                                 [2.0, 2.0, 1.0],
                                 [3.0, 3.0, 3.0]])
        file_data = "\n".join(file_content)
        mocked_open = mock_open(read_data=file_data)
        with patch('builtins.open', mocked_open):
            with patch('numpy.DataSource.open', mocked_open):
                data = load_scattering_data("test")
        self.assertTrue(allclose(data, expected_result, 1e-7),
                        msg="Sunny data loaded correctly")

    def test_load_data_with_unescaped_header(self):
        """
        Test that an unescaped header does not hinder loading.
        """
        file_content = ["Test data for",
                        "file parsing",
                        "1 1 1",
                        "2.0 2.0 1.0",
                        "3 3 3",
                        ]
        expected_result = array([[1.0, 1.0, 1.0],
                                 [2.0, 2.0, 1.0],
                                 [3.0, 3.0, 3.0]])
        file_data = "\n".join(file_content)
        mocked_open = mock_open(read_data=file_data)
        with patch('builtins.open', mocked_open), \
             patch('numpy.DataSource.open', mocked_open):

            data = load_scattering_data("test")