Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add papaya
  • Loading branch information
pex13002 committed Dec 2, 2014
1 parent 5416711 commit b26925e
Show file tree
Hide file tree
Showing 113 changed files with 45,395 additions and 0 deletions.
109 changes: 109 additions & 0 deletions drawing/papaya/examples/BoxPlotExample/BoxPlotExample.pde
@@ -0,0 +1,109 @@
/*
Sketch demonstrating how to plot box plots using the BoxPlot class.
Author: Adila Faruk
*/

// import the library
import papaya.*;
//import papaya.BoxPlot;
// --------------------- Sketch-wide variables ----------------------
BoxPlot[] boxPlot = new BoxPlot[2];
PFont titleFont, mediumFont, smallFont;

float[] breaks;
int[] tension;
int minBreaks, maxBreaks;
float[][] fiveNumSummary;

// boxplot labels
String[] xLabels = new String[] {
"L","M","H"
};


// -----------------------------------
void setup() {
size(630, 500);
smooth();
noLoop();

titleFont = createFont("Helvetica", 22, true);
smallFont = createFont("Helvetica", 11, true);
mediumFont = createFont("Helvetica", 14, true);
// Read in the data
// This data set gives the number of warp breaks per loom,
// where a loom corresponds to a fixed length of yarn.
// breaks: number of breaks in wool
// tension: tension (L,M,H).
String lines[] = loadStrings("warpbreaks.txt");

int ln = lines.length-1;
breaks = new float[ln];
tension = new int[ln];

for (int i=0; i<ln; i++) {
String[] pieces = splitTokens(lines[i+1], "\t") ;
breaks[i] =Float.parseFloat( pieces[1]);
if (pieces[3].equals("L")) {
tension[i] = 0;
}
else if (pieces[3].equals("M")) {
tension[i] = 1;
}
else if (pieces[3].equals("H")) {
tension[i] = 2;
}
}

// get the min and max number of breaks, rounded to the nearest 10
minBreaks = floor( min(breaks)/10)*10-5; maxBreaks = ceil( max(breaks)/10)*10+5;
// get the five number summaries for each data set.
// We have 3 data sets for A, and 3 for B
float[][] woolA = new float[3][5];
float[][] woolB = new float[3][5];
for (int i=0; i<3; i++) {
// Get the five number summary for each data set
woolA[i] = Descriptive.tukeyFiveNum(subset(breaks, 9*i, 9));
woolB[i] = Descriptive.tukeyFiveNum(subset(breaks, 9*(i+3), 9));
}
// setup the box plot
for(int i=0; i<2; i++){
// create the boxPlot
boxPlot[i] = new BoxPlot(this, plotLeft + 1.5*i*plotWidth, plotTop, plotWidth, plotHeight);
// adjust the boundaries a little
boxPlot[i].rightBound = 10; boxPlot[i].leftBound = 35;
}
boxPlot[0].setData(woolA, minBreaks, maxBreaks);
boxPlot[1].setData(woolB, minBreaks, maxBreaks);
}

// -----------------------
// drawing
// plot parameters
float plotLeft = 70, plotTop = 120;
float plotWidth = 200, plotHeight = 300;
color boxColor = color(255);//color(40, 100, 200, 100);
String[] AB = {"A", "B"};

