%%bash
if [ ! -d ./FATS ]; then
    git clone https://github.com/isadoranun/FATS ./FATS
fi
cd ./FATS;
git pull origin master;

%%bash
cd ./FATS;
git log --name-status HEAD^..HEAD;

"""
Explanation: FATS tests
A. Environment description
A.1. Clone last available version of FATS from github
End of explanation
"""


%%bash
cd ./FATS;
cat requirements.txt;

"""
Explanation: A.2. Requirements
So what are requirements files? They are very simple: lists of packages to install. Instead of running something like pip install MyApp and getting whatever libraries come along
Lear more
End of explanation
"""


%%bash
python --version

"""
Explanation: A.3. Python Version
End of explanation
"""


%%bash
uname -srvmoio

"""
Explanation: A.4. uname -srvmoio
The uname command reports basic information about a computer's software and hardware.
Learn more
End of explanation
"""


%%bash
pylint --version

"""
Explanation: A.5. Pylint Version
A Python source code analyzer which looks for programming errors, helps enforcing a coding standard and sniffs for some code smells
Learn More
End of explanation
"""


%%bash
pip freeze | grep caniusepython3

%%bash
caniusepython3 --projects FATS

"""
Explanation: A.6. caniusepython3 version
Determine if a set of project dependencies will work with Python 3
Learn More
End of explanation
"""


%%bash
sloccount FATS/FATS

"""
Explanation: A.7. Sloccount
A set of tools for counting physical Source Lines of Code (SLOC) in a large number of languages of a potentially large set of programs
Learn more
End of explanation
"""


%%bash
flake8 --version

"""
Explanation: A.8. flake8
Tool For Style Guide Enforcement
Learn more
End of explanation
"""


%%bash
pylint --py3k ./FATS/FATS/

"""
Explanation: B. Fats Status
B.1. Python 3.x Status
Check the errors/problems/warnings of FATS if you try to run it on Python 3
End of explanation
"""


%%bash
cd ./FATS;
coverage erase
coverage run --source=FATS -m py.test 
coverage report

"""
Explanation: B.2. Unit-Testing and Coverage
Unit-Testing
In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use Learn more
Code-Coverage
Test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. Learn More
End of explanation
"""


%%bash
flake8 FATS/FATS --count 

"""
Explanation: B.3. Code Style (with flake8)
Coding conventions for the Python code comprising the standard library in the main Python distribution. 
Learn more
End of explanation
"""


import sys
import time as tmod
import warnings

import numpy as np

warnings.simplefilter("ignore")

sys.path.insert(0, "./FATS/")
import FATS


#We open the ligth curve in two different bands
lc_B = FATS.ReadLC_MACHO('lc/lc_1.3444.614.B.txt')
lc_R = FATS.ReadLC_MACHO('lc/lc_1.3444.614.R.txt')

#We import the data
[mag, time, error] = lc_B.ReadLC()
[mag2, time2, error2] = lc_R.ReadLC()

#We preprocess the data
preproccesed_data = FATS.Preprocess_LC(mag, time, error)
[mag, time, error] = preproccesed_data.Preprocess()

preproccesed_data = FATS.Preprocess_LC(mag2, time2, error2)
[mag2, time2, error2] = preproccesed_data.Preprocess()

#We synchronize the data
if len(mag) != len(mag2):
    [aligned_mag, aligned_mag2, aligned_time, aligned_error, aligned_error2] = \
    FATS.Align_LC(time, time2, mag, mag2, error, error2)

lc = np.array([mag, time, error, mag2, aligned_mag, aligned_mag2, aligned_time, aligned_error, aligned_error2])

