(request, context)
Calculate the correlation coefficient for two columns. Scalar function.
        :param request: an iterable sequence of RowData
        :param context:
        :return: the correlation coefficient for each row
        :Qlik expression examples:
        :<AAI Connection Name>.Pearson('1;NA;3;4;5;6.9', ';11;12;;14;')
        :<AAI Connection Name>.Correlation('1;NA;3;4;5;6.9', ';11;12;;14;', 'pearson')
        :Possible values for the third argument are 'pearson', 'kendall' or 'spearman'
Calculate the correlation coefficient for two columns. Scalar function.
        :param request: an iterable sequence of RowData
        :param context:
        :return: the correlation coefficient for each row
        :Qlik expression examples:
        :<AAI Connection Name>.Pearson('1;NA;3;4;5;6.9', ';11;12;;14;')
        :<AAI Connection Name>.Correlation('1;NA;3;4;5;6.9', ';11;12;;14;', 'pearson')
        :Possible values for the third argument are 'pearson', 'kendall' or 'spearman'
Calculate the correlation coefficient for two columns . Scalar function . : param request : an iterable sequence of RowData : param context : : return : the correlation coefficient for each row : Qlik expression examples : : <AAI Connection Name > . Pearson ( 1 ; NA ; 3 ; 4 ; 5 ; 6 . 9 ; 11 ; 12 ;; 14 ; ) : <AAI Connection Name > . Correlation ( 1 ; NA ; 3 ; 4 ; 5 ; 6 . 9 ; 11 ; 12 ;; 14 ; pearson ) : Possible values for the third argument are pearson kendall or spearman
def _correlation(request, context):
        """
        Calculate the correlation coefficient for two columns. Scalar function.
        :param request: an iterable sequence of RowData
        :param context:
        :return: the correlation coefficient for each row
        :Qlik expression examples:
        :<AAI Connection Name>.Pearson('1;NA;3;4;5;6.9', ';11;12;;14;')
        :<AAI Connection Name>.Correlation('1;NA;3;4;5;6.9', ';11;12;;14;', 'pearson')
        :Possible values for the third argument are 'pearson', 'kendall' or 'spearman'
        """
        # Iterate over bundled rows
        for request_rows in request:
            response_rows = []
            
            # Set to True for additional info in terminal and log file
            debug = False
            
            if debug:
                # Create a log file for the 
                logfile = os.path.join(os.getcwd(), 'logs', 'Correlation Log.txt')
                
                sys.stdout.write("Function Call: {0} \n\n".format(time.ctime(time.time())))
                with open(logfile,'a') as f:
                    f.write("Function Call: {0} \n\n".format(time.ctime(time.time())))
            
            # Iterating over rows
            for row in request_rows.rows:
                # Retrieve the value of the parameters
                # Two or Three columns are sent from the client, hence the length of params will be 2 or 3
                params = [col.strData for col in row.duals]
                
                if debug:
                    sys.stdout.write("\nPARAMETERS:\n\n{0}\n".format("\n\n".join(str(x) for x in params)))
                    with open(logfile,'a') as f:
                        f.write("\nPARAMETERS:\n\n{0}\n".format("\n\n".join(str(x) for x in params)))
                
                # Create lists for the two series
                x = params[0].split(";")
                y = params[1].split(";")

                # Set the correlation type based on the third argument. 
                # Default is Pearson if the arg is missing.
                try:
                    corr_type = params[2].lower()
                except IndexError:
                    corr_type = 'pearson'
                
                if debug:
                    sys.stdout.write("\n\nx ({0:d} data points):\n{1}\n".format(len(x), " ".join(str(v) for v in x)))
                    sys.stdout.write("\ny ({0:d} data points):\n{1}\n".format(len(y), " ".join(str(v) for v in y)))
                    sys.stdout.write("\nCorrelation Type: {0}\n\n".format(corr_type))
                    
                    with open(logfile,'a') as f:
                        f.write("\n\nx ({0:d} data points):\n{1}\n".format(len(x), " ".join(str(v) for v in x)))
                        f.write("\ny ({0:d} data points):\n{1}\n".format(len(y), " ".join(str(v) for v in y)))
                        f.write("\nCorrelation Type: {0}\n\n".format(corr_type))
                
                # Check that the lists are of equal length
                if len(x) == len(y) and len(x) > 0:
                    # Create a Pandas data frame using the lists
                    df = pd.DataFrame({'x': [utils.atof(d) for d in x], \
                                       'y': [utils.atof(d) for d in y]})
                
                    # Calculate the correlation matrix for the two series in the data frame
                    corr_matrix = df.corr(method=corr_type)
                    
                    if debug:
                        sys.stdout.write("\n\nCorrelation Matrix:\n{}\n".format(corr_matrix.to_string()))
                        with open(logfile,'a') as f:
                            f.write("\n\nCorrelation Matrix:\n{}\n".format(corr_matrix.to_string()))
                    
                    # Prepare the result
                    if corr_matrix.size > 1:
                        result = corr_matrix.iloc[0,1]
                    else:
                        result = None
                else:
                    result = None               

                # Create an iterable of Dual with a numerical value
                duals = iter([SSE.Dual(numData=result)])

                # Append the row data constructed to response_rows
                response_rows.append(SSE.Row(duals=duals))

            # Yield Row data as Bundled rows
            yield SSE.BundledRows(rows=response_rows)
