Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add papaya
- Loading branch information
Showing
113 changed files
with
45,395 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
drawing/papaya/examples/BoxPlotExample/BoxPlotExample.pde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
drawing/papaya/examples/CastingFinding_andMore/CastingFinding_andMore.pde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
180
drawing/papaya/examples/ComparingTwoDatasets/ComparingTwoDatasets.pde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
drawing/papaya/examples/ComparingTwoDatasets/data/FRESHMAN15.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.