<commit_before>"""Various python classes for rsr package
Author: <NAME> <<EMAIL>>
"""

import numpy as np
import pdf, fit, invert
import matplotlib.pyplot as plt
from scipy import interpolate, stats
from astroML.plotting import hist
import subradar as sr


class Statfit:
    """Class holding statistical fit results
    """
    def __init__(self, sample, func, values, params, chisqr, redchi,
                 elapsed, nfev, message, success, residual, x, n, edges, bins):
        self.sample = sample
        self.func = func
        self.values = values
        self.params = params
        self.chisqr = chisqr
        self.redchi = redchi
        self.elapsed = elapsed
        self.nfev = nfev
        self.message = message
        self.success = success
        self.residual = residual
        self.x = x
        self.n = n
        self.edges = edges
        self.bins = bins


    def power(self, db=True):
        """Total (pt), coherent (pc), and incoherent (pn) components in power
        """
        pt, pc, pn = np.average(self.sample)**2, self.values['a']**2, \
                     2*self.values['s']**2
        if db is True:
            pt, pc, pn = 10*np.log10(pt), 10*np.log10(pc), 10*np.log10(pn)
        return {'pt':pt, 'pc':pc, 'pn':pn, 'pc-pn':pc-pn}


    def crl(self, **kwargs):
        """Correlation coefficient between distribution and theoretical fit
        """
        try:
            out = np.corrcoef(self.n, self.n+self.residual)[0,1]
        except:
            out = np.nan

        if np.isfinite(out) is False:
            out = np.nan
        return out


    def invert(self, frq=60e6, th_max=1e-4, cl_logrange=[5], n=100,
        method='iem', approx='Small_S', ):
        """Invert signal components into physical properties
        """
        if method is 'spm':
            out =  getattr(invert, method)(frq, self.power()['pc'],
                   self.power()['pn'])
        if method is 'iem':
            out = sr.invert.power2srf_norminc(method, approx,
            self.power()['pc'], self.power()['pn'],
            th_max=th_max, wf=frq, cl_logrange=cl_logrange, n=n)
        return out


    def plot(self, ylabel='Normalized Probability', color='k', ls='-',
        bins=None, fbins=100, alpha=.1, method='compound',
        histtype='stepfilled', xlim=None):
        """Plot histogram and pdf
        """
        if xlim is None: xlim = [np.min(self.edges), np.max(self.edges)]
        if bins is None: bins = self.bins

        x = np.linspace(xlim[0], xlim[1], fbins)
        hist(self.sample, bins=bins, color=color, edgecolor=color,
             alpha=alpha, histtype=histtype, normed=True, range=xlim)
        plt.plot(x, self.func(self.values, x, method=method), color=color,
                 ls=ls, linewidth=2)
        plt.xlim(xlim)
        plt.ylabel(ylabel, size=17)
        plt.xlabel('Amplitude', size=17)
        plt.yticks(size='17')
        plt.xticks(size='17')


    def report(self, frq=60e6, inv='spm'):
        """Print a report for the fit
        """
        buff = []
        add = buff.append

        add('[%i] [%6.2f s.] [%3.0f eval.] [%3.3f]\n'
            % (self.flag(), self.elapsed, self.nfev, self.crl()))

        add('%s\n' % (self.message))

        for i, key in enumerate(self.values.keys()):
            add('%s = %.3e, ' % (key, self.params[key].value))

        add('\n')

        for i, key in enumerate(self.power().keys()):
            add('%s = %3.1f dB, ' % (key, self.power()[key]))

        add('\n')

        p = self.invert(frq=frq, method=inv)
        add('%s @ %.0f MHz gives, eps = %.3f, sh = %.2e m' % (inv.upper(),
            frq*1e-6, p['eps'], p['sh']))

        add('\n')

        out = "".join(buff)
        print(out)


    def flag(self):
        """0 is bad data, 1 is good data
        """
        out = self.success * np.isfinite(self.crl()) * (self.crl() > 0)
        return(int(out))
<commit_msg>inversion spans a larger field of solutions<commit_after>"""Various python classes for rsr package
Author: <NAME> <<EMAIL>>
"""