EXCLUDE = [
    'Freq1_harmonics_amplitude_0','Freq1_harmonics_amplitude_1',
    'Freq1_harmonics_amplitude_2','Freq1_harmonics_amplitude_3',
    'Freq2_harmonics_amplitude_0','Freq2_harmonics_amplitude_1',
    'Freq2_harmonics_amplitude_2','Freq2_harmonics_amplitude_3',
    'Freq3_harmonics_amplitude_0','Freq3_harmonics_amplitude_1',
    'Freq3_harmonics_amplitude_2','Freq3_harmonics_amplitude_3',
    'Freq1_harmonics_amplitude_0','Freq1_harmonics_rel_phase_0',
    'Freq1_harmonics_rel_phase_1','Freq1_harmonics_rel_phase_2',
    'Freq1_harmonics_rel_phase_3','Freq2_harmonics_rel_phase_0',
    'Freq2_harmonics_rel_phase_1','Freq2_harmonics_rel_phase_2',
    'Freq2_harmonics_rel_phase_3','Freq3_harmonics_rel_phase_0',
    'Freq3_harmonics_rel_phase_1','Freq3_harmonics_rel_phase_2',
    'Freq3_harmonics_rel_phase_3', "Period_fit", "Psi_eta", "Psi_CS"]


iterations = 1000

times_pls = []
fs = FATS.FeatureSpace(
    Data='all', excludeList=EXCLUDE)

for _ in range(iterations):
    start = tmod.time()
    fs.calculateFeature(lc)
    times_pls.append(tmod.time() - start)


times = []
fs = FATS.FeatureSpace(
    Data='all', excludeList=EXCLUDE + ["PeriodLS"])

for _ in range(iterations):
    start = tmod.time()
    fs.calculateFeature(lc)
    times.append(tmod.time() - start)

msg = """
Total iterations: {iterations}
With PeriodLS:
    - Total: {total_pls}
    - Minimun: {min_pls}
    - Maximun: {max_pls}
    - Mean: {mean_pls}
    - Std: {std_pls}
Without PeriodLS:
    - Total: {total}
    - Minimun: {min}
    - Maximun: {max}
    - Mean: {mean}
    - Std: {std}
""".format(
    iterations=iterations,

    total_pls=np.sum(times_pls), min_pls=np.min(times_pls),
    max_pls=np.max(times_pls), mean_pls=np.mean(times_pls),
    std_pls=np.std(times_pls),

    total=np.sum(times), min=np.min(times),
    max=np.max(times), mean=np.mean(times),
    std=np.std(times))

print(msg)

"""
Explanation: C. Performance of the FATS Lomb-Scargle Method
Check the time of calculate all the features with and without the PeriodLS feature
calculated with the Lomb-Scargle implementation delivered with FATS.
Implementation: https://github.com/isadoranun/FATS/blob/master/FATS/lomb.py
End of explanation
"""


import sys
import time as tmod
import warnings

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns
sns.set()

import pandas as pd

warnings.simplefilter("ignore")


import FATS

iterations = 1000
lc_size = 1000


random = np.random.RandomState(42)

results = {
    "StetsonK": np.empty(iterations),
    "StetsonJ": np.empty(iterations),
    "AndersonDarling": np.empty(iterations)}

for it in range(iterations):
    fs = FATS.FeatureSpace(featureList=list(results.keys()))

    # a simple time array from 0 to 99 with steps of 0.01
    time = np.arange(0, 100, 100./lc_size).shape

    # create 1000 magnitudes with mu 0 and std 1
    mags = random.normal(size=lc_size)

    # create 1000 magnitudes with difference <= 0.1% than mags
    mags2 = mags * random.uniform(0, 0.01, mags.size)

    # create two errors for the magnitudes equivalent to the 0.001%
    # of the magnitudes
    errors = random.normal(scale=0.00001, size=lc_size)
    errors2 = random.normal(scale=0.00001, size=lc_size)

    lc = np.array([
            mags,  # magnitude
            time,  # time
            errors, # error
            mags,  # magnitude2
            mags,  # aligned_magnitude
            mags,  # aligned_magnitude2
            time,  # aligned_time
            errors, # aligned_error
            errors  # aligned_error2
    ])

    fs.calculateFeature(lc)
    for k, v in fs.result("dict").items():
        results[k][it] = v


df = pd.DataFrame(results).describe()
print df

import datetime
datetime.datetime.now().isoformat()

%%bash
git commit -am "test ruuned";
git pull origin master;
git push origin master;
git push github master;

"""
Explanation: D. Features expected values
Check the values of the features: StetsonK, StetsonJ and AndersonDarling
This code execute 1000 iterations and show the output as statistics.
End of explanation
"""


