<commit_before>#!/usr/bin/env python
import numpy as np
import logging
import solvers
import timing
from converger import Converger
from tikhonov import Perturb
from tikhonov import tikhonov_matrix
from timing import funtime
import scipy.sparse

logger = logging.getLogger(__name__)

##------------------------------------------------------------------------------
@funtime
def jacobian_fd(m_o,
                system,
                system_args=None,
                system_kwargs=None,
                dm=1e-4,
                dtype=None):
  '''
  Parameters
  ----------

    system: function where the first argument is a list of model
      parameters and the output is a data list

    m_o: location in model space where the jacobian will be
      computed. must be a mutable sequence (e.g. np.array or list)

    system_args: additional arguments to system

    system_kargs: additional key word arguments to system

    dm: step size used for the finite difference approximation

  Returns
  -------
    J:  jacobian matrix with dimensions: len(data),len(parameters)

  ''' 
  if system_args is None:
    system_args = []
  if system_kwargs is None:
    system_kwargs = {}

  data_o = system(m_o,*system_args,**system_kwargs)
  data_o = np.asarray(data_o)
  param_no = len(m_o)
  data_no = len(data_o)
  Jac = np.zeros((data_no,param_no),dtype=dtype)
  i = 0
  for m_pert in Perturb(m_o,dm):
    data_pert = system(m_pert,*system_args,**system_kwargs)
    data_pert = np.asarray(data_pert)
    Jac[:,i]  = (data_pert - data_o)/dm
    i += 1

  return Jac

##------------------------------------------------------------------------------
@funtime
def covariance_to_weight(C):
  '''returns the weight matrix, W, which satisfies

       np.linalg.inv(C) = W.transpose().dot(W)                     (1)

     This is done using Cholesky factorization of C to find A such
     that
  
       C = A.dot(A.transpose()).                                   (2)

     W is then just the inverse of A.  The decomposition of C into W
     is not unique, but since The solution for any least squares 
     problem depends on C, rather than W, any value of W 'should' do 
     just fine as long as it satisfies (1).
  
     Notes
     -----

     This function is SLOW

  '''
  N = np.shape(C)[0]
  A = np.linalg.cholesky(C)
  W = scipy.linalg.solve_triangular(A,np.eye(N),lower=True)

  return W
  
##------------------------------------------------------------------------------
def _residual(system,
               data,
               prior,
               system_args,<commit_msg>commit before switch<commit_after>#!/usr/bin/env python
import numpy as np
import logging
import solvers
import timing
from converger import Converger
from tikhonov import Perturb
from tikhonov import tikhonov_matrix
from timing import funtime
import scipy.sparse

logger = logging.getLogger(__name__)

##------------------------------------------------------------------------------
@funtime
def jacobian_fd(m_o,
                system,
                system_args=None,
                system_kwargs=None,
                dm=1e-4,
                dtype=None):
  '''
  Parameters
  ----------

    system: function where the first argument is a list of model
      parameters and the output is a data list

    m_o: location in model space where the jacobian will be
      computed. must be a mutable sequence (e.g. np.array or list)

    system_args: additional arguments to system

    system_kargs: additional key word arguments to system

    dm: step size used for the finite difference approximation

  Returns
  -------
    J:  jacobian matrix with dimensions: len(data),len(parameters)

  ''' 
  if system_args is None:
    system_args = []
  if system_kwargs is None:
    system_kwargs = {}

  data_o = system(m_o,*system_args,**system_kwargs)
  data_o = np.asarray(data_o)
  param_no = len(m_o)
  data_no = len(data_o)
  Jac = np.zeros((data_no,param_no),dtype=dtype)
  i = 0
  for m_pert in Perturb(m_o,dm):
    data_pert = system(m_pert,*system_args,**system_kwargs)
    data_pert = np.asarray(data_pert)
    Jac[:,i]  = (data_pert - data_o)/dm
    i += 1

  return Jac

##------------------------------------------------------------------------------
@funtime
def covariance_to_weight(C):
  '''returns the weight matrix, W, which satisfies

       np.linalg.inv(C) = W.transpose().dot(W)                     (1)

     This is done using Cholesky factorization of C to find A such
     that
  
       C = A.dot(A.transpose()).                                   (2)

     W is then just the inverse of A.  The decomposition of C into W
     is not unique, but since The solution for any least squares 
     problem depends on C, rather than W, any value of W 'should' do 
     just fine as long as it satisfies (1).
  
     Notes
     -----

     This function is SLOW

  '''
  N = np.shape(C)[0]
  A = np.linalg.cholesky(C)
  W = scipy.linalg.solve_triangular(A,np.eye(N),lower=True)
  return W
  
##------------------------------------------------------------------------------
def _residual(system,
               data,
               prior,
               system_args,
{'fraction_non_alphanumeric': 0.1731886865555986, 'fraction_numerical': 0.0030995738086013174, 'mean_word_length': 1.778794402583423, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3990576', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1492, 'n_words': 477}