Пример #1
0
  std::vector<flexible_type> pylambda_evaluator::bulk_eval_dict_rows(
    size_t lambda_hash,
    const std::vector<std::string>& keys,
    const sframe_rows& rows,
    bool skip_undefined,
    int seed) {

    python_thread_guard py_thread_guard;

    set_lambda(lambda_hash);
    py_set_random_seed(seed);
    std::vector<flexible_type> ret(rows.num_rows());
    python::dict input;
    size_t cnt = 0;
    try {
      std::vector<flexible_type> row(rows.num_columns());
      for (const auto& row_iter: rows) {
        for (size_t j = 0; j < row.size(); ++j)
          row[j] = row_iter[j];
        PyDict_UpdateFromFlex(input, keys, row);
        python::object output = m_current_lambda->operator()(input);
        PyObject_AsFlex(output, ret[cnt]);
        cnt++;
      }
    } catch (python::error_already_set const& e) {
      std::string error_string = parse_python_error();
      throw(error_string);
    } catch (std::exception& e) {
      throw(std::string(e.what()));
    } catch (...) {
      throw("Unknown exception from python lambda evaluation.");
    }
    return ret;
  }
Пример #2
0
void
librandom::PoissonRandomDev::set_status( const DictionaryDatum& d )
{
  /*
    Limits on mu:

    - mu >= 0 trivial
    - As shown in comments in ldev(), the maximum absolute value
      that can be chosen as a candidate is mu + 710 * sqrt(mu).
    - We thus must require mu + 710 * sqrt(mu) < max(long).
    - This is equivalent to

         mu < ( 2 N + a^2 - sqrt(4 N + a^2) ) / 2

      where N is the largest representable integer and a = 710.
    - Numerical evaluation shows that mu < 0.999 N is safe for 32
      and 64 bit doubles with a good margin.
  */

  const double MU_MAX = 0.999 * std::numeric_limits< long >::max();

  double new_mu = mu_;

  if ( updateValue< double >( d, "lambda", new_mu ) )
  {
    if ( new_mu < 0 )
      throw BadParameterValue( "Poisson RDV: lambda >= 0 required." );
    if ( new_mu > MU_MAX )
      throw BadParameterValue( String::compose( "Poisson RDV: lambda < %1 required.", MU_MAX ) );
    set_lambda( new_mu );
  }
}
Пример #3
0
 flexible_type pylambda_evaluator::eval(size_t lambda_hash, const flexible_type& arg) {
   set_lambda(lambda_hash);
   try {
       python::object input = PyObject_FromFlex(arg);
       python::object output = m_current_lambda->operator()(input);
       return PyObject_AsFlex(output);
   } catch (python::error_already_set const& e) {
     std::string error_string = parse_python_error();
     throw(error_string);
   } catch (std::exception& e) {
     throw(std::string(e.what()));
   } catch (...) {
     throw("Unknown exception from python lambda evaluation.");
   }
 }
Пример #4
0
void Cluster::Load(string filename) 
{
  //open and read file to memory
  if(_Read(filename) != 0) {
    cout <<"Error reading file"<<endl;
    exit;
  };

  //parse term-doc count file
  _Parse(terms);

  //Weight by tf*idf, return total number of food items
  this->N = _Weight();

  //normalize indices to unit vector
  _Norm();

  set_lambda(0.5);
}
Пример #5
0
  std::vector<flexible_type> pylambda_evaluator::bulk_eval_rows(
    size_t lambda_hash,
    const sframe_rows& rows,
    bool skip_undefined,
    int seed) {

    python_thread_guard py_thread_guard;

    set_lambda(lambda_hash);

    py_set_random_seed(seed);

    std::vector<flexible_type> ret(rows.num_rows());
    size_t i = 0;
    for (const auto& x : rows) {
      if (skip_undefined && x[0] == FLEX_UNDEFINED) {
        ret[i++] = FLEX_UNDEFINED;
      } else {
        ret[i++] = eval(lambda_hash, x[0]);
      }
    }
    return ret;
  }
Пример #6
0
  /**
   * Bulk version of eval.
   */
  std::vector<flexible_type> pylambda_evaluator::bulk_eval(
    size_t lambda_hash,
    const std::vector<flexible_type>& args,
    bool skip_undefined,
    int seed) {

    python_thread_guard py_thread_guard;

    set_lambda(lambda_hash);

    py_set_random_seed(seed);

    std::vector<flexible_type> ret(args.size());
    size_t i = 0;
    for (const auto& x : args) {
      if (skip_undefined && x == FLEX_UNDEFINED) {
        ret[i++] = FLEX_UNDEFINED;
      } else {
        ret[i++] = eval(lambda_hash, x);
      }
    }
    return ret;
  }
Пример #7
0
  // Sets hyperparameters from vector containing logs of hyperparameters
void Sampler::SetHyperparameters(const vector<double>& hyperparameters) {
  assert(hyperparameters.size() == 2);
  set_alpha(exp(hyperparameters[0]));
  set_lambda(exp(hyperparameters[1]));
}
Пример #8
0
 void HomogeneousPoissonProcess::mle(){
   double lambda_hat =  suf()->count() / suf()->exposure();
   set_lambda(lambda_hat);
 }