import numpy as np
import pdf, fit, invert
import matplotlib.pyplot as plt
from scipy import interpolate, stats
from astroML.plotting import hist
import subradar as sr


class Statfit:
    """Class holding statistical fit results
    """
    def __init__(self, sample, func, values, params, chisqr, redchi,
                 elapsed, nfev, message, success, residual, x, n, edges, bins):
        self.sample = sample
        self.func = func
        self.values = values
        self.params = params
        self.chisqr = chisqr
        self.redchi = redchi
        self.elapsed = elapsed
        self.nfev = nfev
        self.message = message
        self.success = success
        self.residual = residual
        self.x = x
        self.n = n
        self.edges = edges
        self.bins = bins


    def power(self, db=True):
        """Total (pt), coherent (pc), and incoherent (pn) components in power
        """
        pt, pc, pn = np.average(self.sample)**2, self.values['a']**2, \
                     2*self.values['s']**2
        if db is True:
            pt, pc, pn = 10*np.log10(pt), 10*np.log10(pc), 10*np.log10(pn)
        return {'pt':pt, 'pc':pc, 'pn':pn, 'pc-pn':pc-pn}


    def crl(self, **kwargs):
        """Correlation coefficient between distribution and theoretical fit
        """
        try:
            out = np.corrcoef(self.n, self.n+self.residual)[0,1]
        except:
            out = np.nan

        if np.isfinite(out) is False:
            out = np.nan
        return out


    def invert(self, frq=60e6, th_max=1e-4, cl_logrange=[5], n=1e4,
        ep_range=[1,100], method='iem', approx='Small_S', ):
        """Invert signal components into physical properties
        """
        if method is 'spm':
            out =  getattr(invert, method)(frq, self.power()['pc'],
                   self.power()['pn'])
        if method is 'iem':
            out = sr.invert.power2srf_norminc(method, approx,
            self.power()['pc'], self.power()['pn'], ep_range=ep_range,
            th_max=th_max, wf=frq, cl_logrange=cl_logrange, n=n)
        return out


    def plot(self, ylabel='Normalized Probability', color='k', ls='-',
        bins=None, fbins=100, alpha=.1, method='compound',
        histtype='stepfilled', xlim=None):
        """Plot histogram and pdf
        """
        if xlim is None: xlim = [np.min(self.edges), np.max(self.edges)]
        if bins is None: bins = self.bins

        x = np.linspace(xlim[0], xlim[1], fbins)
        hist(self.sample, bins=bins, color=color, edgecolor=color,
             alpha=alpha, histtype=histtype, normed=True, range=xlim)
        plt.plot(x, self.func(self.values, x, method=method), color=color,
                 ls=ls, linewidth=2)
        plt.xlim(xlim)
        plt.ylabel(ylabel, size=17)
        plt.xlabel('Amplitude', size=17)
        plt.yticks(size='17')
        plt.xticks(size='17')


    def report(self, frq=60e6, inv='spm'):
        """Print a report for the fit
        """
        buff = []
        add = buff.append

        add('[%i] [%6.2f s.] [%3.0f eval.] [%3.3f]\n'
            % (self.flag(), self.elapsed, self.nfev, self.crl()))

        add('%s\n' % (self.message))

        for i, key in enumerate(self.values.keys()):
            add('%s = %.3e, ' % (key, self.params[key].value))

        add('\n')

        for i, key in enumerate(self.power().keys()):
            add('%s = %3.1f dB, ' % (key, self.power()[key]))

        add('\n')

        p = self.invert(frq=frq, method=inv)
        add('%s @ %.0f MHz gives, eps = %.3f, sh = %.2e m' % (inv.upper(),
            frq*1e-6, p['eps'], p['sh']))

        add('\n')

        out = "".join(buff)
        print(out)


    def flag(self):
        """0 is bad data, 1 is good data
        """
        out = self.success * np.isfinite(self.crl()) * (self.crl() > 0)
        return(int(out))

{'fraction_non_alphanumeric': 0.1575091575091575, 'fraction_numerical': 0.0162940507768094, 'mean_word_length': 2.067803177063154, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '592111', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2643, 'n_words': 652}