void intersect(const Ty* X1, const Ty* X2, Ty* M, int d, int n1, int n2) { // compute the respective sums Ty* hs1 = new Ty[n1]; Ty* hs2 = new Ty[n2]; column_sum(X1, d, n1, hs1); column_sum(X2, d, n2, hs2); // compute metrics const Ty* v2 = X2; for (int j = 0; j < n2; ++j, v2 += d) { const Ty* v1 = X1; for (int i = 0; i < n1; ++i, v1 += d) { Ty s = 0; for (int k = 0; k < d; ++k) { Ty u1 = v1[k]; Ty u2 = v2[k]; s += ((u1 < u2) ? u1 : u2); } Ty smax = ((hs1[i] > hs2[j]) ? hs1[i] : hs2[j]); *M++ = ((smax > 0) ? s / smax : 1); } } // release resources delete[] hs1; delete[] hs2; }
void base_reg::base_score(const mematrix<double>& resid, const int model, const int interaction, const int ngpreds, const masked_matrix& invvarmatrix, const int nullmodel) { mematrix<double> oX = reg_data.extract_genotypes(); mematrix<double> X = apply_model(oX, model, interaction, ngpreds, reg_data.is_interaction_excluded, false, nullmodel); beta.reinit(X.ncol, 1); sebeta.reinit(X.ncol, 1); int length_beta = X.ncol; double N = static_cast<double>(resid.nrow); mematrix<double> tX = transpose(X); if (invvarmatrix.length_of_mask != 0){ tX = tX * invvarmatrix.masked_data; } mematrix<double> u = tX * resid; mematrix<double> v = tX * X; mematrix<double> csum = column_sum(X); csum = transpose(csum) * csum; csum = csum * (1. / N); v = v - csum; // use cholesky to invert LDLT <MatrixXd> Ch = LDLT < MatrixXd > (v.data.selfadjointView<Lower>()); // before was // mematrix<double> v_i = invert(v); beta.data = Ch.solve(v.data.adjoint() * u.data); //TODO(maartenk): set size of v_i directly or remove mematrix class mematrix<double> v_i = v; v_i.data = Ch.solve(MatrixXd(length_beta, length_beta). Identity(length_beta, length_beta)); double sr = 0.; double srr = 0.; for (int i = 0; i < resid.nrow; i++) { sr += resid[i]; srr += resid[i] * resid[i]; } double mean_r = sr / N; double sigma2_internal = (srr - N * mean_r * mean_r) / (N - beta.nrow); for (int i = 0; i < beta.nrow; i++) sebeta[i] = sqrt(v_i.get(i, i) * sigma2_internal); mematrix<double> chi2 = transpose(u) * v_i * u; chi2 = chi2 * (1. / sigma2_internal); chi2_score = chi2[0]; }
void base_reg::base_score(mematrix<double>& resid, regdata& rdata, int verbose, double tol_chol, int model, int interaction, int ngpreds, const masked_matrix& invvarmatrix, int nullmodel) { mematrix<double> oX = rdata.extract_genotypes(); mematrix<double> X = apply_model(oX, model, interaction, ngpreds, rdata.is_interaction_excluded, false, nullmodel); beta.reinit(X.ncol, 1); sebeta.reinit(X.ncol, 1); double N = static_cast<double>(resid.nrow); mematrix<double> tX = transpose(X); if (invvarmatrix.length_of_mask != 0) tX = tX * invvarmatrix.masked_data; mematrix<double> u = tX * resid; mematrix<double> v = tX * X; mematrix<double> csum = column_sum(X); csum = transpose(csum) * csum; csum = csum * (1. / N); v = v - csum; // use cholesky to invert mematrix<double> v_i = v; cholesky2_mm(v_i, tol_chol); chinv2_mm(v_i); // before was // mematrix<double> v_i = invert(v); beta = v_i * u; double sr = 0.; double srr = 0.; for (int i = 0; i < resid.nrow; i++) { sr += resid[i]; srr += resid[i] * resid[i]; } double mean_r = sr / N; double sigma2_internal = (srr - N * mean_r * mean_r) / (N - beta.nrow); for (int i = 0; i < beta.nrow; i++) sebeta[i] = sqrt(v_i.get(i, i) * sigma2_internal); mematrix<double> chi2 = transpose(u) * v_i * u; chi2 = chi2 * (1. / sigma2_internal); chi2_score = chi2[0]; }
/* Calibration run. Aborting allowed at "safe" points where the scanner won't * be left in a crap state. */ int sanei_canon_pp_calibrate(scanner_parameters *sp, char *cal_file) { int count, readnum, colournum, scanlinenum; int outfile; int scanline_size; int scanline_count = 6; /* Don't change this unless you also want to change do_adjust */ const int calibration_reads = 3; unsigned char command_buffer[10]; image_segment image; unsigned char *databuf; char colours[3][6] = {"Red", "Green", "Blue"}; /* Calibration data is monochromatic (greyscale format) */ scanline_size = sp->scanheadwidth * 1.25; /* 620P has to be difficult here... */ if (!(sp->type) ) scanline_count = 8; /* Probably shouldn't have to abort *just* yet, but may as well check */ if (sp->abort_now) return -1; DBG(40, "Calibrating %ix%i pixels calibration image " "(%i bytes each scan).\n", sp->scanheadwidth, scanline_count, scanline_size * scanline_count); /* Allocate memory for calibration data */ sp->blackweight = (unsigned long *) calloc(sizeof(unsigned long), sp->scanheadwidth); sp->redweight = (unsigned long *) calloc(sizeof(unsigned long), sp->scanheadwidth); sp->greenweight = (unsigned long *) calloc(sizeof(unsigned long), sp->scanheadwidth); sp->blueweight = (unsigned long *) calloc(sizeof(unsigned long), sp->scanheadwidth); /* The data buffer needs to hold a number of images (calibration_reads) * per colour, each sp->scanheadwidth x scanline_count */ databuf = malloc(scanline_size * scanline_count * calibration_reads*3); /* And allocate space for converted image data in this image_segment */ image.image_data = malloc(scanline_count * sp->scanheadwidth * 2 * calibration_reads); image.width = sp->scanheadwidth; image.height = scanline_count * calibration_reads; /* Sending the "dark calibration" command */ memcpy(command_buffer, cmd_calblack, 10); /* Which includes the size of data we expect the scanner to return */ command_buffer[7] = ((scanline_size * scanline_count) & 0xff00) >> 8; command_buffer[8] = (scanline_size * scanline_count) & 0xff; DBG(40, "Step 1/3: Calibrating black level...\n"); for (readnum = 0; readnum < calibration_reads; readnum++) { DBG(40, " * Black scan number %d/%d.\n", readnum + 1, calibration_reads); if (sp->abort_now) return -1; if (send_command(sp->port, command_buffer, 10, 100000, 5000000)) { DBG(1, "Error reading black level!\n"); free (image.image_data); free(databuf); return -1; } /* Black reference data */ sanei_canon_pp_read(sp->port, scanline_size * scanline_count, databuf + (readnum * scanline_size * scanline_count)); } /* Convert scanner format to a greyscale 16bpp image */ for (scanlinenum = 0; scanlinenum < scanline_count * calibration_reads; scanlinenum++) { convdata(databuf + (scanlinenum * scanline_size), image.image_data + (scanlinenum * sp->scanheadwidth*2), sp->scanheadwidth, 1); } /* Take column totals */ for (count = 0; count < sp->scanheadwidth; count++) { /* Value is normalised as if we took 6 scanlines, even if we * didn't (620P I'm looking at you!) */ sp->blackweight[count] = (column_sum(&image, count) * 6) / scanline_count >> 6; } /* 620P has to be difficult here... */ if (!(sp->type) ) { scanline_count = 6; image.height = scanline_count * calibration_reads; } DBG(40, "Step 2/3: Gamma tables...\n"); DBG(40, " * Requesting creation of new of gamma tables...\n"); if (sp->abort_now) return -1; if (send_command(sp->port, cmd_cleargamma, 10, 100000, 5000000)) { DBG(1,"Error sending gamma command!\n"); free (image.image_data); free(databuf); return -1; } DBG(20, " * Snoozing for 15 seconds while the scanner calibrates..."); usleep(15000000); DBG(40, "done.\n"); DBG(40, " * Requesting gamma table values..."); if (send_command(sp->port, cmd_readgamma, 10, 100000, 10000000)) { DBG(1,"Error sending gamma table request!\n"); free (image.image_data); free(databuf); return -1; } DBG(40, "done.\n"); DBG(40, " * Reading white-balance/gamma data... "); sanei_canon_pp_read(sp->port, 32, sp->gamma); DBG(40, "done.\n"); if (sp->abort_now) return -1; memcpy(command_buffer, cmd_calcolour, 10); /* Set up returned data size */ command_buffer[7] = ((scanline_size * scanline_count) & 0xff00) >> 8; command_buffer[8] = (scanline_size * scanline_count) & 0xff; DBG(40, "Step 3/3: Calibrating sensors...\n"); /* Now for the RGB high-points */ for (colournum = 1; colournum < 4; colournum++) { /* Set the colour we want to read */ command_buffer[3] = colournum; for (readnum = 0; readnum < 3; readnum++) { DBG(10, " * %s sensors, scan number %d/%d.\n", colours[colournum-1], readnum + 1, calibration_reads); if (sp->abort_now) return -1; if (send_command(sp->port, command_buffer, 10, 100000, 5000000)) { DBG(1,"Error sending scan request!"); free (image.image_data); free(databuf); return -1; } sanei_canon_pp_read(sp->port, scanline_size * scanline_count, databuf + (readnum * scanline_size * scanline_count)); } /* Convert colour data from scanner format to RGB data */ for (scanlinenum = 0; scanlinenum < scanline_count * calibration_reads; scanlinenum++) { convdata(databuf + (scanlinenum * scanline_size), image.image_data + (scanlinenum * sp->scanheadwidth * 2), sp->scanheadwidth, 1); } /* Sum each column of the image and store the results in sp */ for (count = 0; count < sp->scanheadwidth; count++) { if (colournum == 1) sp->redweight[count] = column_sum(&image, count) >> 6; else if (colournum == 2) sp->greenweight[count] = column_sum(&image, count) >> 6; else sp->blueweight[count] = column_sum(&image, count) >> 6; }