######################### filter_by_call_signal.py #####################
# ELEMENTS ALONG EACH ROW ARE SUBJECTED TO A FILTER AND COUNTED.

# For selected columns in the current dataset, the number of elements along
# each row that satisfy a given criterian are counted, and the resulting
# column is appended to the data set. In the case of continuous columns,
# the number of elements greater than a threshold value is counted along 
# each row. In the case of categorical columns, the number of elements
# matching the given string value are counted along each row.

# The user is prompted by the user interface to select the continuous columns by number
# using a column chooser and to provide a threshold value for continuous columns, along with
# a name for the new column to be created.
# The user interface also prompts for selecting categorical columns by name and a
# string for comparison along with a name for the new column to be created.
# The resulting column contaning the number of elements along
# each row that matches with the given number or string (as the case may be) is appended
# to the current data set.

#------------------------------------------------------------------------------------------------
#from script.algorithm import *
from script.view import *
from script.dataset import *
from script.omega import createComponent, showDialog
from java.lang import Float
import string


# Strips the blank spaces around the input string.
def stripString(st):
	return string.strip(str(st))	


# Counting along rows for selected columns	
def countAlongRows(listcont,value,listcat, name, dataset):
        column1=[]
        column2=[]
	for i in range(dataset.getRowCount()):
		num1 = 0
                num2 = 0
		for j in range(listcont.getSize()):
			if dataset[listcont.get(j)][i] >value:
				num1 = num1+1
		for j in range(listcat.getSize()):
			if str(dataset[listcat.get(j)][i])==name:
                                num2 = num2+1
		column1.append(num1)
		column2.append(num2)
	return column1, column2


	
#  Main script start here----


#get current data set
node=script.project.getActiveDatasetNode()
d=node.getDataset()
 
# Creating the panels             
p1 = createComponent(type="columnlist", columnType = "continuous", id="cont", description="Continuous", dataset=d )
p2 = createComponent(type="string", id="contstr", description="Value greater than", value=" ")
p3 = createComponent(type="string", id="contName", description="Column Name", value="")
p4 = createComponent(type="columnlist",columnType="categorical", id="cat", description="Categorical", dataset=d)
p5 = createComponent(type="string", id="catstr", description="Categorical Name", value=" ")
p6 = createComponent(type="string", id="catName", description="Column Name", value="")
panel = createComponent(type="Group", id="both_boxes",description="Filter by Call", components=[p1,p2, p3, p4, p5, p6])                  
result = showDialog(panel)


# getting the strings from dialog boxes and stripping it off the
#   blank spaces (if any) before and after

catName=stripString(p5.getValue())

name1 = stripString(p3.getValue())
if name1=="":
    contColName='Count_Categorical'
else:
    contColName=name1
   
name2 = stripString(p6.getValue())
if name2=="":
    catColName='Count_Continuous'
else:
    catColName=name2



#Takes care of the "cancel" button and exit icon on window.

if result!=None:

# getting the list of selected continuous columns
    liscon = p1.getValue()

# getting the list of selected categorical columns
    liscat = p4.getValue()

#getting the string from dialog box and converting to float numbers.
    try:
	
    	contThreshold = Float(p2.getValue()).floatValue()
    except:
	script.omega.error("Illegal entry: Float value expected for Threshold Value")


#creating the new columns
    continuousColumn,categoricalColumn=countAlongRows(liscon,contThreshold,liscat,catName,d)

# Appending the two columns to the data set
    newcol1=createIntColumn(contColName, continuousColumn)
    newcol2=createIntColumn(catColName, categoricalColumn)
    d.addColumn(newcol1)
    d.addColumn(newcol2)

