<commit_before>#solarnmf_functions.py

#<NAME>
#27 January 2015

#Description:

#Inputs:

#Outputs:

#Import necessary modules
import numpy as np
import solarnmf_process_data as spd

def make_t_matrix(toption,**kwargs):
    """Set up the observation matrix either using a series of simulated gaussians or a dataset specified by a keyword argument.
    
    Parameters
    ----------
    toption: str
        Option that says whether this is a simulated ('simulation') observation (a series of gaussians) or an observation to be pulled from a file ('data')
    **kwargs
        nx: int
            number of columns for the simulated data set
        ny: int
            number of rows for the simulated data set
        p: int
            number of sources in the simulated data set
        
    Returns
    -------
    T: numpy two-dimensional array
        Matrix that represents the observation we want to factorize
    
    """
    #Check which option to use
    if "simulation" == toption:
        
        #Parse arguments for simulation
        for key in kwargs:
            if key == "nx":
                nx = int(kwargs[key])
            elif key == "ny":
                ny = int(kwargs[key])
            elif key == "p":
                p = int(kwargs[key])
            
        #Call function to make simulated results
        sim_result = make_gaussians(nx,ny,p,format=kwargs['format'])
        
        #If we are using Gauusian matrices, return the matrices
        if kwargs['format'] == 'matrix':
            #Return values 
            return {'target':sim_result['target'],'T':sim_result['T']}
        else:
            #Format vector as matrix
            x = sim_result['x']
            
            #Make sure data is normalized
            x = x/np.max(x)
        
            #Apply the smoothing function
            x_smooth = spd.smooth_1d_window(x,window_length=11,window='hanning')
        
            #Make a matrix representation of the time series
            x_mat = spd.ts2mat(x_smooth,len(x_smooth),0.1)
    
            #Rotate the important data along the diagonal
            x_mat_rot = spd.crop_and_rotate(x_mat,kwargs['angle'])
        
            #Return the rotated matrix and the original vector
            return {'T':x_mat_rot,'x':x,'target':sim_result['target']}
        
    if "data" == toption:
        for key in kwargs:
            if key == "filename":
                filename = kwargs[key]
            elif key == "file_format":
                file_format = kwargs[key]
        
        #Load the specified file
        x = np.loadtxt(filename)
        
        #Make sure data is normalized
        x = x/np.max(x)
        
        #Apply the smoothing function
        x_smooth = spd.smooth_1d_window(x,window_length=11,window='hanning')
        
        #Make a matrix representation of the time series
        x_mat = spd.ts2mat(x_smooth,len(x_smooth),0.1)
    
        #Rotate the important data along the diagonal
        x_mat_rot = spd.crop_and_rotate(x_mat,kwargs['angle'])
        
        #Return the rotated matrix and the original vector
        return {'T':x_mat_rot,'x':x}


def make_gaussians(nx,ny,p,**kwargs):
    
    #Check the format
    if kwargs['format'] == 'matrix':
        #Calculate standard deviations in x and y
        sigma_x = 0.15*np.ones((1,p))
        sigma_y = 0.15*np.ones((1,p))
    
        #Generate random center positions for pulses (normalized to [0,1])
        centers = np.random.rand(p,2)
    
        #Preallocate for composite and individual pulses
        T = np.zeros((ny,nx))
        target = np.zeros((ny,nx,p))
    
        #Set up the X and Y meshes
        X,Y = np.meshgrid(np.linspace(0,1,nx),np.linspace(0,1,ny))
    
        #Calculate the Gaussian pulses and add them to the T matrix
        for i in range(p):
            #Calculate Gaussian pulse
            z = np.exp(-1/(2*(sigma_x[0,i])**2)*((X - centers[i,0])**2) - 1/(2*(sigma_y[0,i])**2)*((Y - centers[i,1])**2))
            #Add to total T matrix
            T = T + z
            #Save the events that make up the T matrix
            target[:,:,i] = z
        
        #Normalize the T matrix to one
        T = T/np.max(T)
    
        #Return the X,Y,T and target matrices
        return {'target':target, 'T':T}
    else:
        #Create list for simulated gaussians
        target = []
        #Initialize total
        x = 0.0
        #Simulate 1D timeseries
        #Create series of Gaussians
        t = np.linspace(-1,1,nx)
        for i in range(p):
            #Get random sign
            if np.random.rand() > 0.5:
                mu_sign = -1.0
            else:
                mu_sign = 1.0
            #Create the Gaussian
            temp = np.random.rand()*np.exp(-(t+mu_sign*np.random.rand())**2/(2.0*0.1**2))
            #Add to total
            x = x + temp
            #Save the components
            target.append(temp)
            
        #Return the timeseries
        return {'target':target,'x':x}
    

