
name          = "IPSOS BatchFit"
description   = "Reads a IPSOS CSV file, and performs fits on each pair of columns"
version       = "1.2"
author        = "Daniel G. Hyams"


import csv
import numpy

def run():
    # read in the datasets
    input_file = "C:\\Users\\dhyams\\Downloads\\IPSOS-F4.csv"
    output_file = "C:\\Users\\dhyams\\Downloads\\IPSOS-F4-results.csv"
    output_file_eval = "C:\\Users\\dhyams\\Downloads\\IPSOS-F4-results-evaluated.csv"
	
    try:
        dsets = read_ipsos_datasets(input_file)
        
        outfile = open(output_file,"w")
        outfile_eval = open(output_file_eval,"w")
    except:
        import traceback
        api.message(traceback.format_exc(),1)
        return
    
    # now, run CurveFinder on all of them.
    for i,dset in enumerate(dsets):
       api.message('Working on dataset %d'%i)
       if dset.min() == 0.0 and dset.max() == 0.0:
           api.message("***Skipping a marker dataset***")
           outfile.write('DATASET_%d,SKIPPED\n'%i)
       else:
           results = api.curvefinder(data=dset,nonlinreg_builtin=True,nonlinreg_custom=False,linear=True,poly=False,silent=True,do_multicore=True)
           best_result = results[0]
           api.message('Best is %s [%s] %s'%(best_result.name,best_result.equation,str(best_result.param)))
           params = [str(x) for x in best_result.param]
           outfile.write('DATASET_%d,%s,%s,%s\n'%(i,best_result.name,best_result.equation,','.join(params)))
           
           # evaluate the model from xmin to xmax of this particular dataset, 100 points.
           xmin,xmax = dset[:,0].min(),dset[:,0].max()
           xx = numpy.linspace(xmin,xmax,100)
           yy = best_result.eval_function(xx,*best_result.param)
           xlist = [str(x) for x in xx]
           ylist = [str(y) for y in yy]
           outfile_eval.write('DATASET_%d,%s,%s,%s,%s\n'%(i,best_result.name,best_result.equation,','.join(xlist),','.join(ylist)))
           
           api.update_ui()
    outfile.close()
    outfile_eval.close()
    
    
def read_ipsos_datasets(fname):
    #Reads a CSV file, IPSOS formatted, and returns a list of datasets in 
    #that file.
    
    # first, read the file into memory
    thereader = csv.reader(open(fname,'r'))
    alldata = []
    
    for r in thereader: alldata.append(r)
    nsets = len(alldata[0])/2
    nrows = len(alldata)
    api.message("IPSOS reader: %d datasets to read. "%nsets) 
    
    # now, separate the data into datasets.
    datasets = []    
    for id in xrange(nsets):
        # figure out how many data points we have for this particular dataset
        n = 0
        for i in xrange(1,nrows):
            if not alldata[i][id*2+0]:  break
            else:                       n += 1
            
        # create the dataset and save off the labels for it
        dset = numpy.empty((n,2))
        #dset.xlabel,dset.ylabel = alldata[0][id*2 + 0],alldata[0][id*2 + 1]
              
        # record the data for the dataset.
        for i in xrange(0,n):
           dset[i][0],dset[i][1] = float(alldata[i+1][id*2 + 0]),float(alldata[i+1][id*2 + 1])
           
        datasets.append(dset)
    
    # debugging    
    #api.message(str(datasets[2]))  

    return datasets    