void draw() {
background(color(255));
// draw the box plots
for(int i=0; i<2; i++){
boxPlot[i].drawBoxPlot(xLabels,2,boxColor);
fill(100); textAlign(CENTER,TOP);
textFont(smallFont,13);
text("Wool "+AB[i], plotLeft + (.5+1.5*i)*plotWidth, plotTop + plotHeight + boxPlot[i].bottomBound + 10);
}

// labeling ------------------------------------------------
textAlign(LEFT, BOTTOM);
fill(120);
textFont(titleFont);
text("Number of Warp Breaks per Loom of Wool", 40, 50);
float textHeight = 1.4*textAscent();
//fill(40, 100,150);
textFont(smallFont, 13);
textAlign(LEFT, TOP);
text("Wool Types 'A' and 'B', under Low (L), Medium (M), and High (H) Tension", 40, 52);

}
1 change: 1 addition & 0 deletions drawing/papaya/examples/BoxPlotExample/data/warpbreaks.txt
@@ -0,0 +1 @@
sample breaks sample wool tension1 26 A L2 30 A L3 54 A L4 25 A L5 70 A L6 52 A L7 51 A L8 26 A L9 67 A L10 18 A M11 21 A M12 29 A M13 17 A M14 12 A M15 18 A M16 35 A M17 30 A M18 36 A M19 36 A H20 21 A H21 24 A H22 18 A H23 10 A H24 43 A H25 28 A H26 15 A H27 26 A H28 27 B L29 14 B L30 29 B L31 19 B L32 29 B L33 31 B L34 41 B L35 20 B L36 44 B L37 42 B M38 26 B M39 19 B M40 16 B M41 39 B M42 28 B M43 21 B M44 39 B M45 29 B M46 20 B H47 21 B H48 24 B H49 17 B H50 13 B H51 15 B H52 15 B H53 16 B H54 28 B H
Expand Down
@@ -0,0 +1,95 @@
/*
Methods used here:
- Polynomial
- Descriptive.frequencies for getting the distinct values,
- some Mat methods (sum, multiply)
- Find
- Cast
In some cases, the methods showcased here are suboptimal to other existing methods.
but are used anyway for illustrative purposes.
Adila Faruk, 2012.
*/

// import the library
import papaya.*;

// create a few different types of data points
int ln = 50;
// create a y matrix, consisting of 4 rows
float[] y0 = new float[ln];
float[] x = new float[ln];

float[] coeffs = new float[] {
1.2, 0, 5
};
for (int i=0; i<ln; i++) {
x[i] = i+1;
y0[i] = round(random(-10, 10));
}
// y = 1.2x^2 + 5
float[] y1 = Polynomial.polyval(x, new float[] {1.2, 0, 5});

// sum the first and second rows
float[] y2 = Mat.sum(y0, y1);
// multiply x and y0
float[] y3 = Mat.multiply(x, y0);


/*
Get the distinct value in y0 and their corresponding frequencies.
This requires a sorted y0, hence the Mat.copyThenSort call.
Using sort directly will sort the original y0 which we don't want.
After getting the distinct values and their frequencies, we cast them
to float and int arrays. This is mainly to show how to use the Cast class
*/

ArrayList yDistinct = new ArrayList(); ArrayList frequencies = new ArrayList();
// call the function
Descriptive.frequencies(Mat.copyThenSort(y0),yDistinct,frequencies);
/*
Cast yDistinct and frequencies to float and int arrays
float[] yTemp = new float[yDistinct.size()]; yDistinct.toArray(yTemp);
will give you a class cast exception since floats and ints are primitives.
*/

y3 = Cast.arrayListToFloat(yDistinct);
int[] freq = Cast.arrayListToInt(frequencies);
// print results to screen. set this to false here if you don't care to see this.
boolean printToScreen = true;
if(printToScreen){
println("There are "+y3.length+" distinct values in y0. Their values and corresponding frequencies are:");
for(int i=0; i<y3.length; i++){
println("Value: "+ y3[i]+", Frequency: "+freq[i]);
}
}



/*
Find class: returns -1 if the element is not found
*/
// index of the first element in y0 containing 5.
int idxFirst = Find.indexOf(y0,5);
// index of the alst element in y0 containing 5
int idxLast = Find.lastIndexOf(y0,5);
// the number of times 5 is repeated in y0
int numRepeats = Find.numRepeats(y0,5);
// all the indices in y0 containing 5
int[] indices = Find.indicesWith(y0,5);

// print results to screen. set this to false here if you don't care to see this.
printToScreen = true;
if(printToScreen){
println("\n\nFind class examples:\n\tFirst index of y0 containing 5: "+idxFirst);
println("\tLast index of y0 containing 5: "+idxLast);
println("\tNumber of times 5 is repeated: "+numRepeats);
print("\tIndices of y0 containing 5: ");
for(int i=0; i<indices.length-1; i++){
print(indices[i]+", ");
} print(indices[indices.length-1]);
}



180 changes: 180 additions & 0 deletions drawing/papaya/examples/ComparingTwoDatasets/ComparingTwoDatasets.pde
@@ -0,0 +1,180 @@
import papaya.*;

