##
##
##
##     sarchitect designer script to get 'Rankit plot'
##
##     Shaillay Kumar Dogra
##     05 Oct 2006    
##     editor@qsarworld.com
##
##
##     INPUT: a column labelled as "Standardized Residuals".
##     Script can be modified to change the input column and check 'normality' assumption 
##     for any set of values (in a given column).
##
##     Generates random nos. belonging to Gaussian distribution of mean 0 and variance 1; sorts them.
##     Sorts the input column.
##
##     OUTPUT: scatter plot of 'sorted random nos.' vs. 'sorted input column'.
##
##     Appends 2 columns to dataset - "Sorted Random Values" & "Sorted Standardized Residuals".
##
##
##     References :
##	(1) http://en.wikipedia.org/wiki/Normal_probability_plot
##	(2) http://docs.python.org/lib/module-random.html
##
##


import script
from script.dataset import *
from script.project import *
from script.omega import createComponent, showDialog
from javax.swing import *
from math import *
from script.view import *
import random    ## python 'random' module 


dataset = getActiveDataset()
row_count = dataset.getRowCount()

rand_col = [0] * row_count
for i in range(row_count):
	rand_col[i] = random.gauss(0,1)

rand_col.sort()

addCol=createFloatColumn("Sorted Random Values",rand_col)
dataset.addColumn(addCol)

## CHECK GAUSSIAN-DISTRIBUTION 'QUALITY' OF GENERATED RANDOM NOS.
#rand_idx = dataset.index("Sorted Random Values")
#Histogram(column = rand_idx).show()

residual_col = dataset.index("Standardized Residuals")
sorted_resid = [ ]
for i in dataset[residual_col]: 
	sorted_resid.append(i)

sorted_resid.sort()

addCol=createFloatColumn("Sorted Standardized Residuals",sorted_resid)
dataset.addColumn(addCol)

## SCATTER PLOT
x_col = dataset.index("Sorted Random Values")
y_col = dataset.index("Sorted Standardized Residuals")
ScatterPlot(yaxis = y_col, xaxis = x_col).show()

## REPORT COMPLETION
parent=script.tool.getTool().getFrame()
mesg = "Done With Script Execution."
JOptionPane.showMessageDialog(parent,mesg,"STATUS!",JOptionPane.INFORMATION_MESSAGE)

##
## END
##