// // Programmer: Craig Stuart Sapp // Creation Date: Sat Oct 4 05:50:39 PDT 2008 (extended version of scaperank) // Last Modified: Sun Oct 5 16:40:18 PDT 2008 // Last Modified: Mon Sep 10 22:09:53 PDT 2012 copied from correlation.cpp // Filename: ...sig/examples/all/correlation.cpp // Web Address: http://sig.sapp.org/examples/museinfo/correlation/correlation.cpp // Syntax: C++; museinfo // // Description: Generate a comparison between multiple sets of // number sequences from S0 through S4. // #include "Array.h" #include "Options.h" #include "museinfo.h" /* for humdrum file class */ #include #include /* for qsort and bsearch functions */ #include /* for random number seeding */ #include using namespace std; /////////////////////////////////////////////////////////////////////////// class Performance { public: Performance(void); ~Performance(); int spine; // original spine from input int process; // process = 0 means do not process in analysis int random; // random = 1 => random testing sequence int average; // average = 1 => average performance data const char* pid; // performance id string, if given in input const char* name; // performance name Array data; // array of performance data }; Performance::Performance(void) { spine = process = random = average = -1; pid = ""; name = ""; data.setSize(1000); data.setSize(0); data.setGrowth(1000); } Performance::~Performance() { data.setSize(0); pid = ""; name = ""; spine = process = random = average = -1; } class Scape { public: Scape (void); ~Scape (void); void setScapeSize (int aSize); Array > data; }; Scape::Scape(void) { data.setSize(0); } Scape::~Scape() { // do nothing; } void Scape::setScapeSize(int aSize) { //cout << "SCAPE SIZE: " << aSize << endl; data.setSize(aSize-1); data.allowGrowth(0); int i; for (i=0; i& performances, HumdrumFile& infile); void printNumberDecimal (ofstream& outfile, double number, int decimals); void printScore (string& filename, HumdrumFile& infile, Array& performances, Array >& score0data, int decimals); void sortRanks (Array& ranks, Array& values); double pearsonCorrelation (int size, double* a, double* b); void calculateScore0Array (Array >& dataarray, Array& performances); void calculateRankArray (Array >& rankarray, Array >& scorearray); double getMean (int size, double* a); int numbersort (const void* A, const void* B); void getRange (double& minn, double& maxx, Array data); int performerNameQ (const char* string); void printRank (string& filename, HumdrumFile& infile, Array& performances, Array >& rank0data); void calculateAllCorrelations(Array >& correlations, Array& performances); void calculateCorrelationLayer(Scape& scape, Performance& a, Performance& b); void calculateType1Scores (Array >& score1data, Array >& correlations, Array& perfs); void calculateType1ScoreSingle(Array& performancedata, Array >& correlations, int reference, Array& masklayer); int getMaxCorrIndex (Array >& correlations, int reference, int scaperow, int scapecol, Array& masklayer); void calculateScore0Test (Array >& score0data, Array >& correlations); double getCorrelation (int row, int col, int scaperow, int scapecol, Array >& correlations); void findBestMatch (Array >& correlations, Array& masklayer, int& bestmatch, double& bestscore, int reference); void removeBetterHalfFromMaskLayer(Array& masklayer, Array& rank2data); void calculateType3ScoreSingle(Array& score3data, Array& masklayer, Array >& correlations, int reference); void printCorrelationRankings(string& reference, Array >& score0data, Array >& rank0data, Array& performances); // User interface variables: Options options; int score0q = 0; // used with --s0 (correlation score) int rank0q = 0; // used with --r0 (correlation score) int referenceQ = 0; // used with -r option string reference = ""; // used with -r option string filebase = "data"; // used with -b: output file basename int level = 0; /////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { // process the command-line options checkOptions(options, argc, argv); HumdrumFile infile; Array performances; string hum_filename = ""; if (options.getArgCount() <= 0) { hum_filename = ""; infile.read(cin); } else { hum_filename = options.getArg(1); infile.read(hum_filename); } readData(performances, infile); if (level < 0) { // finished calculating everything requested std::cout << "Warning: no calculations done" << std::endl; exit(0); } // calculate score 0 data: ///////////////////////////////////////// Array > score0data; Array > rank0data; string filename; calculateScore0Array(score0data, performances); calculateRankArray(rank0data, score0data); if (referenceQ) { printCorrelationRankings(reference, score0data, rank0data, performances); } else { if (score0q) { filename = filebase; filename += "-s0.txt"; cout << "Storing score-0 data in " << filename << endl; printScore(filename, infile, performances, score0data, 3); } if (rank0q) { filename = filebase; filename += "-r0.txt"; cout << "Storing score-0 ranks in " << filename << endl; printRank(filename, infile, performances, rank0data); } } return 0; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // printCorrelationRankings -- // void printCorrelationRankings(string& reference, Array >& score0data, Array >& rank0data, Array& performances) { // cout << "PERFORMER = " << reference << endl; int i; int index = -1; for (i=0; i >& correlations, Array& masklayer, int& bestmatch, double& bestscore, int reference) { Array score1data; score1data.setSize(masklayer.getSize()); score1data.allowGrowth(0); score1data.setAll(-1000); calculateType1ScoreSingle(score1data, correlations, reference, masklayer); bestmatch = -1000; bestscore = -1000.0; int i; for (i=1; i >& score0data, Array >& correlations) { int i, j; for (i=0; i >& correlations) { int a; int b; if (row < col) { a = col; b = row; } else if (row > col) { a = row; b = col; } else { return 1.0; } //cout << "correlation[" << row << "][" << col << "][" << scaperow // << "][" << scapecol << "] = " // << correlations[a-1][b].data[scaperow][scapecol] // << endl; return correlations[a-1][b].data[scaperow][scapecol]; } ////////////////////////////// // // calculateType1Scores -- calculate the index of the best correlations // for each subsequence. // void calculateType1Scores(Array >& score1data, Array >& correlations, Array& perfs) { Array > masklayer; int arraysize = score1data.getSize(); int i; masklayer.setSize(arraysize); masklayer.allowGrowth(0); for (i=0; i& performancedata, Array >& correlations, int reference, Array& masklayer) { Array sums; sums.setSize(performancedata.getSize()); sums.allowGrowth(0); sums.setAll(0); int i, j; int maxindex; int tsum = 0; int scaperows = correlations[0][0].data.getSize(); int scapecols; for (i=0; i >& correlations, int reference, int scaperow, int scapecol, Array& masklayer) { int maxindex = -2; double maxval = -2.0; double testval; int i; int rowsize = correlations.getSize()+1; for (i=0; i >& correlations, Array& performances) { int i, j; // subtract one for top row since diagonal always one correlations.setSize(performances.getSize()-1); for (i=1; i& performances, HumdrumFile& infile) { int dataspines = infile.getMaxTracks(); performances.setSize(dataspines); performances.allowGrowth(0); // pre-allocate storage space for data int i, j; for (i=0; i= 0.0) && (maxx >= 0.0) && (maxx <= 1.0)) { performances[i].random = 1; } } } ////////////////////////////// // // performerNameQ -- // int performerNameQ(const char* string) { int digitcount = 0; int spacecount = 0; int coloncount = 0; int equalscount = 0; int len = strlen(string); int i; for (i=0; i data) { int i; minn = data[0]; maxx = data[0]; for (i=1; i data[i]) { minn = data[i]; } if (maxx < data[i]) { maxx = data[i]; } } } ////////////////////////////// // // calculateScore0Array -- // void calculateScore0Array(Array >& dataarray, Array& performances) { dataarray.setSize(performances.getSize()); int i, j; for (i=0; i >& rankarray, Array >& scorearray) { rankarray.setSize(scorearray.getSize()); int i; for (i=0; i& ranks, Array& values) { int i; Array presort; Array sortdata; presort.setSize(values.getSize()); sortdata.setSize(values.getSize()); for (i=0; i& performances, Array >& rank0data) { int i, j; ofstream outfile; outfile.open(filename); if (!outfile.is_open()) { std::cout << "Error: could not open " << filename << " for writing" << std::endl; exit(1); } // print bibliographic information: for (i=0; i& performances, Array >& score0data, int decimals) { int i, j; ofstream outfile; #ifndef OLDCPP outfile.open(filename); #else outfile.open(filename, ios::noreplace); #endif if (!outfile.is_open()) { std::cout << "Error: could not open " << filename << " for writing" << std::endl; exit(1); } // print bibliographic information: for (i=0; i decimals) { std::cout << "Error printing number " << number << std::endl; std::cout << "deccount = " << deccount << std::endl; std::cout << "decimals = " << decimals << std::endl; std::cout << "string = >>" << string << "<<" << std::endl; exit(1); } outfile << string; if (decarea == 0) { outfile << '.'; } for (i=0; i valueb) { return -1; } else if (valuea < valueb) { return +1; } else { return 0; } } ////////////////////////////// // // checkOptions -- // void checkOptions(Options& opts, int argc, char* argv[]) { opts.define("s0=b", "create score-0 data file"); opts.define("r0=b", "create rank-0 data file"); opts.define("r|reference=s:", "reference performance"); opts.define("0=b", "create rank-0 and score-0 data file"); opts.define("a|all=b", "create all score and rank files"); opts.define("b|base|filebase=s:data", "basename for generating filenames"); opts.define("author=b", "author of program"); opts.define("version=b", "compilation info"); opts.define("example=b", "example usages"); opts.define("help=b", "short description"); opts.process(argc, argv); // handle basic options: if (opts.getBoolean("author")) { cout << "Written by Craig Stuart Sapp, " << "craig@ccrma.stanford.edu, October 2008" << std::endl; exit(0); } else if (opts.getBoolean("version")) { cout << argv[0] << ", version: October 2008" << std::endl; cout << "compiled: " << __DATE__ << std::endl; exit(0); } else if (opts.getBoolean("help")) { usage(opts.getCommand().c_str()); exit(0); } else if (opts.getBoolean("example")) { example(); exit(0); } if (opts.getBoolean("s0")) { score0q = 1; } if (opts.getBoolean("r0")) { rank0q = 1; } if (opts.getBoolean("0")) { score0q = rank0q = 1; } referenceQ = opts.getBoolean("reference"); reference = opts.getString("reference"); if (referenceQ) { score0q = rank0q = 1; } if (opts.getBoolean("all")) { score0q = rank0q = 1; level = 0; } filebase = opts.getString("filebase"); } ////////////////////////////// // // example -- // void example(void) { } ////////////////////////////// // // usage -- // void usage(const char* command) { } // md5sum: 27d65c83753e00ae8eba55569913bca2 correlation.cpp [20160320]