/* Using the Comparisons class for checking the "freshman-15".
Here, we focus on just the MannWhitney method, but the
signTest and TTest can be used as well.
See also the OneWayAnova class for one-way anovas.
*/

// --------------------- Sketch-wide variables ----------------------
PFont titleFont, mediumFont, smallFont;

float[] BMI_April, BMI_Sept, WT_April, WT_Sept;
// create a "Sex" class that takes in the input csv file and organizes the data
// by sex
Sex M = new Sex();
Sex F = new Sex();
float[] pValue = new float[4];

// ------------------------ Initialisation --------------------------

// Initialise the data
void setup()
{
size(800, 600);
smooth();
noLoop();

titleFont = createFont("Helvetica", 20, true);
mediumFont = createFont("Helvetica", 13, true);
smallFont = createFont("Helvetica", 11, true);
textFont(smallFont);

//Read in the data
String lines[] = loadStrings("FRESHMAN15.csv");
int ln = lines.length-1;
// store everything in the M and F classes
for (int i=0; i<ln; i++) {
String[] pieces = splitTokens(lines[i+1], ",") ;
if (pieces[0].equals("M") ) {
M.addElement(pieces);
}
else {
F.addElement(pieces);
}
}

/* Perform the Mann-Whitney test on male and female Weights and BMIs.
before and after */
M.MannWhitneyTest();
F.MannWhitneyTest();
// get the MannWhitney pvalues.
pValue = new float[]{F.Wt_p,M.Wt_p,F.BMI_p,M.BMI_p};


// Print everything out in the console to see what's going on with these here freshmen...
println("Females:\np-value for BMI is "+F.BMI_p+", p-value for Weight is "+F.Wt_p);
println("Males:\np-value for BMI is "+M.BMI_p+", p-value for Weight is "+M.Wt_p);
println("Changes are insignificant...\n\nWhat about the differences between males and females?");

// combine the April and September data since we've already seen that there's minimal difference.
float[] allWt_F = Mat.concat(F.Wt_Sept,F.Wt_Apr); float[] allWt_M = Mat.concat(M.Wt_Sept,M.Wt_Apr);

// compute the Mann-Whitney test statistic and p-valu
float[] allWt = Comparison.mannWhitney(allWt_F,allWt_M);
println("p-value for Weight difference between males and females: "+allWt[1]);

// doing the same thing for BMI is simple enough. First we combine the data
float[] allBMI_F = Mat.concat(F.BMI_Sept,F.BMI_Apr); float[] allBMI_M = Mat.concat(M.BMI_Sept,M.BMI_Apr);
// then we compute the Mann-Whitney test statistic & p-value
float[] allBMI = Comparison.mannWhitney(allBMI_F,allBMI_M);

println("p-value for BMI difference between males and females: "+allBMI[1]);
println("Aha! so, guys are in general heavier, but, since the BMI comparisons show insignificant differences\n"+
"we can attribute the weight differences to guys being generally taller.");
}


//Visuals. How to use the subplot class.
// specify the colors for the different groups.
color fSept = color(50, 80, 200, 100);
color fApr = color(50, 80, 200, 200);
color mSept = color(200, 50, 80, 100);
color mApr = color(200, 50, 80, 200);
String[][] plotTitles = { { "Weight: Females(kg)", "BMI: Females(kg/m^2)" } , { "Weight: Males(kg)", "BMI: Males(kg/m^2)"}};

