<commit_before>import numpy as np
import math
import scipy
import scipy.optimize
import lightcurve
import generaltools

# this is the right version of powerspectrum.py

### add up a number of periodograms or average them
## psall: list of periograms (note: *must* have same resolution!)
## method: 
##     'average', 'avg', 'mean': average periodograms
##     'add', 'sum': add them up
def add_ps(psall, method='avg'):

    pssum = np.zeros(len(psall[0].ps))
    for x in psall:
        pssum = pssum + x.ps

    if method.lower() in ['average', 'avg', 'mean']:
        pssum = pssum/len(psall)

    psnew = PowerSpectrum()
    psnew.freq = psall[0].freq
    psnew.ps = pssum
    psnew.n = psall[0].n
    psnew.df = psall[0].df
    psnew.norm = psall[0].norm
    return psnew 

### REDOING THIS IN CLASSES ####
class PowerSpectrum(lightcurve.Lightcurve):
    def __init__(self, lc = None, counts = None, nphot=None, norm='leahy', verbose=False):

        self.norm = norm

        if isinstance(lc, lightcurve.Lightcurve) and counts == None:             
            pass
            # print "You put in an object of type Lightcurve. Go you!"
        elif not lc == None and not counts == None:
            if verbose == True:
                print "You put in a standard light curve (I hope). Converting to object of type Lightcurve"
            lc = lightcurve.Lightcurve(lc, counts, verbose=verbose)
        else:
            self.freq = None
            self.ps = None
            self.df = None
            return
        #    raise Exception("You screwed up. Put in either a Lightcurve or two lists of time, counts")

        if nphot == None:
            nphots = np.sum(lc.counts)
        else:
            nphots = nphot
        nel = np.round(lc.tseg/lc.res)
        #print(nel)
        #if lc.tseg/lc.res > nel:
        #    nel = nel + 1
     
        #print "lc.tseg: " + str(lc.tseg)
        #print "lc.res: " + str(lc.res)
        df = 1.0/lc.tseg
        fnyquist = 0.5/(lc.time[1]-lc.time[0])
        #freq = np.arange(nel/2)*df ### make a list of frequencies 
        #print "len(freq): " + str(len(freq))
        #print "len(lc.counts): " + str(len(lc.counts))
        fourier= scipy.fft(lc.counts) ### do Fourier transform
        f2 = fourier.conjugate() ### do conjugate
        ff = f2*fourier   ### multiply both together
        fr = np.array([x.real for x in ff]) ### get out the real part of ff
        ps = 2.0*fr[0: (nel/2 )]/nphots
        freq = np.arange(len(ps))*df
        #print "ps0 : " + str(ps[0])
        #print "df : " + str(df)
        #print "nel: " + str(nel)
        #print "nphots" + str(nphots)
 

        if norm.lower() in ['leahy']:
            self.ps = ps#2.0*fr[0: (nel/2 -1)]/nphots   ### do the power spectrum
            
        elif norm.lower() in ['rms']:
            #self.ps = 2.0*lc.tseg*2.0*np.sqrt(nel)*fr[0:(nel/2 -1)]/(nphots**2.0)
            self.ps = ps/(df*nphots)        
            #print "ps rms: " + str(self.ps[0])
        ### put frequency to mid-frequencies
        elif norm.lower() in ['variance', 'var']:
            #self.ps = ps*nphots/len(lc.counts)
            self.ps = ps*nphots/len(lc.counts)**2.0

            #self.ps = ps*nphots/(2.0*fnyquist)**2.0


        self.freq = [f+(freq[1]-freq[0])/2.0 for f in freq]
        self.df = self.freq[1] - self.freq[0]
        self.nphots = nphots
        self.n = len(lc.counts)

    def rebinps(self, res, verbose=False):
        ### frequency range of power spectrum
        flen = (self.freq[-1] - self.freq[0])
        ### calculate number of new bins in rebinned spectrum
        bins = np.floor(flen/res) 
        ### calculate *actual* new resolution
        self.bindf = flen/bins
        ### rebin power spectrum to new resolution
        binfreq, binps, dt = self._rebin(self.freq, self.ps, bins, method='mean', verbose=verbose)
        newps = PowerSpectrum()
        newps.freq = binfreq
        newps.ps = binps
        newps.df = dt

        return newps


    ### NEED TO TEST THIS!! DON'T KNOW WHETHER IT WORKS!!!
    def rebingeo(self, factor, method = 'sum'):
        freq = np.array(self.freq)
        ps = np.array(self.ps)<commit_msg>Added attribute nphots to rebinned powerspectrum<commit_after>import numpy as np
import math
import scipy
import scipy.optimize
import lightcurve
import generaltools

