# Path: freesas/test/utilstests.py
# def get_datafile(name):
#     """Provides the full path of a test file,
#     downloading it from the internet if needed
# 
#     :param name: name of the file to get
#     :return: full path of the datafile
#     """
#     logger.info(f"Download file {name}")
#     fullpath = downloader.getfile(name)
#     return fullpath
# 
# Path: freesas/autorg.py
# def auto_gpa(data, Rg_min=1.0, qRg_max=1.3, qRg_min=0.5):
#     def curate_data(data):
# def auto_guinier(data, Rg_min=1.0, qRg_max=1.3, relax=1.2):
#         I = data.T[1]
#         I = I[range0]
#         I0 = I[p1] * numpy.exp(q2[p1] * Rg ** 2 / 3.0)
#         I1 = I[range1]
# 
# Path: freesas/invariants.py
# def calc_Rambo_Tainer(data,
#                       guinier, qmax=2.0):
#     """calculates the invariants Vc and Qr from the Rambo & Tainer 2013 Paper,
#     also the the mass estimate based on Qr for proteins
#     
#     :param data: data in q,I,dI format, q in nm^-1
#     :param guinier: RG_RESULT instance with result from the Guinier fit
#     :param qmax: maximum q-value for the calculation in nm^-1
#     @return: dict with Vc, Qr and mass plus errors
#     """
#     scale_prot = 1.0 / 0.1231
#     power_prot = 1.0
# 
#     imax = abs(data[:, 0] - qmax).argmin()
#     if (imax <= guinier.start_point) or (guinier.start_point < 0):  # unlikely but can happened
#         logger.error("Guinier region start too late for Rambo_Tainer invariants calculation")
#         return None
#     vc = calc_Vc(data[:imax, :], guinier.Rg, guinier.sigma_Rg, guinier.I0, guinier.sigma_I0, guinier.start_point)
# 
#     qr = vc[0] ** 2 / (guinier.Rg)
#     mass = scale_prot * qr ** power_prot
# 
#     dqr = qr * (guinier.sigma_Rg / guinier.Rg + 2 * ((vc[1]) / (vc[0])))
#     dmass = mass * dqr / qr
# 
#     return RT_RESULT(vc[0], vc[1], qr, dqr, mass, dmass)


import logging
import unittest
import numpy
from math import sqrt, pi
from scipy.stats import linregress
from .utilstests import get_datafile
from ..autorg import (
    autoRg,
    RG_RESULT,
    linear_fit,
    auto_gpa,
    auto_guinier,
)
from .._autorg import curate_data  # pylint: disable=E0401
from ..invariants import calc_Rambo_Tainer
from .._bift import distribution_sphere  # pylint: disable=E0401
    def test_atsas(self):
        logger.info("test file: %s", self.testfile)
        data = numpy.loadtxt(self.testfile)
        atsas_result = self.atsas_autorg.copy()
        logger.debug("Reference version: %s" % atsas_result.pop("Version"))
        atsas_result = RG_RESULT(**atsas_result)
        free_result = autoRg(data)
        logger.debug("Ref: %s" % (atsas_result,))
        logger.debug("Obt: %s" % (free_result,))
        self.assertAlmostEqual(
            atsas_result.Rg, free_result.Rg, 1, "RG fits within 2 digits"
        )
        self.assertAlmostEqual(
            atsas_result.I0,
            free_result.I0,
            msg="I0 fits within +/- 1 ",
            delta=1,
        )
        self.assertAlmostEqual(
            atsas_result.quality,
            free_result.quality,
            0,
            msg="quality fits within 0 digits",
        )

    def test_synthetic(self):
        """Test based on sythetic data: a sphere of radius R0=4 which Rg should be 4*sqrt(3/5)"""
        R0 = 4
        I0 = 100
        data = create_synthetic_data(R0=R0, I0=I0)

