<commit_before># coding: utf-8

""" Handles the analysis for SCOPE """

from __future__ import division, print_function

__author__ = "<NAME> <<EMAIL>>"

# Standard library
import logging
import os

# Third-party
import emcee
import matplotlib.pyplot as plt
import numpy as np
import numpy.random as random
import scipy.optimize

# Module
import models, utils, specutils

logger = logging.getLogger(__name__)

__all__ = ["initialise_priors", "log_likelihood", "solve"]

def initialise_priors(model, observations):
    """ Initialise the priors (or initial conditions) for the analysis """

    walker_priors = []
    initial_normalisation_coefficients = {}

    walkers = model.configuration["solver"].get("walkers", 1)

    if model.configuration.has_key("priors"):
        for i in xrange(walkers):

            current_walker = []
            for j, dimension in enumerate(model.dimensions):

                if dimension == "jitter" or dimension.startswith("jitter."):
                    # Uniform prior between 0 and 1
                    current_walker.append(random.uniform(0, 1))
                    continue

                # Implicit priors
                if dimension.startswith("normalise_observed."):
                    aperture = dimension.split(".")[1]
                    coefficient_index = int(dimension.split(".")[-1].lstrip("a"))

                    if aperture not in initial_normalisation_coefficients:
                        index = model._mapped_apertures.index(aperture)
                        order = model.configuration["normalise_observed"][aperture]["order"]

                        spectrum = observations[index]
                        
                        # Get the full range of spectra that will be normalised
                        if "masks" in model.configuration and aperture in model.configuration["masks"]:
                            
                            ranges = np.array(model.configuration["masks"][aperture])
                            min_range, max_range = np.min(ranges), np.max(ranges)
                            range_indices = np.searchsorted(spectrum.disp, [min_range, max_range])

                            flux_indices = np.zeros(len(spectrum.disp), dtype=bool)
                            flux_indices[range_indices[0]:range_indices[1]] = True
                            flux_indices *= np.isfinite(spectrum.flux) * (spectrum.flux > 0)
                            
                            logger.info("Normalising from {1:.0f} to {2:.0f} Angstroms in {0} aperture".format(
                                aperture, np.min(spectrum.disp[flux_indices]), np.max(spectrum.disp[flux_indices])))
                        else:
                            flux_indices = np.isfinite(spectrum.flux) * (spectrum.flux > 0) 

                        # Fit the spectrum with a polynomial of order X
                        coefficients = np.polyfit(spectrum.disp[flux_indices], spectrum.flux[flux_indices], order)

                        # Save the coefficients and variances
                        initial_normalisation_coefficients[aperture] = coefficients
                    
                    coefficient = initial_normalisation_coefficients[aperture][coefficient_index]
                    
                    spectrum = observations[model._mapped_apertures.index(aperture)]
                    n = len(initial_normalisation_coefficients[aperture])

                    sigma = 100./(np.mean(spectrum.disp[flux_indices])**(n - coefficient_index - 1))
                    val = np.random.normal(coefficient, sigma)
                    current_walker.append(val)

                    continue<commit_msg>new init of coefficients<commit_after># coding: utf-8

""" Handles the analysis for SCOPE """

from __future__ import division, print_function

__author__ = "<NAME> <<EMAIL>>"

# Standard library
import logging
import os

# Third-party
import emcee
import matplotlib.pyplot as plt
import numpy as np
import numpy.random as random
import scipy.optimize

# Module
import models, utils, specutils

logger = logging.getLogger(__name__)

__all__ = ["initialise_priors", "log_likelihood", "solve"]

def initialise_priors(model, observations):
    """ Initialise the priors (or initial conditions) for the analysis """

    walker_priors = []
    initial_normalisation_coefficients = {}

    walkers = model.configuration["solver"].get("walkers", 1)

    if model.configuration.has_key("priors"):
        for i in xrange(walkers):

            current_walker = []
            interpolated_flux = {}
            for j, dimension in enumerate(model.dimensions):

                if dimension == "jitter" or dimension.startswith("jitter."):
                    # Uniform prior between 0 and 1
                    current_walker.append(random.uniform(0, 1))
                    continue

                # Implicit priors
                if dimension.startswith("normalise_observed."):

                    if len(interpolated_flux) == 0:
                        interpolated_flux = model.interpolate_flux(current_walker[:len(self.grid_points.dtype.names)])

                        if np.all(np.isfinite(interpolated_flux.values()[0])):
                            interpolated_flux = {}
                            for aperture in model.apertures:
                                interpolated_flux[aperture] = np.ones(len(model.dispersion[aperture]))

                    aperture = dimension.split(".")[1]
                    coefficient_index = int(dimension.split(".")[-1].lstrip("a"))

                    # If we're at this stage we should have grid point dimensions
                    if aperture not in initial_normalisation_coefficients:

                        index = model._mapped_apertures.index(aperture)
                        order = model.configuration["normalise_observed"][aperture]["order"]

                        spectrum = observations[index]
                        
                        # Get the full range of spectra that will be normalised
                        if "masks" in model.configuration and aperture in model.configuration["masks"]:
                            
                            ranges = np.array(model.configuration["masks"][aperture])
                            min_range, max_range = np.min(ranges), np.max(ranges)
                            range_indices = np.searchsorted(spectrum.disp, [min_range, max_range])

                            flux_indices = np.zeros(len(spectrum.disp), dtype=bool)
                            flux_indices[range_indices[0]:range_indices[1]] = True
                            flux_indices *= np.isfinite(spectrum.flux) * (spectrum.flux > 0)
                            
                            logger.info("Normalising from {1:.0f} to {2:.0f} Angstroms in {0} aperture".format(
                                aperture, np.min(spectrum.disp[flux_indices]), np.max(spectrum.disp[flux_indices])))
                        else:
                            flux_indices = np.isfinite(spectrum.flux) * (spectrum.flux > 0) 

                        # Fit the spectrum with a polynomial of order X
                        resampled_interpolated_flux = np.interp(spectrum.disp[flux_indices], model.dispersion[aperture],
                            interpolated_flux[aperture])
                        coefficients = np.polyfit(spectrum.disp[flux_indices], spectrum.flux[flux_indices]/resampled_interpolated_flux, order)

                        # Save the coefficients and variances
                        initial_normalisation_coefficients[aperture] = coefficients
                    
                    coefficient = initial_normalisation_coefficients[aperture][coefficient_index]
                    current_walker.append(coefficient)

                    continue
{'fraction_non_alphanumeric': 0.09009698825931597, 'fraction_numerical': 0.005104645227156712, 'mean_word_length': 1.7949358059914409, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '870560', 'n_tokens_mistral': 2122, 'n_tokens_neox': 1919, 'n_words': 423}