# this is the right version of powerspectrum.py

### add up a number of periodograms or average them
## psall: list of periograms (note: *must* have same resolution!)
## method: 
##     'average', 'avg', 'mean': average periodograms
##     'add', 'sum': add them up
def add_ps(psall, method='avg'):

    pssum = np.zeros(len(psall[0].ps))
    for x in psall:
        pssum = pssum + x.ps

    if method.lower() in ['average', 'avg', 'mean']:
        pssum = pssum/len(psall)

    psnew = PowerSpectrum()
    psnew.freq = psall[0].freq
    psnew.ps = pssum
    psnew.n = psall[0].n
    psnew.df = psall[0].df
    psnew.norm = psall[0].norm
    return psnew 

### REDOING THIS IN CLASSES ####
class PowerSpectrum(lightcurve.Lightcurve):
    def __init__(self, lc = None, counts = None, nphot=None, norm='leahy', verbose=False):

        self.norm = norm

        if isinstance(lc, lightcurve.Lightcurve) and counts == None:             
            pass
            # print "You put in an object of type Lightcurve. Go you!"
        elif not lc == None and not counts == None:
            if verbose == True:
                print "You put in a standard light curve (I hope). Converting to object of type Lightcurve"
            lc = lightcurve.Lightcurve(lc, counts, verbose=verbose)
        else:
            self.freq = None
            self.ps = None
            self.df = None
            return
        #    raise Exception("You screwed up. Put in either a Lightcurve or two lists of time, counts")

        if nphot == None:
            nphots = np.sum(lc.counts)
        else:
            nphots = nphot
        nel = np.round(lc.tseg/lc.res)
        #print(nel)
        #if lc.tseg/lc.res > nel:
        #    nel = nel + 1
     
        #print "lc.tseg: " + str(lc.tseg)
        #print "lc.res: " + str(lc.res)
        df = 1.0/lc.tseg
        fnyquist = 0.5/(lc.time[1]-lc.time[0])
        #freq = np.arange(nel/2)*df ### make a list of frequencies 
        #print "len(freq): " + str(len(freq))
        #print "len(lc.counts): " + str(len(lc.counts))
        fourier= scipy.fft(lc.counts) ### do Fourier transform
        f2 = fourier.conjugate() ### do conjugate
        ff = f2*fourier   ### multiply both together
        fr = np.array([x.real for x in ff]) ### get out the real part of ff
        ps = 2.0*fr[0: (nel/2 )]/nphots
        freq = np.arange(len(ps))*df
        #print "ps0 : " + str(ps[0])
        #print "df : " + str(df)
        #print "nel: " + str(nel)
        #print "nphots" + str(nphots)
 

        if norm.lower() in ['leahy']:
            self.ps = ps#2.0*fr[0: (nel/2 -1)]/nphots   ### do the power spectrum
            
        elif norm.lower() in ['rms']:
            #self.ps = 2.0*lc.tseg*2.0*np.sqrt(nel)*fr[0:(nel/2 -1)]/(nphots**2.0)
            self.ps = ps/(df*nphots)        
            #print "ps rms: " + str(self.ps[0])
        ### put frequency to mid-frequencies
        elif norm.lower() in ['variance', 'var']:
            #self.ps = ps*nphots/len(lc.counts)
            self.ps = ps*nphots/len(lc.counts)**2.0

            #self.ps = ps*nphots/(2.0*fnyquist)**2.0


        self.freq = [f+(freq[1]-freq[0])/2.0 for f in freq]
        self.df = self.freq[1] - self.freq[0]
        self.nphots = nphots
        self.n = len(lc.counts)

    def rebinps(self, res, verbose=False):
        ### frequency range of power spectrum
        flen = (self.freq[-1] - self.freq[0])
        ### calculate number of new bins in rebinned spectrum
        bins = np.floor(flen/res) 
        ### calculate *actual* new resolution
        self.bindf = flen/bins
        ### rebin power spectrum to new resolution
        binfreq, binps, dt = self._rebin(self.freq, self.ps, bins, method='mean', verbose=verbose)
        newps = PowerSpectrum()
        newps.freq = binfreq
        newps.ps = binps
        newps.df = dt
        newps.nphots = binps[0]
        newps.n = 2*len(binps)
        return newps


    ### NEED TO TEST THIS!! DON'T KNOW WHETHER IT WORKS!!!
    def rebingeo(self, factor, method = 'sum'):
        freq = np.array(self.freq)
        ps = np.array(self.ps)
{'fraction_non_alphanumeric': 0.14042754222274714, 'fraction_numerical': 0.012046769812212118, 'mean_word_length': 2.029695885509839, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4845539', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2865, 'n_words': 770}