def initialize_uva(nx,ny,q,r,r_iter,T):
    
    #Initialize U and V matrices
    utemp = np.random.rand(ny,q)
    vtemp = np.random.rand(q,nx)
    atemp = np.dot(utemp,vtemp)
    
    #Set artificial value for div
    div_final = 1000
    #Set convergence limit for div_limit
    div_limit = 1.0e-6
    
    #Begin loop to check divergence criteria
    for i in range(r):
        #Print some output
        print "Starting initialization iteration ",i
        #Call the minimizer
        utemp,vtemp,atemp,dtemp = minimize_div(utemp,vtemp,T,atemp,r_iter,div_limit)
 
        #Check the new value of div
        if dtemp[-1] < div_final:
            div_final = d_temp
            u = utemp
            v = vtemp
        
        #Generate next random u and v
        utemp = np.random.rand(ny,q)
        vtemp = np.random.rand(q,nx)
        atemp = np.dot(utemp,vtemp)
        
    #Create the initial a matrix from the initial u and v matrices
    A = np.dot(u,v)
        
    #Return u and v values with lowest final div value
    return u,v,A
        
        
    
def minimize_div(u,v,T,A,max_i,div_limit):
    
    #Set epsilon parameter to make sure everything is non-negative
    eps = 1.0e-6
    
    #Initialize vector for divergence metric<commit_msg>Fixes to prevent leaving initialization without setting u and v<commit_after>#solarnmf_functions.py

#<NAME>
#27 January 2015

#Description:

#Inputs:

#Outputs:

#Import necessary modules
import numpy as np
import solarnmf_process_data as spd

def make_t_matrix(toption,**kwargs):
    """Set up the observation matrix either using a series of simulated gaussians or a dataset specified by a keyword argument.
    
    Parameters
    ----------
    toption: str
        Option that says whether this is a simulated ('simulation') observation (a series of gaussians) or an observation to be pulled from a file ('data')
    **kwargs
        nx: int
            number of columns for the simulated data set
        ny: int
            number of rows for the simulated data set
        p: int
            number of sources in the simulated data set
        
    Returns
    -------
    T: numpy two-dimensional array
        Matrix that represents the observation we want to factorize
    
    """
    #Check which option to use
    if "simulation" == toption:
        
        #Parse arguments for simulation
        for key in kwargs:
            if key == "nx":
                nx = int(kwargs[key])
            elif key == "ny":
                ny = int(kwargs[key])
            elif key == "p":
                p = int(kwargs[key])
            
        #Call function to make simulated results
        sim_result = make_gaussians(nx,ny,p,format=kwargs['format'])
        
        #If we are using Gauusian matrices, return the matrices
        if kwargs['format'] == 'matrix':
            #Return values 
            return {'target':sim_result['target'],'T':sim_result['T']}
        else:
            #Format vector as matrix
            x = sim_result['x']
            
            #Make sure data is normalized
            x = x/np.max(x)
        
            #Apply the smoothing function
            x_smooth = spd.smooth_1d_window(x,window_length=11,window='hanning')
        
            #Make a matrix representation of the time series
            x_mat = spd.ts2mat(x_smooth,len(x_smooth),0.1)
    
            #Rotate the important data along the diagonal
            x_mat_rot = spd.crop_and_rotate(x_mat,kwargs['angle'])
        
            #Return the rotated matrix and the original vector
            return {'T':x_mat_rot,'x':x,'target':sim_result['target']}
        
    if "data" == toption:
        for key in kwargs:
            if key == "filename":
                filename = kwargs[key]
            elif key == "file_format":
                file_format = kwargs[key]
        
        #Load the specified file
        x = np.loadtxt(filename)
        
        #Make sure data is normalized
        x = x/np.max(x)
        
        #Apply the smoothing function
        x_smooth = spd.smooth_1d_window(x,window_length=11,window='hanning')
        
        #Make a matrix representation of the time series
        x_mat = spd.ts2mat(x_smooth,len(x_smooth),0.1)
    
        #Rotate the important data along the diagonal
        x_mat_rot = spd.crop_and_rotate(x_mat,kwargs['angle'])
        
        #Return the rotated matrix and the original vector
        return {'T':x_mat_rot,'x':x}