def _correlation ( request , context ) : # Iterate over bundled rows for request_rows in request : response_rows = [ ] # Set to True for additional info in terminal and log file debug = False if debug : # Create a log file for the  logfile = os . path . join ( os . getcwd ( ) , 'logs' , 'Correlation Log.txt' ) sys . stdout . write ( "Function Call: {0} \n\n" . format ( time . ctime ( time . time ( ) ) ) ) with open ( logfile , 'a' ) as f : f . write ( "Function Call: {0} \n\n" . format ( time . ctime ( time . time ( ) ) ) ) # Iterating over rows for row in request_rows . rows : # Retrieve the value of the parameters # Two or Three columns are sent from the client, hence the length of params will be 2 or 3 params = [ col . strData for col in row . duals ] if debug : sys . stdout . write ( "\nPARAMETERS:\n\n{0}\n" . format ( "\n\n" . join ( str ( x ) for x in params ) ) ) with open ( logfile , 'a' ) as f : f . write ( "\nPARAMETERS:\n\n{0}\n" . format ( "\n\n" . join ( str ( x ) for x in params ) ) ) # Create lists for the two series x = params [ 0 ] . split ( ";" ) y = params [ 1 ] . split ( ";" ) # Set the correlation type based on the third argument.  # Default is Pearson if the arg is missing. try : corr_type = params [ 2 ] . lower ( ) except IndexError : corr_type = 'pearson' if debug : sys . stdout . write ( "\n\nx ({0:d} data points):\n{1}\n" . format ( len ( x ) , " " . join ( str ( v ) for v in x ) ) ) sys . stdout . write ( "\ny ({0:d} data points):\n{1}\n" . format ( len ( y ) , " " . join ( str ( v ) for v in y ) ) ) sys . stdout . write ( "\nCorrelation Type: {0}\n\n" . format ( corr_type ) ) with open ( logfile , 'a' ) as f : f . write ( "\n\nx ({0:d} data points):\n{1}\n" . format ( len ( x ) , " " . join ( str ( v ) for v in x ) ) ) f . write ( "\ny ({0:d} data points):\n{1}\n" . format ( len ( y ) , " " . join ( str ( v ) for v in y ) ) ) f . write ( "\nCorrelation Type: {0}\n\n" . format ( corr_type ) ) # Check that the lists are of equal length if len ( x ) == len ( y ) and len ( x ) > 0 : # Create a Pandas data frame using the lists df = pd . DataFrame ( { 'x' : [ utils . atof ( d ) for d in x ] , 'y' : [ utils . atof ( d ) for d in y ] } ) # Calculate the correlation matrix for the two series in the data frame corr_matrix = df . corr ( method = corr_type ) if debug : sys . stdout . write ( "\n\nCorrelation Matrix:\n{}\n" . format ( corr_matrix . to_string ( ) ) ) with open ( logfile , 'a' ) as f : f . write ( "\n\nCorrelation Matrix:\n{}\n" . format ( corr_matrix . to_string ( ) ) ) # Prepare the result if corr_matrix . size > 1 : result = corr_matrix . iloc [ 0 , 1 ] else : result = None else : result = None # Create an iterable of Dual with a numerical value duals = iter ( [ SSE . Dual ( numData = result ) ] ) # Append the row data constructed to response_rows response_rows . append ( SSE . Row ( duals = duals ) ) # Yield Row data as Bundled rows yield SSE . BundledRows ( rows = response_rows )