# NaCl: *Nouveaux algorithmes de Courbes de lumiere*

> |:warning:|  **This code is still under development and cannot be considered stable** 

The `NaCl` package contains code to develop and train type Ia supernova
spectrophotometric models. NaCl can train hybrid models, i.e. models trained
simultaneously on lightcurves, spectral and/or, optionaly, spectrophotometric
data, such as the spectrophotometric sequences published by the SNfactory
collaboration.

As of today, `NaCl` contains:
  - a re-implementation of the SALT2 model and error model (Guy et al, 2007,
    2010), with various improvements to make sure that the training can be
    performed in one single minimization 
  - classes to manage a hybrid (light curves + spectra + photometric spectra)
    training sample
  - a minimization framework that is able to minize a log-likelihood function,
    along with quadratic constraints (optional) and quadratic regularization
    penalities (optional).  It is also possible to fit a fairly general error
    model simultaneously.



# Installation

It it generally a good idea to work in a clean virtual environment: 

```bash
python -m venv naclenv
source naclenv/bin/activate
```


## Compile from source [recommended]

At this stage of development, the best option is to clone the `NaCl`
repository and compile the code from the source:

```bash
git clone https://gitlab.in2p3.fr/cosmo/sn-nacl
cd sn-nacl
pip install -r requirements.txt
```

To install the code: 
```bash
python setup.py install [--user]
```
or (recommended):
```bash
pip install . 
```

if you intend to hack the code, and prefer to work with an editable install:
```bash
pip install -e . 
```

### With pip

`NaCl` may also be installed with pip:

```bash
pip install sn-nacl 
```

### Dealing with scikit-sparse

`NaCl` depends on the python bindings to the `cholmod` package,
distributed with the `scikit-sparse` package.  In most cases,
`scikit-sparse` will be fetched automatically and installed without any
problem.  However, depending on your version of python the
installation may crash for various reasons: 
  - he package needs numpy to be installed already (and doesn't fetch it
    automatically if missing).  
  - the packages needs `cython` : `pip install cython` should solve the problem.
  - finally, libsuitesparse should also be available on your
	system. On a debian or ubuntu system: ```sudo apt-get install
	libsuitesparse-dev``` should suffice. On Fedora/CentOS the
	equivalent is ```sudo yum install
	libsuitesparse-devel```. Otherwise, an alternative is to to the
	[SuiteSparse repository](https://github.com/DrTimothyAldenDavis/SuiteSparse),
	clone it and follow the installation instructions.
  - Also, for python>=3.10, the pip
installation of scikit-sparse may complain about various things and
stops.  If you encounter this kind of problem, this longer install
sequence should work:

```bash 
pip install numpy
git clone https://github.com/scikit-sparse/scikit-sparse.git
cd scikit-sparse; python setup.py install; cd ..
pip install sn-nacl
```

## Install the Metadata [mandatory]

To work, NaCl needs a number of metadata files, notably the passbands
of the survey instruments used to gather the training datasets. We
intend to interface NaCl with the sncosmo registry. Until it is done,
you need to download the metadata archive, unpack it somewhere and
point the SALTPATH environment variable to its location:

```
wget http://me.lsst.eu/regnault/nacl/snfit_data.tar.gz 
tar xvzf snfit_data.tar.gz 
cd snfit_data
export SALTPATH=`pwd`
```

## SALT2 files [mandatory]

`NaCl` uses the original SALT2 model to generate its simulated
datasets. All relevant SALT2 files have been compacted into one single
archive. To create it:

```
cd sn-nacl/data
python make_salt2_npz.py 
```

It creates a `salt2.npz` which can be used by the `NaCl` code. 


## Training datasets [optional]

NaCl was developped using three kinds of simulated training datasets. 
  - ideal datasets. These are generated on the fly
  - JLA- and K21-emulations. To use those, you need to download the
    original data and compact it into two single data frames (for the
    spectra and the photometry respectively). Since it takes some time, 
	it is done manually once for all (see instructions below). 
	
  
### JLA 

```
cd sn-nacl/data/jla
python make.py
```

### K21

```
cd sn-nacl/data/k21
make -f src/module.mk
```

In a near future, we will probably distribute the compacted training
samples using the sncosmo registry. Consider the above as a temporary
annoyance.


## Building the documentation 

First make sure you have sphinx installed: 
```bash
pip install sphinx_gallery sphinx_rtd_theme
```
then 
```bash
$ cd docs
$ make html  # for example 
```
type 
```bash
$ make 
```
for a list of targets. 


## Tutorials

```bash
cd sn-nacl/tutorials
python simple_tutorial.py
python tutorial_model_error_snake.py
# python tutorial_model_variancemodel_calib.py
```