void draw() {
background(255);
// create the subplot.
SubPlot splot = new SubPlot(this, 110, 100, 600, 400, 2, 2);

for (int i=0; i<2; i++) {

splot.setDataExtremes(0, F.ln+1, 40, 120,i,0);
splot.setDataExtremes(0, F.ln+1, 10, 50,i,1);
for (int j=0; j<2; j++) {
textAlign(LEFT,TOP);
splot.writeTitle(mediumFont,plotTitles[i][j],i,j);
splot.yLabels(4,i,j);
}
}

// x values for the scatter plot with each x value corresponding
// to a subject.
float[] fX = new float[F.ln];
float[] mX = new float[M.ln];
for (int i=0; i<F.ln; i++) {
fX[i] = i+1;
}
for (int i=0; i<M.ln; i++) {
mX[i] = i+1;
}

// draw the scatter plots
// input it (xdata, ydata, color, subplot x number, subplot y number).
splot.drawScatterPlot(fX, F.Wt_Sept, fSept, 0, 0);
splot.drawScatterPlot(fX, F.Wt_Apr, fApr, 0, 0);

splot.drawScatterPlot(mX, M.Wt_Sept, mSept, 1, 0);
splot.drawScatterPlot(mX, M.Wt_Apr, mApr, 1, 0);

splot.drawScatterPlot(fX, F.BMI_Sept, fSept, 0, 1);
splot.drawScatterPlot(fX, F.BMI_Apr, fApr, 0, 1);

splot.drawScatterPlot(mX, M.BMI_Sept, mSept, 1, 1);
splot.drawScatterPlot(mX, M.BMI_Apr, mApr, 1, 1);

// legend
splot.legendHoriz("bottom", new int[]{fSept,fApr,mSept,mApr}, new String[]{"Females, Before","Females, After","Males, Before","Males, After"});
// write the pvalues to the screen as well.
textAlign(LEFT,TOP); textFont(smallFont);
text("pValue: "+ nfc(pValue[0],4),splot.xLefts[0][0]+5,splot.yTops[0][0]+20);
text("pValue: "+ nfc(pValue[1],4),splot.xLefts[1][0]+5,splot.yTops[1][0]+20);
text("pValue: "+ nfc(pValue[2],4),splot.xLefts[0][1]+5,splot.yTops[0][1]+20);
text("pValue: "+ nfc(pValue[3],4),splot.xLefts[1][1]+5,splot.yTops[1][1]+20);

// plot title
textFont(titleFont);
text("Does the Freshman-15 exist?",splot.getLeft() - splot.s[0][0].leftBound,splot.getTop()-50);
}




// convenience class ----------------------------------------------------------------
class Sex {
float[] BMI_Apr = new float[0];
float[] BMI_Sept = new float[0];
float[] Wt_Apr = new float[0];
float[] Wt_Sept= new float[0];
int ln = 0;

// for the MannWhitney test, by Sex
float BMI_p;
float Wt_p;

Sex() {
}

/*
Update the data. This is *not* the best way to do things (that would
be to use an arraylist and then cast that to a float.
But this eliminates a few steps in the code so bear with me :)
*/
void addElement(String[] s) {
Wt_Sept = append(Wt_Sept, Float.parseFloat(s[1]) );
Wt_Apr = append(Wt_Apr, Float.parseFloat(s[2]) );
BMI_Sept = append(BMI_Sept, Float.parseFloat(s[3]) );
BMI_Apr = append(BMI_Apr, Float.parseFloat(s[4]) );
ln = BMI_Apr.length;
}

/** compares the BMIs and Weights before and after */
void MannWhitneyTest() {
// get the pvalue which corresponds to the second value returned
BMI_p = Comparison.mannWhitney(BMI_Apr, BMI_Sept)[1];
// get the pvalue which corresponds to the second value returned
Wt_p = Comparison.mannWhitney(Wt_Apr,Wt_Sept)[1];
}
}