Пример #9
0
static SRM_binning option_assign(int argc, char **argv)
	{ SRM_binning args;
	  int c,option_index;
	  static struct option long_options[] =
			{ {"multiplicity", required_argument ,0,0},
			  {"binOut",required_argument,0,2},
			  {"inb",required_argument,0,3},
			  {"perm",no_argument,0,4},
			  {0,0,0,0} 
			};
	  char *insert=NULL;

	  args.multple = 2.0;
	  args.quantile = 0.95;
	  args.bin_size = 100;
	  args.win_size = 200;
	  args.output_file = NULL;
	  args.binout_file = NULL;
	  args.inbin_file = NULL;
	  args.outlier_file=NULL;
	  args.autoselect_lambda = 0;
	  args.FP = 1;
	  args.B = 10;
	  args.fdr = 0;
	  args.lambda = 2;
	  args.paired = 0;
	  args.tumor_file = NULL;
	  args.normal_file = NULL;
	  args.insert = 220;
	  args.sd = 20;
	  args.tumor_freq = 0.5;
	  args.resampling = 1;
	
	  args.in_tmor = NULL;
	  args.in_nml = NULL;
	  args.output = NULL;
	  args.inbin = NULL;
	  args.outbin = NULL;
	  args.outlier = NULL;

	  while((c=getopt_long(argc,argv,"2q:b:w:o:R:hf:p:B:l:",long_options,&option_index))!=-1){
		switch(c){
			case 0:
				args.multple = atoi(optarg);
				if(args.multple<1.0) { fprintf(stderr,"Error,multiplicity must be larger than or equal to 1.0\n"); exit(1);}
				break;
			case 2:
				args.binout_file = strdup(optarg);
				break;
			case 3: 
				args.inbin_file = strdup(optarg);
				break;
			case 4: args.resampling = 0;
				break;
			case 'l': 
				args.lambda = atof(optarg);
				if(args.lambda<=0.0) { fprintf(stderr,"Parameter misspecification: Lambda must be positive number.\n"); exit(1);}
				break;
                        case 'f':
                                args.FP = atof(optarg);
                                args.autoselect_lambda = 1;
                                if(args.FP<=0.0) {fprintf(stderr,"Expected number of type I error must be postive\n");exit(1);}
                                break;

			case 'I':
				insert = strdup(optarg);
				args.paired = 1;
				break;
			case 'q':
				args.quantile = atof(optarg);
				if(args.quantile<=0.0 || args.quantile>1.0)  { fprintf(stderr,"Quantile should be between 0 and 1.\n"); exit(1);}
				break;
			case 'b':
				args.bin_size = atoi(optarg);
				if(args.bin_size<1) {fprintf(stderr,"Bin size should be at least 1.\n");exit(1);}
				break;
			case 'w':
				args.win_size = atoi(optarg);
				if(args.win_size<1) {fprintf(stderr,"The window size must be positive.\n");exit(1);}
				break;
			case '2':
				args.paired = 1;
				break;
			case 'o':
				args.output_file = strdup(optarg);
				break;
			case 'R':
				args.outlier_file = strdup(optarg);
                        case 'p':
                                args.tumor_freq = atof(optarg);
                                if(args.tumor_freq<0.0||args.tumor_freq>=1.0){fprintf(stderr,"The probabilty of a read being a tumor read must be between 0 and 1\n");exit(1);}
                                args.fdr = 1;
                                break;
                        case 'B':
                                args.B = atoi(optarg);
                                if(args.B<=0) {fprintf(stderr,"The option -B must be a positve number\n");exit(1);}
                                break;
			case 'h': 
				explain_command(argv);
				exit(0);
			case '?': /* getopt_long already printed an error message. */
				exit(1);
				break;
			default:
				abort (); 
			}
		}

        if (argc - optind!=2&&args.inbin_file==NULL){
           	explain_command(argv); exit(1);
         	}

	if(args.inbin_file==NULL){
		args.tumor_file = strdup(argv[optind]);
		args.normal_file = strdup(argv[optind+1]);

	        args.in_tmor = fopen(args.tumor_file,"r");
	        if(args.in_tmor == NULL)
	                { fprintf(stderr,"fopen %s: %s\n", args.tumor_file,strerror(errno));
        	          exit(1);
	                }

	        args.in_nml = fopen(args.normal_file,"r");
	        if(args.in_nml == NULL)
        	        { fprintf(stderr,"fopen %s: %s\n", args.normal_file,strerror(errno));
	                  exit(1);
	                }

		}
	else{
		args.inbin =  fopen(args.inbin_file,"r");
		if(args.inbin==NULL){
			fprintf(stderr,"fopen %s: %s\n", args.inbin_file,strerror(errno));
                        exit(1);
			}
		}
	

        if(args.outlier_file!=NULL) {
                args.outlier = fopen(args.outlier_file,"w");
                if(args.outlier==NULL) {fprintf(stderr,"fopen %s: %s\n",args.outlier_file,strerror(errno));exit(1);}
                }

        if(args.output_file!=NULL){
                args.output = fopen(args.output_file,"w");
                if(args.output==NULL) { fprintf(stderr,"fopen %s: cannot create the file.\n",args.output_file); exit(1);}
                }else{
		args.output = stdout;
		}

        if(args.binout_file!=NULL){
                args.outbin = fopen(args.binout_file,"w");
                if(args.outbin==NULL) { fprintf(stderr,"fopen %s: cannot create the file.\n",args.binout_file); exit(1);}
                }

	if(insert!=NULL){
		char *substr;
		substr = strtok(insert,",");
		args.insert = atoi(substr);
		substr=strtok(NULL,",");
		if(substr==NULL){fprintf(stderr,"Incorrect format for option -I\n");exit(1);}
		args.sd = atoi(substr);
		if(args.insert<0||args.sd<0) {fprintf(stderr,"Error, insert size and its standard deviaiton must be positive\n");}
		}

	set_lambda(args.lambda);

	return args;

	}