# Path: freesas/test/utilstests.py
# def get_datafile(name):
#     """Provides the full path of a test file,
#     downloading it from the internet if needed
# 
#     :param name: name of the file to get
#     :return: full path of the datafile
#     """
#     logger.info(f"Download file {name}")
#     fullpath = downloader.getfile(name)
#     return fullpath
# 
# Path: freesas/autorg.py
# def auto_gpa(data, Rg_min=1.0, qRg_max=1.3, qRg_min=0.5):
#     def curate_data(data):
# def auto_guinier(data, Rg_min=1.0, qRg_max=1.3, relax=1.2):
#         I = data.T[1]
#         I = I[range0]
#         I0 = I[p1] * numpy.exp(q2[p1] * Rg ** 2 / 3.0)
#         I1 = I[range1]
# 
# Path: freesas/invariants.py
# def calc_Rambo_Tainer(data,
#                       guinier, qmax=2.0):
#     """calculates the invariants Vc and Qr from the Rambo & Tainer 2013 Paper,
#     also the the mass estimate based on Qr for proteins
#     
#     :param data: data in q,I,dI format, q in nm^-1
#     :param guinier: RG_RESULT instance with result from the Guinier fit
#     :param qmax: maximum q-value for the calculation in nm^-1
#     @return: dict with Vc, Qr and mass plus errors
#     """
#     scale_prot = 1.0 / 0.1231
#     power_prot = 1.0
# 
#     imax = abs(data[:, 0] - qmax).argmin()
#     if (imax <= guinier.start_point) or (guinier.start_point < 0):  # unlikely but can happened
#         logger.error("Guinier region start too late for Rambo_Tainer invariants calculation")
#         return None
#     vc = calc_Vc(data[:imax, :], guinier.Rg, guinier.sigma_Rg, guinier.I0, guinier.sigma_I0, guinier.start_point)
# 
#     qr = vc[0] ** 2 / (guinier.Rg)
#     mass = scale_prot * qr ** power_prot
# 
#     dqr = qr * (guinier.sigma_Rg / guinier.Rg + 2 * ((vc[1]) / (vc[0])))
#     dmass = mass * dqr / qr
# 
#     return RT_RESULT(vc[0], vc[1], qr, dqr, mass, dmass)


# Path: freesas/test/test_autorg.py
import logging
import unittest
import numpy
from math import sqrt, pi
from scipy.stats import linregress
from .utilstests import get_datafile
from ..autorg import (
    autoRg,
    RG_RESULT,
    linear_fit,
    auto_gpa,
    auto_guinier,
)
from .._autorg import curate_data  # pylint: disable=E0401
from ..invariants import calc_Rambo_Tainer
from .._bift import distribution_sphere  # pylint: disable=E0401

    def test_atsas(self):
        logger.info("test file: %s", self.testfile)
        data = numpy.loadtxt(self.testfile)
        atsas_result = self.atsas_autorg.copy()
        logger.debug("Reference version: %s" % atsas_result.pop("Version"))
        atsas_result = RG_RESULT(**atsas_result)
        free_result = autoRg(data)
        logger.debug("Ref: %s" % (atsas_result,))
        logger.debug("Obt: %s" % (free_result,))
        self.assertAlmostEqual(
            atsas_result.Rg, free_result.Rg, 1, "RG fits within 2 digits"
        )
        self.assertAlmostEqual(
            atsas_result.I0,
            free_result.I0,
            msg="I0 fits within +/- 1 ",
            delta=1,
        )
        self.assertAlmostEqual(
            atsas_result.quality,
            free_result.quality,
            0,
            msg="quality fits within 0 digits",
        )

    def test_synthetic(self):
        """Test based on sythetic data: a sphere of radius R0=4 which Rg should be 4*sqrt(3/5)"""
        R0 = 4
        I0 = 100
        data = create_synthetic_data(R0=R0, I0=I0)

        Rg = autoRg(data)