@@ -0,0 +1 @@
SEX,WTSEP,WTAPR,BMISP,BMIAP,,,,,,,,,,,,,,,,M,72,59,22.02,18.14,,,,,,,,,,,,,,,,M,97,86,19.7,17.44,,,,,,,,,,,,,,,,M,74,69,24.09,22.43,,,,,,,,,,,,,,,,M,93,88,26.97,25.57,,,,,,,,,,,,,,,,F,68,64,21.51,20.1,,,,,,,,,,,,,,,,M,59,55,18.69,17.4,,,,,,,,,,,,,,,,F,64,60,24.24,22.88,,,,,,,,,,,,,,,,F,56,53,21.23,20.23,,,,,,,,,,,,,,,,F,70,68,30.26,29.24,,,,,,,,,,,,,,,,F,58,56,21.88,21.02,,,,,,,,,,,,,,,,F,50,47,17.63,16.89,,,,,,,,,,,,,,,,M,71,69,24.57,23.85,,,,,,,,,,,,,,,,M,67,66,20.68,20.15,,,,,,,,,,,,,,,,F,56,55,20.97,20.36,,,,,,,,,,,,,,,,F,70,68,27.3,26.73,,,,,,,,,,,,,,,,F,61,60,23.3,22.88,,,,,,,,,,,,,,,,F,53,52,19.48,19.24,,,,,,,,,,,,,,,,M,92,92,24.74,24.69,,,,,,,,,,,,,,,,F,57,58,20.69,20.79,,,,,,,,,,,,,,,,M,67,67,20.49,20.6,,,,,,,,,,,,,,,,F,58,58,21.09,21.24,,,,,,,,,,,,,,,,F,49,50,18.37,18.53,,,,,,,,,,,,,,,,M,68,68,22.4,22.61,,,,,,,,,,,,,,,,F,69,69,28.17,28.43,,,,,,,,,,,,,,,,M,87,88,23.6,23.81,,,,,,,,,,,,,,,,M,81,82,26.52,26.78,,,,,,,,,,,,,,,,M,60,61,18.89,19.27,,,,,,,,,,,,,,,,F,52,53,19.31,19.75,,,,,,,,,,,,,,,,M,70,71,20.96,21.32,,,,,,,,,,,,,,,,F,63,64,21.78,22.22,,,,,,,,,,,,,,,,F,56,57,19.78,20.23,,,,,,,,,,,,,,,,M,68,69,22.4,22.82,,,,,,,,,,,,,,,,M,68,69,22.76,23.19,,,,,,,,,,,,,,,,F,54,56,20.15,20.69,,,,,,,,,,,,,,,,M,80,82,22.14,22.57,,,,,,,,,,,,,,,,M,64,66,20.27,20.76,,,,,,,,,,,,,,,,F,57,59,22.15,22.93,,,,,,,,,,,,,,,,F,63,65,23.87,24.67,,,,,,,,,,,,,,,,F,54,56,18.61,19.34,,,,,,,,,,,,,,,,F,56,58,21.73,22.58,,,,,,,,,,,,,,,,M,54,56,18.93,19.72,,,,,,,,,,,,,,,,M,73,75,25.88,26.72,,,,,,,,,,,,,,,,M,77,79,28.59,29.53,,,,,,,,,,,,,,,,F,63,66,21.89,22.79,,,,,,,,,,,,,,,,F,51,54,18.31,19.28,,,,,,,,,,,,,,,,F,59,62,19.64,20.63,,,,,,,,,,,,,,,,F,65,68,23.02,24.1,,,,,,,,,,,,,,,,F,53,56,20.63,21.91,,,,,,,,,,,,,,,,F,62,65,22.61,23.81,,,,,,,,,,,,,,,,F,55,58,22.03,23.42,,,,,,,,,,,,,,,,M,74,77,20.31,21.34,,,,,,,,,,,,,,,,M,74,78,20.31,21.36,,,,,,,,,,,,,,,,M,64,68,19.59,20.77,,,,,,,,,,,,,,,,M,64,68,21.05,22.31,,,,,,,,,,,,,,,,F,57,61,23.47,25.11,,,,,,,,,,,,,,,,F,64,68,22.84,24.29,,,,,,,,,,,,,,,,F,60,64,19.5,20.9,,,,,,,,,,,,,,,,M,64,68,18.51,19.83,,,,,,,,,,,,,,,,M,66,71,21.4,22.97,,,,,,,,,,,,,,,,F,52,57,17.72,19.42,,,,,,,,,,,,,,,,M,71,77,22.26,23.87,,,,,,,,,,,,,,,,F,55,60,21.64,23.81,,,,,,,,,,,,,,,,M,65,71,22.51,24.45,,,,,,,,,,,,,,,,M,75,82,23.69,25.8,,,,,,,,,,,,,,,,F,42,49,15.08,17.74,,,,,,,,,,,,,,,,M,74,82,22.64,25.33,,,,,,,,,,,,,,,,M,94,105,36.57,40.86,,,,,,,,,,,,,,,,
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit b26925e

Please sign in to comment.