<commit_before>from __future__ import division
import numpy as np
from pymc import deterministic, Normal
from pymc.MCMC import MCMC
from .array_utils import *
from .ModelComponents import Sky, PSF
from .ModelComponents.PSFSelector import PSFSelector
from .model_parser import component_list_from_file
# import matplotlib.pyplot as pp


def multicomponent_model(obs_data, obs_ivm, psf_data, psf_ivm,
                         components=None, mag_zp=0, mask_file=None, **kwargs):
    """
    Multi-component model for MCMC psf fitting. Components is a list of
    ComponentBase subclasses, or a model definition python file parsable by
    model_parser.
    Returns an MCMC object ready for sampling. **kwargs are passed to MCMC()
    """
    if isinstance(components, basestring):
        try:
            components = component_list_from_file(components)
        except IOError, err:
            message = 'Unable to open components file {}. Does it exist?'
            err.message = message.format(components)
            raise err

    np.seterr(divide='ignore')

    obs_data, obs_var, bad_px = preprocess_obs(obs_data, obs_ivm, mask_file)

    # Read in PSFs, set up selector
    if isinstance(psf_data, basestring) or isinstance(psf_ivm, basestring):
        psf_data, psf_ivm = [psf_data], [psf_ivm]
    psf_list, var_list = [], []
    for psf, ivm in zip(psf_data, psf_ivm):
        f_psf, f_psf_var = preprocess_psf(psf, ivm, obs_data.shape)
        psf_list.append(f_psf)
        var_list.append(f_psf_var)
    psf_select = PSFSelector(psf_list, var_list, filenames=psf_data)

    # pre-compute data x,y coordinates
    data_coords = array_coords(obs_data)

    model_comps = []
    stochastics = []
    sky = Sky(adu=0.0)

    for count, component in enumerate(components):
        component.update_trace_names(count=count)
        # Sky is added after convolution
        if isinstance(component, Sky):
            sky = component
            stochastics += [sky]
        else:
            model_comps += [component]

    @deterministic(plot=False, trace=False)
    def raw_model(model_comps=model_comps):
        modelpx = np.zeros_like(obs_var)
        for comp in model_comps:
            comp.add_to_array(modelpx, mag_zp, coords=data_coords)
        return modelpx

    @deterministic(plot=False, trace=False)
    def convolved_model(psf=psf_select, raw_model=raw_model, sky=sky):
        cmodel = convolve(raw_model, psf.psf())
        return cmodel + sky.adu

    @deterministic(plot=False, trace=False)
    def composite_ivm(obs_var=obs_var, psf=psf_select, raw_model=raw_model):
        # compute model variance
        model_var = convolve(raw_model**2, psf.variance())
        compIVM = 1 / (model_var + obs_var)
        return compIVM
<commit_msg>Problem identified for odd-sized arrays when doing fourier convolution<commit_after>from __future__ import division
import numpy as np
from pymc import deterministic, Normal
from pymc.MCMC import MCMC
from .array_utils import *
from .ModelComponents import Sky, PSF
from .ModelComponents.PSFSelector import PSFSelector
from .model_parser import component_list_from_file
# import matplotlib.pyplot as pp


def multicomponent_model(obs_data, obs_ivm, psf_data, psf_ivm,
                         components=None, mag_zp=0, mask_file=None, **kwargs):
    """
    Multi-component model for MCMC psf fitting. Components is a list of
    ComponentBase subclasses, or a model definition python file parsable by
    model_parser.
    Returns an MCMC object ready for sampling. **kwargs are passed to MCMC()
    """
    if isinstance(components, basestring):
        try:
            components = component_list_from_file(components)
        except IOError, err:
            message = 'Unable to open components file {}. Does it exist?'
            err.message = message.format(components)
            raise err

    np.seterr(divide='ignore')

    obs_data, obs_var, bad_px = preprocess_obs(obs_data, obs_ivm, mask_file)

    # Read in PSFs, set up selector
    if isinstance(psf_data, basestring) or isinstance(psf_ivm, basestring):
        psf_data, psf_ivm = [psf_data], [psf_ivm]
    psf_list, var_list = [], []
    for psf, ivm in zip(psf_data, psf_ivm):
        f_psf, f_psf_var = preprocess_psf(psf, ivm, obs_data.shape)
        psf_list.append(f_psf)
        var_list.append(f_psf_var)
    psf_select = PSFSelector(psf_list, var_list, filenames=psf_data)

    # pre-compute data x,y coordinates
    data_coords = array_coords(obs_data)

    model_comps = []
    stochastics = []
    sky = Sky(adu=0.0)

    for count, component in enumerate(components):
        component.update_trace_names(count=count)
        # Sky is added after convolution
        if isinstance(component, Sky):
            sky = component
            stochastics += [sky]
        else:
            model_comps += [component]

    @deterministic(plot=False, trace=False)
    def raw_model(model_comps=model_comps):
        modelpx = np.zeros_like(obs_var)
        for comp in model_comps:
            comp.add_to_array(modelpx, mag_zp, coords=data_coords)
        return modelpx

    @deterministic(plot=False, trace=False)
    def convolved_model(psf=psf_select, raw_model=raw_model, sky=sky):
        cmodel = convolve(raw_model, psf.psf())
        return cmodel + sky.adu

    @deterministic(plot=False, trace=False)
    def composite_ivm(obs_var=obs_var, psf=psf_select, raw_model=raw_model):
        # compute model variance
        # FIXME: odd sizes (127x127) cause this to screw up in one dimension?
        model_var = convolve(raw_model**2, psf.variance())
        compIVM = 1 / (model_var + obs_var)
        return compIVM

{'fraction_non_alphanumeric': 0.08528634361233481, 'fraction_numerical': 0.0028193832599118945, 'mean_word_length': 3.140043763676149, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173039', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1827, 'n_words': 460}