def make_gaussians(nx,ny,p,**kwargs):
    
    #Check the format
    if kwargs['format'] == 'matrix':
        #Calculate standard deviations in x and y
        sigma_x = 0.15*np.ones((1,p))
        sigma_y = 0.15*np.ones((1,p))
    
        #Generate random center positions for pulses (normalized to [0,1])
        centers = np.random.rand(p,2)
    
        #Preallocate for composite and individual pulses
        T = np.zeros((ny,nx))
        target = np.zeros((ny,nx,p))
    
        #Set up the X and Y meshes
        X,Y = np.meshgrid(np.linspace(0,1,nx),np.linspace(0,1,ny))
    
        #Calculate the Gaussian pulses and add them to the T matrix
        for i in range(p):
            #Calculate Gaussian pulse
            z = np.exp(-1/(2*(sigma_x[0,i])**2)*((X - centers[i,0])**2) - 1/(2*(sigma_y[0,i])**2)*((Y - centers[i,1])**2))
            #Add to total T matrix
            T = T + z
            #Save the events that make up the T matrix
            target[:,:,i] = z
        
        #Normalize the T matrix to one
        T = T/np.max(T)
    
        #Return the X,Y,T and target matrices
        return {'target':target, 'T':T}
    else:
        #Create list for simulated gaussians
        target = []
        #Initialize total
        x = 0.0
        #Simulate 1D timeseries
        #Create series of Gaussians
        t = np.linspace(-1,1,nx)
        for i in range(p):
            #Get random sign
            if np.random.rand() > 0.5:
                mu_sign = -1.0
            else:
                mu_sign = 1.0
            #Create the Gaussian
            temp = np.random.rand()*np.exp(-(t+mu_sign*np.random.rand())**2/(2.0*0.1**2))
            #Add to total
            x = x + temp
            #Save the components
            target.append(temp)
            
        #Return the timeseries
        return {'target':target,'x':x}
    

def initialize_uva(nx,ny,q,r,r_iter,T):
    
    #Initialize U and V matrices
    utemp = np.random.rand(ny,q)
    vtemp = np.random.rand(q,nx)
    atemp = np.dot(utemp,vtemp)
    
    #Set artificial value for div
    div_final = 1000
    #Set convergence limit for div_limit
    div_limit = 1.0e-6
    
    #Initialize flag
    good_start = False
    
    #Begin loop to check divergence criteria
    for i in range(r):
        #Print some output
        print "Starting initialization iteration ",i
        #Call the minimizer
        utemp,vtemp,atemp,dtemp = minimize_div(utemp,vtemp,T,atemp,r_iter,div_limit)
 
        #Check the new value of div
        if dtemp[-1] < div_final:
            div_final = d_temp
            u = utemp
            v = vtemp
            good_start = True
            break
        
        #Generate next random u and v
        utemp = np.random.rand(ny,q)
        vtemp = np.random.rand(q,nx)
        atemp = np.dot(utemp,vtemp)
        
    #Check if u,v for decent div value were found
    if good_start == False:
        print "Initializing with matrices yielding div = ",dtemp[-1]
        u = utemp
        v = vtemp
        
    #Create the initial a matrix from the initial u and v matrices
    A = np.dot(u,v)
        
    #Return u and v values with lowest final div value
    return u,v,A
        
        
    
def minimize_div(u,v,T,A,max_i,div_limit):
    
    #Set epsilon parameter to make sure everything is non-negative
    eps = 1.0e-6
    
    #Initialize vector for divergence metric
{'fraction_non_alphanumeric': 0.09850422382391692, 'fraction_numerical': 0.011392699372239015, 'mean_word_length': 1.8180825507752785, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, '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': '2719829', 'n_tokens_mistral': 3972, 'n_tokens_neox': 3853, 'n_words': 1170}