int get_median(int arr1[], int arr2[], int n) { if (n == 1) return (arr1[0]+arr2[0])>>1; if (n == 2) return (max(arr1[0], arr2[0]) + min(arr1[1], arr2[1]))>>1; int mid1 = median(arr1, n); int mid2 = median(arr2, n); // printf("%d %d\n", mid1, mid2); if (mid1 == mid2) return mid1; if (mid1 > mid2) { if (n%2) //odd return get_median(&arr1[0], &arr2[n/2+1], n/2); else return get_median(&arr1[0], &arr2[n/2], n/2); } else { if (n%2) //odd return get_median(&arr1[n/2+1], &arr2[0], n/2); else //even return get_median(&arr1[n/2], &arr2[0], n/2); } }
/** Finds which channels need to be sorted first and preproceses achv for fast sort */ static double prepare_sort(struct box *b, hist_item achv[]) { /* ** Sort dimensions by their variance, and then sort colors first by dimension with highest variance */ channelvariance channels[4] = { {index_of_channel(r), b->variance.r}, {index_of_channel(g), b->variance.g}, {index_of_channel(b), b->variance.b}, {index_of_channel(a), b->variance.a}, }; qsort(channels, 4, sizeof(channels[0]), comparevariance); for(unsigned int i=0; i < b->colors; i++) { const float *chans = (const float *)&achv[b->ind + i].acolor; // Only the first channel really matters. When trying median cut many times // with different histogram weights, I don't want sort randomness to influence outcome. achv[b->ind + i].sort_value = ((unsigned int)(chans[channels[0].chan]*65535.0)<<16) | (unsigned int)((chans[channels[2].chan] + chans[channels[1].chan]/2.0 + chans[channels[3].chan]/4.0)*65535.0); } const f_pixel median = get_median(b, achv); // box will be split to make color_weight of each side even const unsigned int ind = b->ind, end = ind+b->colors; double totalvar = 0; for(unsigned int j=ind; j < end; j++) totalvar += (achv[j].color_weight = color_weight(median, achv[j])); return totalvar / 2.0; }
struct node* mergesort_sll(struct node* start, struct node* end) { if (start == NULL ) { return NULL; } if (start == end || start->next == NULL) { return start; } if (end && start->next == end) { if (end->value < start->value){ int temp = start->value; start->value = end->value; end->value = temp; } end->next = NULL; return start; } struct node* median = get_median(start); if(median == NULL || median->next == NULL) { return NULL; } struct node* after_median = median->next; median->next = NULL; end->next = NULL; struct node* a = mergesort_sll(start, median); struct node* b = mergesort_sll(after_median, end); return merge(a,b); }
float test_min_max_enhancement_old(int y,int x,int kernel_radius,IplImage * image){ vector<int> kernel_values; for(int i=y-kernel_radius;i<=y+kernel_radius;i++){ for(int j=x-kernel_radius;j<=x+kernel_radius;j++){ if(is_coordinate_inside_boundaries(i,j,image)){ int value = get_pixel(image,i,j); kernel_values.push_back(value); } } } float median = get_median(kernel_values); int max_val = get_max_value(kernel_values); int min_val = get_min_value(kernel_values); int e = .001; //float new_value = (float)(max_val - min_val)/(max_val + min_val + e); //float new_value = (float)(max_val - min_val)/(median + e); //float new_value = (float)(max_val - min_val)/(max_val + e);//seems better float new_value = (float)(max_val - min_val)/(min_val + e); //float new_value = (float)(max_val - min_val)/((max_val + min_val)/2 + e); //int rvalue = (int)ceil(new_value); //return rvalue ; return new_value; }
void equi_width_histo::print() const { // print min_histogram std::cout.precision(3); if (m_histo_min.size() > 0){ for (unsigned int i = m_histo_min.size(); i > 0; i--){ std::cout << "[" << m_min - i * m_bucket_size << "\t" << m_min - (i-1) * m_bucket_size << "] : " << m_histo_min[i-1] << "\n"; } } // print original histogram for (int i = 0; i < m_nmb_buckets; i++){ std::cout << "[" << m_min + i * m_bucket_size << "\t" << m_min + (i+1) * m_bucket_size << "] : " << m_histo[i] << "\n"; } // print min_histogram if (m_histo_max.size() > 0){ for (unsigned int i = 0; i < m_histo_max.size(); i++){ std::cout << "[" << m_max + i * m_bucket_size << "\t" << m_max + (i+1) * m_bucket_size << "] : " << m_histo[i] << "\n"; } } std::cout << std::endl; std::cout << "Count: " << " " << get_count() << std::endl; std::cout << "Median: " << " " << get_median() << std::endl; std::cout << "Skew: " << " " << get_skew() << std::endl; }
// computing the jumping position from Reads[current] to Reads[next], find out that Reads[current] pos x (=jump_from) aligns to Reads[next] pos y(=jump_to), x > prev_jump in Reads[current] int get_jumping_index(int current, int next, struct read_state * Reads, struct kmer_state * kmer, int *jump_from, int *jump_to, int prev_jump){ int j1,j; int array_median[50000]; int array_count=0; int dis_median; int temp_kmer_index; int prev_pos = -100; int diff = 100000; for (j = 0; j < Reads[current].overlap_read_count ; j++){ if (Reads[current].overlap_read[j] == next){ diff = (Reads[current].overlap_start_pos1[j] - Reads[current].overlap_start_pos2[j] + Reads[current].overlap_end_pos1[j] - Reads[current].overlap_end_pos2[j])/2; } } if (diff == 100000){ for (j = 0; j < Reads[next].overlap_read_count ; j++){ if (Reads[next].overlap_read[j] == current){ diff = -(Reads[next].overlap_start_pos1[j] - Reads[next].overlap_start_pos2[j] + Reads[next].overlap_end_pos1[j] - Reads[next].overlap_end_pos2[j])/2; } } } for (j = 0; j < Reads[current].kmer_count ; j++){ temp_kmer_index = Reads[current].kmer_index[j]; if (kmer[temp_kmer_index].true_cov > 0){ for (j1 = 0; j1 < kmer[temp_kmer_index].true_cov ; j1++){ if (kmer[temp_kmer_index].reads[j1] == next){ if (Reads[current].kmer_pos[j] > prev_jump){ if (((Reads[current].kmer_pos[j] - kmer[temp_kmer_index].reads_pos[j1] > diff-Jump/2)&&(Reads[current].kmer_pos[j] - kmer[temp_kmer_index].reads_pos[j1] < diff+Jump/2))||(diff == 100000)){ array_median[array_count] = Reads[current].kmer_pos[j] - kmer[temp_kmer_index].reads_pos[j1]; array_count++; prev_pos = Reads[current].kmer_pos[j]; } } printf("LINK [%d] [%d]: %d-%d=%d (%d) \n", current,next, Reads[current].kmer_pos[j], kmer[temp_kmer_index].reads_pos[j1], Reads[current].kmer_pos[j]- kmer[temp_kmer_index].reads_pos[j1], diff); } } } } dis_median = get_median(array_median, array_count); for (j = 0; j < Reads[current].kmer_count ; j++){ temp_kmer_index = Reads[current].kmer_index[j]; if (kmer[temp_kmer_index].true_cov > 0){ for (j1 = 0; j1 < kmer[temp_kmer_index].true_cov ; j1++){ if (kmer[temp_kmer_index].reads[j1] == next){ if ((Reads[current].kmer_pos[j] > prev_jump) ){ if (abs_dis(dis_median, Reads[current].kmer_pos[j] - kmer[temp_kmer_index].reads_pos[j1]) < Jump/4){ (*jump_from) = Reads[current].kmer_pos[j]; (*jump_to) = kmer[temp_kmer_index].reads_pos[j1]; printf("MEDIAN [%d] [%d]: %d %d\n", current,next,(*jump_from), (*jump_to) ); return(0); } } } } } } }
inline double get_mad(const std::map<int, size_t> &hist, double median) { // median absolute deviation std::map<int, size_t> hist2; for (auto iter = hist.begin(); iter != hist.end(); ++iter) { int x = abs(iter->first - math::round_to_zero(median)); hist2[x] = iter->second; } return get_median(hist2); }
/********************************************//** * \brief Apply a median filter on an image * * \param img_in t_img* - input image * \param filtersize CPU_INT16S - filter size * \param img_out t_img* - output image * \return CPU_CHAR - status of operation * * Creating a matrix is not necessary prior to use this filter ***********************************************/ CPU_CHAR apply_median_filter(t_img * img_in,CPU_INT16S filtersize,t_img * img_out) { CPU_CHAR ret = ERR_NONE; CPU_INT16S i,i_img,j_img; CPU_INT16S filter_range =0; if(filtersize%2 != 1) { filtersize ++; } filter_range = (filtersize -1)/2; //Write Header for(i=0;i<img_in->FileHeader_size;i++) { img_out->FileHeader[i] = img_in->FileHeader[i]; } img_out->signature = img_in->signature; img_out->depth = img_in->depth; img_out->wi = img_in->wi; img_out->he = img_in->he; img_out->FileHeader_size = img_in->FileHeader_size; for(i_img=0;i_img< (img_in->he ) ;i_img++) { for(j_img=0 ; (j_img< img_in->wi );j_img++) { if( ((i_img-filter_range )>=0) && ((j_img-filter_range )>=0) && ((i_img-filter_range )<img_in->he) && ((j_img-filter_range )<img_in->wi ) ) { img_out->Red[i_img][j_img] = get_median(img_in->Red,filter_range,i_img,j_img); img_out->Green[i_img][j_img]= get_median(img_in->Green,filter_range,i_img,j_img); img_out->Blue[i_img][j_img] = get_median(img_in->Blue,filter_range,i_img,j_img); } } } return ret; }
int read_hdrfile(char *infile) { SEASAT_header_ext *hdr; FILE *fp = fopen(infile,"r"); int val, i, end_line; double dtmp, t, t1; int clock_drift_hist[MAX_CLOCK_DRIFT]; int clock_drift_median; double clock_shift; if (fp==NULL) {printf("ERROR: can't open %s header file\n",infile); return(1); } hdr = (SEASAT_header_ext *) malloc(sizeof(SEASAT_header_ext)); for (i=0; i<start_line; i++) { val = get_values(fp, hdr); if (val!=20) {printf("ERROR: unable to read to specified start line in header file\n"); exit(1);} } station_code = hdr->station_code; start_year = 1970 + hdr->lsd_year; start_date = hdr->day_of_year; start_sec = (double) hdr->msec / 1000.0; s_date.year = 1970 + hdr->lsd_year; s_date.jd = hdr->day_of_year; dtmp = (double) hdr->msec / 1000.0; date_sec2hms(dtmp,&s_time); if (USE_CLOCK_DRIFT==1) { for (i=0; i<MAX_CLOCK_DRIFT; i++) clock_drift_hist[i] = 0; end_line = start_line + npatches*patch_size; for (i = start_line; i<end_line; i++) { val = get_values(fp, hdr); if (val!=20) {printf("ERROR: unable to read to specified end line in header file\n"); exit(1);} clock_drift_hist[hdr->clock_drift]++; } printf("APPLYING CLOCK DRIFT TO IMAGE TIMING.\n"); clock_drift_median = get_median(clock_drift_hist,MAX_CLOCK_DRIFT); printf("\tclock_drift_median = %li \n",clock_drift_median); clock_shift = (double) clock_drift_median / 1000.0; start_sec += clock_shift; if (start_sec > 86400.0) {start_sec -= 86400.0; start_date += 1;} dtmp = date_hms2sec(&s_time)+clock_shift; if (dtmp > 86400.0) { s_date.jd+=1; dtmp-=86400.0;} date_sec2hms(dtmp,&s_time); } printf("Found start time: %i %i %lf\n",start_year, start_date, start_sec); fclose(fp); }
int update_stats(struct B * b, struct BenchmarkResult * result) { result->ns_mean = get_mean(&b->samples[0], b->n); result->ns_median = get_median(&b->samples[0], b->n); result->s_mean = 0; result->s_median = 0; return B_SUCCESS; }
double Shrinkage::get_lambda_var() { generate_wk(data,0); double sum_up=0,sum_d=0; double median=get_median(get_diag(S_MLE)); for (int i=0;i<P;i++){ sum_up+=w_var(wk,i,i); sum_d+=(S_MLE.get(i,i)-median)*(S_MLE.get(i,i)-median); } return round_lambda(sum_up/sum_d); }
int get_median_on_table(void * db, char * table_name, char * data) { destroy_list_nbrs(MATH_LIST); MATH_LIST = create_list_nbrs(); search_DB(db, table_name, data, math_callback); int median = get_median(MATH_LIST); return median; }
int get_median_partition(t_list *lst, int size_partition) { int min; int max; int *t; int size; int median; size = build_table(lst, &t); //penser au pattern singleton hidden_quicksort(t, 0, size - 1); get_max_partition(lst, size_partition, &max); get_min_partition(lst, size_partition, &min); median = get_median(t, get_index_array(t, min, size), get_index_array(t, max, size)); return (median); }
int Scanner::get_free_way() { int max_med=0; int median; int direction = 180; for(int i=START;i<END;i = i + STEP) { median = get_median(i); if (median > max_med) { max_med = median; direction = i; } } return direction; }
static void print_report(struct report_options * options, unsigned int iters, cycles_t *tstamp,int size, int no_cpu_freq_fail) { double cycles_to_units; cycles_t median; unsigned int i; const char* units; cycles_t *delta = malloc((iters - 1) * sizeof *delta); if (!delta) { perror("malloc"); return; } for (i = 0; i < iters - 1; ++i) delta[i] = tstamp[i + 1] - tstamp[i]; if (options->cycles) { cycles_to_units = 1; units = "cycles"; } else { cycles_to_units = get_cpu_mhz(no_cpu_freq_fail); units = "usec"; } if (options->unsorted) { printf("#, %s\n", units); for (i = 0; i < iters - 1; ++i) printf("%d, %g\n", i + 1, delta[i] / cycles_to_units / 2); } qsort(delta, iters - 1, sizeof *delta, cycles_compare); if (options->histogram) { printf("#, %s\n", units); for (i = 0; i < iters - 1; ++i) printf("%d, %g\n", i + 1, delta[i] / cycles_to_units / 2); } median = get_median(iters - 1, delta); printf("%7d %d %7.2f %7.2f %7.2f\n", size,iters,delta[0] / cycles_to_units / 2, delta[iters - 2] / cycles_to_units / 2,median / cycles_to_units / 2); free(delta); }
static void print_report(struct perftest_parameters *user_param) { double cycles_to_units; cycles_t median; unsigned int i; const char* units; cycles_t *delta = malloc((user_param->iters - 1) * sizeof *delta); if (!delta) { perror("malloc"); return; } for (i = 0; i < user_param->iters - 1; ++i) delta[i] = tstamp[i + 1] - tstamp[i]; if (user_param->r_flag->cycles) { cycles_to_units = 1; units = "cycles"; } else { cycles_to_units = get_cpu_mhz(user_param->cpu_freq_f); units = "usec"; } if (user_param->r_flag->unsorted) { printf("#, %s\n", units); for (i = 0; i < user_param->iters - 1; ++i) printf("%d, %g\n", i + 1, delta[i] / cycles_to_units ); } qsort(delta, user_param->iters - 1, sizeof *delta, cycles_compare); if (user_param->r_flag->histogram) { printf("#, %s\n", units); for (i = 0; i < user_param->iters - 1; ++i) printf("%d, %g\n", i + 1, delta[i] / cycles_to_units ); } median = get_median(user_param->iters - 1, delta); printf(REPORT_FMT_LAT,(unsigned long)user_param->size,user_param->iters,delta[0] / cycles_to_units , delta[user_param->iters - 2] / cycles_to_units ,median / cycles_to_units ); free(delta); }
int main() { int arr1[] = {9, 11, 13, 20, 22, 40}; int arr2[] = {13, 15, 20, 21, 23, 50}; int len1 = sizeof(arr1)/sizeof(int); int len2 = sizeof(arr2)/sizeof(int); if (len1 == len2) { printf("the median of arr1 & arr2 is %d\n", get_median(arr1, arr2, len1)); } else { printf("the length of two arrays is not the same!\n"); } return 0; }
void introsort_loop(Iter begin, Iter end, int depth_limit, Compare comp) { typedef typename std::iterator_traits<Iter>::difference_type diff_t; while (depth_limit) { diff_t size = end - begin; if (size <= INSERTION_SORT_THRESHOLD) return; get_median(begin, begin + 1, begin + size / 2, end - 1, comp); Iter pivot_pos = unguarded_partition(begin, end, comp); --depth_limit; introsort_loop(pivot_pos + 1, end, depth_limit, comp); end = pivot_pos; } // we have reached the depth limit, we've likely hit a worst case // use heapsort on what's left std::make_heap(begin, end); std::sort_heap(begin, end); }
int main(int argc, char** argv) { struct node* node = init_node(9); struct node *root = insert_front(node, 2); root = insert_front(root,3); root = insert_front(root, 4); root = insert_front(root, 5); //struct node* root2 = init_node(7); // root2 = insert_front(root2, 6); root = mergesort_sll(root, node); struct node* curr = root; while(curr) { printf("%d\n", curr->value); curr = curr->next; } struct node* median = get_median(root); printf("\n\n%d\n", median->value); }
void testcases() { struct node *res; char *ip; int i,check; float res1; for(i=0;i<10;i++) { res=NULL; ip=NULL; ip=malloc_str(testDB[i].input); ip=valid_str(ip); if(ip!=NULL) { ip=rem_space(ip); res=create_list(ip); res=mergesort(res); res1=get_median(res); } else { res1=0; } check=val_cmp(res1,testDB[i].value); //printf("%f\t%f\n",res1,testDB[i].value); if(check==0) printf("passed\n"); else printf("failed\n"); free(ip); free(res); } }
int quick_sort(int array[], int length) { if (length > 1) { int i = 0, j = length - 2; int median = get_median(array, length); print_array(array, length); while (1) { while (array[++i] < median) { } while (array[--j] > median) { } if (i < j) { swap(array, i, j); } else { break; } } swap(array, i, length - 1); quick_sort(array, i); quick_sort(array + i + 1, length - i - 1); } return 0; }
void print_results(struct root_struct *ptr) { int x, hashes, gt100, bucket_reuse, bmax, bmin, average_count; float std_dev; unsigned int lasthash; DEBUG("Print results"); hashes = gt100 = bucket_reuse = bmax = 0; bmin = 999999; if(ptr == NULL) { printf("No packets!\n"); return; } average_count = average(ptr); median = get_median(ptr); /* if(!(flags & (SEQ_NUMS | IPIDS | SRC_PORTS | DST_PORTS | SRC_HOSTS))) { average_count = (average_count + 2) * 100; } if(flags & (SRC_HOSTS | SRC_PORTS | DST_PORTS)) { average_count = (average_count + 2) * 50; } if(flags & SEQ_NUMS) { average_count = (average_count * 1000); } if(flags & IPIDS) { average_count = average_count * 2; } */ std_dev=standard_deviation(root); lasthash = ptr -> hash_value; hashes ++; while(ptr) { bucket_reuse++; if(lasthash != ptr -> hash_value) { hashes ++; lasthash = ptr -> hash_value; if(bmax < bucket_reuse) bmax = bucket_reuse; if(bmin > bucket_reuse) bmin = bucket_reuse; bucket_reuse = 0; } // if(abs(ptr -> count - median) > ((std_dev * anomalosity) )) { gt100 ++; if(!(flags & (SEQ_NUMS | IPIDS | SRC_PORTS | DST_PORTS | SRC_HOSTS | CHECKSUM))) { if(ptr->count > anomalosity){ printf("+-------------------------------------------"\ "--------------------------------------------+\n"); for(x=0;x!=significant_bytes;x++) { printf("%s%x ", (ptr->key[x] < 16 ? "0" : ""), (unsigned char)(ptr->key[x])); } printf("\n"); for(x=0;x!=significant_bytes;x++) { printf("%c ",(isprint(ptr->key[x])? ptr->key[x] : '.')); } printf(" -#: %d\n", ptr->count); } } else { printf("%d ", ptr->count); } if((flags & DST_PORTS) | (flags & SRC_PORTS) | (flags &IPIDS) | (flags & CHECKSUM)) { unsigned short int port; port = ntohs(((unsigned short int *)ptr->key)[0]); printf("%u\n",port); } if(flags & SEQ_NUMS) { unsigned long int seq; seq = ntohl(((unsigned long int *)ptr->key)[0]); printf("%u\n",seq); } if(flags & SRC_HOSTS) { unsigned char octet; int x; for (x=0; x!= 4; x++) { octet = *((unsigned char *)(ptr->key)+x); printf("%u%c", octet, (x == 3 ? ' ' : '.')); } printf("\n"); } } ptr = ptr->next_root; } if(!(flags & QUIET)) { printf("Hashes: %d\nHits: %d\nMisses: %d\nSignatures: "\ "%d\nHits > 100: %d\nPackets: %u\n"\ "Efficiency: Max Reuse: %d Min Reuse: %d\n", hashes, hash_hits, hash_misses, sigs, gt100, packets, bmax, bmin); printf("Standard Deviation: %f Average: %f Median: %f\n",standard_deviation(root),average(root),get_median(root)); } }
int main(void) { printf("The result is %d\n", get_median(x, y, len(x))); return 0; }
nemo_main() { int i, j, k, ki; real x, y, z, xmin, xmax, mean, sigma, skew, kurt, bad, w, *data; real dmin, dmax; real sum, sov, q1, q2, q3; Moment m; bool Qmin, Qmax, Qbad, Qw, Qmedian, Qrobust, Qtorben, Qmmcount = getbparam("mmcount"); bool Qx, Qy, Qz, Qone, Qall, Qign = getbparam("ignore"); bool Qhalf = getbparam("half"); bool Qmaxpos = getbparam("maxpos"); real nu, nppb = getdparam("nppb"); int npar = getiparam("npar"); int ngood; int ndat = 0; int nplanes; int min_count, max_count; int maxmom = getiparam("maxmom"); int maxpos[2]; char slabel[32]; instr = stropen (getparam("in"), "r"); read_image (instr,&iptr); strclose(instr); nx = Nx(iptr); ny = Ny(iptr); nz = Nz(iptr); dprintf(1,"# data order debug: %f %f\n",Frame(iptr)[0], Frame(iptr)[1]); if (hasvalue("tab")) tabstr = stropen(getparam("tab"),"w"); planes = (int *) allocate((nz+1)*sizeof(int)); nplanes = nemoinpi(getparam("planes"),planes,nz+1); Qall = (planes[0]==-1); if (planes[0]==0) { Qone = FALSE; nplanes = nz; for (k=0; k<nz; k++) planes[k] = k; } else if (!Qall) { Qone = (!Qall && nplanes==1); for (k=0; k<nplanes; k++) { if (planes[k]<1 || planes[k]>nz) error("%d is an illegal plane [1..%d]",planes[k],nz); planes[k]--; } } if (hasvalue("win")) { instr = stropen (getparam("win"), "r"); read_image (instr,&wptr); strclose(instr); if (Nx(iptr) != Nx(wptr)) error("X sizes of in=/win= don't match"); if (Ny(iptr) != Ny(wptr)) error("Y sizes of in=/win= don't match"); if (Nz(iptr) != Nz(wptr)) error("Z sizes of in=/win= don't match"); Qw = TRUE; } else Qw = FALSE; Qmin = hasvalue("min"); if (Qmin) xmin = getdparam("min"); Qmax = hasvalue("max"); if (Qmax) xmax = getdparam("max"); Qbad = hasvalue("bad"); if (Qbad) bad = getdparam("bad"); Qmedian = getbparam("median"); Qrobust = getbparam("robust"); Qtorben = getbparam("torben"); if (Qtorben) Qmedian = TRUE; if (Qmedian || Qrobust || Qtorben) { ndat = nx*ny*nz; data = (real *) allocate(ndat*sizeof(real)); } sov = 1.0; /* volume of a pixel/voxel */ Qx = Qign && Nx(iptr)==1; Qy = Qign && Ny(iptr)==1; Qz = Qign && Nz(iptr)==1; sov *= Qx ? 1.0 : Dx(iptr); sov *= Qy ? 1.0 : Dy(iptr); sov *= Qz ? 1.0 : Dz(iptr); strcpy(slabel,"*"); if (!Qx) strcat(slabel,"Dx*"); if (!Qy) strcat(slabel,"Dy*"); if (!Qz) strcat(slabel,"Dz*"); if (maxmom < 0) { warning("No work done, maxmom<0"); stop(0); } if (Qall) { /* treat cube as one data block */ ini_moment(&m,maxmom,ndat); ngood = 0; for (k=0; k<nz; k++) { for (j=0; j<ny; j++) { for (i=0; i<nx; i++) { x = CubeValue(iptr,i,j,k); if (Qhalf && x>=0.0) continue; if (Qmin && x<xmin) continue; if (Qmax && x>xmax) continue; if (Qbad && x==bad) continue; w = Qw ? CubeValue(wptr,i,j,k) : 1.0; accum_moment(&m,x,w); if (Qhalf && x<0) accum_moment(&m,-x,w); if (Qmedian) data[ngood++] = x; if (tabstr) fprintf(tabstr,"%g\n",x); } } } if (npar > 0) { nu = n_moment(&m)/nppb - npar; if (nu < 1) error("%g: No degrees of freedom",nu); printf("chi2= %g\n", show_moment(&m,2)/nu/nppb); printf("df= %g\n", nu); } else { nsize = nx * ny * nz; sum = mean = sigma = skew = kurt = 0; if (maxmom > 0) { mean = mean_moment(&m); sum = show_moment(&m,1); } if (maxmom > 1) sigma = sigma_moment(&m); if (maxmom > 2) skew = skewness_moment(&m); if (maxmom > 3) kurt = kurtosis_moment(&m); printf ("Number of points : %d\n",n_moment(&m)); printf ("Min and Max : %f %f\n",min_moment(&m), max_moment(&m)); printf ("Mean and dispersion : %f %f\n",mean,sigma); printf ("Skewness and kurtosis : %f %f\n",skew,kurt); printf ("Sum and Sum*%s : %f %f\n",slabel, sum, sum*sov); if (Qmedian) { if (Qtorben) { printf ("Median Torben : %f (%d)\n",median_torben(ngood,data,min_moment(&m),max_moment(&m)),ngood); } else { printf ("Median : %f\n",get_median(ngood,data)); q2 = median(ngood,data); q1 = median_q1(ngood,data); q3 = median_q3(ngood,data); printf ("Q1,Q2,Q3 : %f %f %f\n",q1,q2,q3); } #if 1 if (ndat>0) printf ("MedianL : %f\n",median_moment(&m)); #endif } if (Qrobust) { compute_robust_moment(&m); printf ("Mean Robust : %f\n",mean_robust_moment(&m)); printf ("Sigma Robust : %f\n",sigma_robust_moment(&m)); printf ("Median Robust : %f\n",median_robust_moment(&m)); } if (Qmmcount) { min_count = max_count = 0; xmin = min_moment(&m); xmax = max_moment(&m); for (i=0; i<nx; i++) { for (j=0; j<ny; j++) { for (k=0; k<nz; k++) { x = CubeValue(iptr,i,j,k); if (x==xmin) min_count++; if (x==xmax) max_count++; } } } /* i */ printf("Min_Max_count : %d %d\n",min_count,max_count); } printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize); } } else { /* treat each plane seperately */ /* tabular output, one line per (selected) plane */ printf("# iz z min max N mean sigma skew kurt sum sumsov "); if (Qmedian) printf(" [med med]"); if (Qrobust) printf(" robust[N mean sig med]"); if (Qmaxpos) printf(" maxposx maxposy"); printf("\n"); ini_moment(&m,maxmom,ndat); for (ki=0; ki<nplanes; ki++) { reset_moment(&m); k = planes[ki]; z = Zmin(iptr) + k*Dz(iptr); ngood = 0; for (j=0; j<ny; j++) { for (i=0; i<nx; i++) { x = CubeValue(iptr,i,j,k); if (Qmin && x<xmin) continue; if (Qmax && x>xmax) continue; if (Qbad && x==bad) continue; if (Qmaxpos) { if (i==0 && j==0) { dmax = x; maxpos[0] = 0; maxpos[1] = 0;} else if (x>dmax) { dmax = x; maxpos[0] = i; maxpos[1] = j;} } w = Qw ? CubeValue(wptr,i,j,k) : 1.0; accum_moment(&m,x,w); if (Qmedian) data[ngood++] = x; } } nsize = nx * ny * nz; sum = mean = sigma = skew = kurt = 0; if (maxmom > 0) { mean = mean_moment(&m); sum = show_moment(&m,1); } if (maxmom > 1) sigma = sigma_moment(&m); if (maxmom > 2) skew = skewness_moment(&m); if (maxmom > 3) kurt = kurtosis_moment(&m); if (n_moment(&m) == 0) { printf("# %d no data\n",k+1); continue; } printf("%d %f %f %f %d %f %f %f %f %f %f", k+1, z, min_moment(&m), max_moment(&m), n_moment(&m), mean,sigma,skew,kurt,sum,sum*sov); if (Qmedian) { printf (" %f",get_median(ngood,data)); if (ndat>0) printf (" %f",median_moment(&m)); } if (Qrobust) { compute_robust_moment(&m); printf (" %d %f %f %f",n_robust_moment(&m), mean_robust_moment(&m), sigma_robust_moment(&m), median_robust_moment(&m)); } if (Qmaxpos) { printf(" %d %d",maxpos[0],maxpos[1]); } #if 0 if (Qmmcount) { min_count = max_count = 0; xmin = min_moment(&m); xmax = max_moment(&m); for (i=0; i<nx; i++) { for (j=0; j<ny; j++) { x = CubeValue(iptr,i,j,k); if (x==xmin) min_count++; if (x==xmax) max_count++; } } /* i,j */ printf(" %d %d",min_count,max_count); } printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize); #endif printf("\n"); } /* ki */ } }
int main(int argc, char *argv[]) { char *broker_ip = strdup("127.0.0.1"); int recv_port = 5562; int send_port = 5561; int run_seconds = 10; int num_channels = 50; int msg_size = 20; int num_clients = get_cpu_count() / 2; num_clients = num_clients > 0 ? num_clients : 1; int verbose = 0; int opt; while ((opt = getopt(argc, argv, "b:r:s:t:S:C:vh")) != -1) { switch (opt) { case 'b': free(broker_ip); broker_ip = strdup(optarg); break; case 'r': recv_port = atoi(optarg); break; case 's': send_port = atoi(optarg); break; case 't': run_seconds = atoi(optarg); break; case 'S': msg_size = atoi(optarg); break; case 'C': num_clients = atoi(optarg); break; case 'v': verbose = 1; break; case 'h': usage(); exit(EXIT_SUCCESS); default: fprintf(stderr, "Try 'nanomsg_pubsub_cliet -h' for more information"); exit(EXIT_FAILURE); } } int i; char **channels = (char **) malloc(sizeof(char *) * num_channels); for (i = 0; i < num_channels; i++) { channels[i] = (char *) malloc(SIZE8); memset(channels[i], '\0', SIZE8); sprintf(channels[i], "%d", i); } pubsub_info *info = (pubsub_info *) malloc(sizeof(pubsub_info)); info->broker_ip = broker_ip; info->recv_port = recv_port; info->send_port = send_port; info->msg_size = msg_size; info->num_channels = num_channels; info->channels = channels; info->verbose = verbose; pthread_t *pub_threads = (pthread_t *) malloc(sizeof(pthread_t) * num_clients); for (i = 0; i < num_clients; i++) { pthread_create(&pub_threads[i], NULL, publisher, (void *)info); } sleep(1); pthread_t *sub_threads = (pthread_t *) malloc(sizeof(pthread_t) * num_clients); for (i = 0; i < num_clients; i++) { pthread_create(&sub_threads[i], NULL, subscriber, (void *)info); } int rc; int metrics_fd; metrics_fd = nn_socket(AF_SP, NN_SUB); assert(metrics_fd != -1); char sub_addr[SIZE32]; rc = sprintf(sub_addr, "tcp://%s:%d", broker_ip, send_port); sub_addr[rc] = '\0'; rc = nn_connect(metrics_fd, sub_addr); assert(rc >= 0); rc = nn_setsockopt(metrics_fd, NN_SUB, NN_SUB_SUBSCRIBE, "metrics", 7); assert(rc == 0); INT64 start, now; get_timestamp(&start); int *stats = (int *) malloc(sizeof(int) * num_clients * run_seconds); int cnt = 0; while (1) { get_timestamp(&now); if (now - start > run_seconds * 1000) { break; } char *msg = NULL; int bytes = nn_recv(metrics_fd, &msg, NN_MSG, 0); assert (bytes >= 0); char *start = strchr(msg, ' ') + 1; stats[cnt++] = atoi(start); nn_freemsg (msg); } printf("%d median msg/sec\n", get_median(stats, cnt)); free(broker_ip); free(info); free(stats); printf("done!\n"); return 0; }
int * breakout(double * ts, int n, int min_size, double beta, int degree, int *ol){ if (!ts || min_size < 5 || n < 2 * min_size || !ol){ return NULL; } double (*G)(double); switch(degree){ case 1 : G = Linear; break; case 2 : G = Quadratic; break; default : G = Const; break; } int * prev = (int*)malloc(sizeof(int) * (n + 1)); memset(prev, 0, sizeof(int) * (n + 1)); int * num = (int*)malloc(sizeof(int) * (n + 1)); memset(num, 0, sizeof(int) * (n + 1)); double * F = (double*)malloc(sizeof(double) * (n + 1)); memset(F, 0, sizeof(double) * (n + 1)); MTrace * m1 = m_create((CMP_FN)cmp_fn, NULL); MTrace * m2 = m_create((CMP_FN)cmp_fn, NULL); for (int s = 2 * min_size; s < n + 1; ++s){ m_clear(m1); m_clear(m2); for (int i = 0; i < min_size - 1; ++i){ m_add(m1, ts + i); } for (int i = min_size - 1; i < s; ++i){ m_add(m2, ts + i); } for (int t = min_size; t < s - min_size + 1; ++t){ m_add(m1, ts + t - 1); m_remove(m2, ts + t - 1); if (prev[t] > prev[t - 1]){ for (int i = prev[t - 1]; i < prev[t]; ++i){ m_remove(m1, ts + i); } } if (prev[t] < prev[t - 1]){ for (int i = prev[t]; i < prev[t - 1]; ++i){ m_add(m1, ts + i); } } double lm = *(double*)get_median(m1); double rm = *(double*)get_median(m2); double normalize = ((t - prev[t]) * (s - t)) \ / ((double)(s - prev[t]) * (s - prev[t])); double tmp_s = F[t] + normalize * (lm - rm) * (lm - rm) - beta * G(num[t]); if (tmp_s > F[s]){ num[s] = num[t] + 1; F[s] = tmp_s; prev[s] = t; } } } int k = num[n]; *ol = k; int * re = (int*)malloc(sizeof(int) * k); memset(re, 0, sizeof(int) * k); int i = n; while(i > 0){ if (prev[i]) re[--k] = prev[i]; i = prev[i]; } free(prev); prev = NULL; free(num); num = NULL; free(F); F = NULL; m_free(m1); m1 = NULL; m_free(m2); m2 = NULL; return re; }
// [[Rcpp::export]] extern "C" std::vector<int> EDM_percent(const std::vector<double>& Z, const int min_size=24, const double percent=0, const int degree=0){ //Z: time series //min_size: minimum segment size //beta: penalization term for the addition of a change point //identify which type of penalization to use double (*G)(double); switch(degree){ case 1: G=Linear; break; case 2: G=Quadratic; break; default: G=Const; break; } int n = Z.size(); std::vector<int> prev(n+1,0);//store optimal location of previous change point std::vector<int> number(n+1,0);//store the number of change points in optimal segmentation std::vector<double> F(n+1,0);//store optimal statistic value //F[s] is calculated using observations { Z[0], Z[1], ..., Z[s-1] } //trees used to store the "upper half" of the considered observations std::multiset<double> right_min, left_min; //trees used to store the "lower half" of the considered observations std::multiset<double, std::greater<double> > right_max, left_max; //Iterate over possible locations for the last change for(int s=2*min_size; s<n+1; ++s){ right_max.clear(); right_min.clear();//clear right trees left_max.clear(); left_min.clear();//clear left trees //initialize left and right trees to account for minimum segment size for(int i=prev[min_size-1]; i<min_size-1; ++i) insert_element(left_min, left_max, Z[i]); for(int i=min_size-1; i<s; ++i) insert_element(right_min, right_max, Z[i]); //Iterate over possible locations for the penultimate change for(int t=min_size; t<s-min_size+1; ++t){//modify limits to deal with min_size insert_element(left_min, left_max, Z[t-1]);//insert element into left tree remove_element(right_min, right_max, Z[t-1]);//remove element from right tree //left tree now has { Z[prev[t-1]], ..., Z[t-1] } //right tree now has { Z[t], ..., Z[s-1] } //check to see if optimal position of previous change point has changed //if so update the left tree if(prev[t] > prev[t-1]){ for(int i=prev[t-1]; i<prev[t]; ++i) remove_element(left_min, left_max, Z[i]); } else if(prev[t] < prev[t-1]){ for(int i=prev[t]; i<prev[t-1]; ++i) insert_element(left_min, left_max, Z[i]); } //calculate statistic value double left_median = get_median(left_min,left_max), right_median = get_median(right_min,right_max); double normalize = ( (t-prev[t]) * (s-t) ) / ( std::pow(static_cast<double>(s-prev[t]),2) ); double tmp = F[t] + normalize * std::pow(static_cast<double>(left_median - right_median),2); //Find best location for change point. check % condition later if(tmp > F[s]){ number[s] = number[t] + 1; F[s] = tmp; prev[s] = t; } } //check to make sure we meet the percent change requirement if( prev[s]){ if(F[s] - F[prev[s]] < percent*G(number[prev[s]])*F[prev[s]]){ number[s] = number[prev[s]]; F[s] = F[prev[s]]; prev[s] = prev[prev[s]]; } } } //obtain list of optimal change point estimates std::vector<int> ret; int at = n; while(at){ if(prev[at])//don't insert 0 as a change point estimate ret.push_back(prev[at]); at = prev[at]; } sort(ret.begin(),ret.end()); return ret; }
void Breakpoint::predict_SV() { bool aln = false; bool split = false; bool noise = false; int num = 0; std::map<long, int> starts; std::map<long, int> stops; std::map<long, int> lengths; //ins! std::map<std::string, int> strands; std::vector<long> start2; std::vector<long> stops2; std::vector<long> lengths2; for (std::map<std::string, read_str>::iterator i = positions.support.begin(); i != positions.support.end(); i++) { if ((*i).second.SV & this->sv_type) { ///check type //cout << "Hit" << endl; if ((*i).second.coordinates.first != -1) { if (starts.find((*i).second.coordinates.first) == starts.end()) { starts[(*i).second.coordinates.first] = 1; } else { starts[(*i).second.coordinates.first]++; } start2.push_back((*i).second.coordinates.first); } if ((*i).second.coordinates.second != -1) { //TODO test if (stops.find((*i).second.coordinates.second) == stops.end()) { stops[(*i).second.coordinates.second] = 1; } else { stops[(*i).second.coordinates.second]++; } stops2.push_back((*i).second.coordinates.second); } if ((*i).second.SV & INS) { //check lenght for ins only! // std::cout<<"LENGTH 1st: "<<(*i).second.length<<" "<<(*i).first<<std::endl; if (lengths.find((*i).second.length) == lengths.end()) { lengths[(*i).second.length] = 1; } else { lengths[(*i).second.length]++; } lengths2.push_back((*i).second.length); } if (!((*i).second.type == 0 && ((*i).second.SV & INV))) { std::string tmp = translate_strand((*i).second.strand); if (strands.find(tmp) == strands.end()) { strands[tmp] = 1; } else { strands[tmp]++; } } if ((*i).second.type == 0) { aln = true; } else if ((*i).second.type == 1) { split = true; } else if ((*i).second.type == 2) { noise = true; } else { std::cerr << "Type " << (*i).second.type << std::endl; } num++; } } long mean = 0; long counts = 0; int maxim = 0; long coord = 0; for (map<long, int>::iterator i = starts.begin(); i != starts.end(); i++) { // cout << "start:\t" << (*i).first << " " << (*i).second << endl; if ((*i).second > maxim) { coord = (*i).first; maxim = (*i).second; } //mean += ((*i).first * (*i).second); // counts += (*i).second; } if (maxim < 5) { this->positions.start.most_support = get_median(start2); //mean / counts; } else { this->positions.start.most_support = coord; } maxim = 0; coord = 0; mean = 0; counts = 0; for (map<long, int>::iterator i = stops.begin(); i != stops.end(); i++) { // cout << "stop:\t" << (*i).first << " " << (*i).second << endl; if ((*i).second > maxim) { coord = (*i).first; maxim = (*i).second; } //mean += ((*i).first * (*i).second); // counts += (*i).second; } if (maxim < 5) { this->positions.stop.most_support = get_median(stops2); // mean / counts; } else { this->positions.stop.most_support = coord; } if (!(this->get_SVtype() & INS)) { //all types but Insertions: this->length = this->positions.stop.most_support - this->positions.start.most_support; } else { //compute most supported length for insertions: maxim = 0; coord = 0; mean = 0; counts = 0; for (map<long, int>::iterator i = lengths.begin(); i != lengths.end(); i++) { // std::cout<<"LENGTH: "<<(*i).first<<" : "<<(*i).second<<std::endl; if ((*i).second > maxim) { coord = (*i).first; maxim = (*i).second; } // mean += ((*i).first * (long) (*i).second); // counts += (*i).second; } if (maxim < 3) { this->length = get_median(lengths2); } else { this->length = coord; } } starts.clear(); stops.clear(); for (size_t i = 0; i < strands.size(); i++) { maxim = 0; std::string id; for (std::map<std::string, int>::iterator j = strands.begin(); j != strands.end(); j++) { if (maxim < (*j).second) { maxim = (*j).second; id = (*j).first; //std::cout << '\t' << id << std::endl; } } if (maxim > 0) { this->strand.push_back(id); strands[id] = 0; } } strands.clear(); this->supporting_types = ""; if (aln) { this->supporting_types += "AL"; } if (split) { if (!supporting_types.empty()) { this->supporting_types += ","; } this->supporting_types += "SR"; } if (noise) { if (!supporting_types.empty()) { this->supporting_types += ","; } this->supporting_types += "NR"; } }
nemo_main() { int i, j, k; real x, xmin, xmax, mean, sigma, skew, kurt, median, bad, w, *data; real sum, sov; Moment m; bool Qmin, Qmax, Qbad, Qw, Qmedian, Qmmcount = getbparam("mmcount"); real nu, nppb = getdparam("nppb"); int npar = getiparam("npar"); int ngood = 0; int min_count, max_count; instr = stropen (getparam("in"), "r"); read_image (instr,&iptr); strclose(instr); if (hasvalue("win")) { instr = stropen (getparam("win"), "r"); read_image (instr,&wptr); strclose(instr); if (Nx(iptr) != Nx(wptr)) error("X sizes of in/win don't match"); if (Ny(iptr) != Ny(wptr)) error("X sizes of in/win don't match"); if (Nz(iptr) != Nz(wptr)) error("X sizes of in/win don't match"); Qw = TRUE; } else Qw = FALSE; nx = Nx(iptr); ny = Ny(iptr); nz = Nz(iptr); Qmin = hasvalue("min"); if (Qmin) xmin = getdparam("min"); Qmax = hasvalue("max"); if (Qmax) xmax = getdparam("max"); Qbad = hasvalue("bad"); if (Qbad) bad = getdparam("bad"); Qmedian = getbparam("median"); if (Qmedian) data = (real *) allocate(nx*ny*nz*sizeof(real)); sov = Dx(iptr)*Dy(iptr)*Dz(iptr); /* voxel volume; TODO: should we do 2D vs. 3D ? */ ini_moment(&m,4,0); for (i=0; i<nx; i++) { for (j=0; j<ny; j++) { for (k=0; k<nz; k++) { x = CubeValue(iptr,i,j,k); if (Qmin && x<xmin) continue; if (Qmax && x>xmax) continue; if (Qbad && x==bad) continue; w = Qw ? CubeValue(wptr,i,j,k) : 1.0; accum_moment(&m,x,w); if (Qmedian) data[ngood++] = x; } } } if (npar > 0) { nu = n_moment(&m)/nppb - npar; if (nu < 1) error("%g: No degrees of freedom",nu); printf("chi2= %g\n", show_moment(&m,2)/nu/nppb); printf("df= %g\n", nu); } else { nsize = nx * ny * nz; mean = mean_moment(&m); sigma = sigma_moment(&m); skew = skewness_moment(&m); kurt = kurtosis_moment(&m); sum = show_moment(&m,1); printf ("Min=%f Max=%f\n",min_moment(&m), max_moment(&m)); printf ("Number of points : %d\n",n_moment(&m)); printf ("Mean and dispersion : %f %f\n",mean,sigma); printf ("Skewness and kurtosis : %f %f\n",skew,kurt); printf ("Sum and Sum*Dx*Dy*Dz : %f %f\n",sum, sum*sov); if (Qmedian) printf ("Median : %f\n",get_median(ngood,data)); if (Qmmcount) { min_count = max_count = 0; xmin = min_moment(&m); xmax = max_moment(&m); for (i=0; i<nx; i++) { for (j=0; j<ny; j++) { for (k=0; k<nz; k++) { x = CubeValue(iptr,i,j,k); if (x==xmin) min_count++; if (x==xmax) max_count++; } } } /* i */ printf("Min_Max_count : %d %d\n",min_count,max_count); } printf ("%d/%d out-of-range points discarded\n",nsize-n_moment(&m), nsize); } }