Clearing the environment and exception handling

This commit is contained in:
hollorol 2019-01-16 02:05:40 +01:00
parent b3a2034182
commit 8fe211ff1e
31 changed files with 6 additions and 299 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
*.o
*.a
*~
*.dll

View File

@ -54,6 +54,7 @@ importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(limSolve,xsample)
importFrom(magrittr,'%<>%')
importFrom(rmarkdown,pandoc_version)
importFrom(rmarkdown,render)
importFrom(scales,percent)
importFrom(tcltk,tk_choose.files)

View File

@ -20,9 +20,9 @@ paramSweep <- function(inputDir="./",
outVar="daily_gpp",
htmlOutName = "paramsweep.html"){
if(is.null(rmarkdown::pandoc_version())){
stop("In order to use parameterSweep you have to have pandoc (1.12.3+) installed or run this function from Rstudio\n
You can download pandoc from here: 'https://pandoc.org/', or Rstudio from here: 'https://www.rstudio.com/'")
if(is.null(pandoc_version())){
stop("In order to use parameterSweep you have to have\n pandoc (1.12.3+) installed or run this function from Rstudio\n
You can download pandoc from here: 'https://pandoc.org/',\n or Rstudio from here: 'https://www.rstudio.com/'")
}
currDir <- getwd()
opSystem <- Sys.info()[[1]]

View File

@ -1,128 +0,0 @@
#include <Rcpp.h>
#include <numeric>
#include <iostream>
#include <fstream>
#include <string>
#include <new>
#include <algorithm>
#include <numeric>
#include <ctime>
using namespace Rcpp;
using namespace std;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
IntegerVector getWritePositions(double a){
//getWritePositions returns abstract rownumbers to rownumbers and other indexek
// getWritePositions(173.72) = c(173,7,3) // it supports up to 10 subvalues
IntegerVector outVec(3); // outVec is vector of rowIndex, colNumber, choosen Index
a = a * 100;
outVec[0] = (int)a / 100;
outVec[1] = ((int)a /10) % 10 + 1;
outVec[2] = (int)a % 10;
return outVec;
}
IntegerMatrix getPositions(NumericVector v){
int numVari = v.size();
IntegerMatrix indexek(numVari,3);
IntegerVector positions(3);
for(int i = 0; i < numVari; ++i){
positions = getWritePositions(v[i]);
indexek(i,_) = positions;
}
return indexek;
}
void goNextLine(std::ifstream& fin){
char c='a';
while((c!='\n')&&(fin.get(c))){}
}
#define NEXT goNextLine(fin)
int fileChanger(std::string inFile, std::string outFile, IntegerVector linum, NumericVector num, IntegerVector colnum, IntegerVector colindex){
std::ifstream fin(inFile);
if (!fin.is_open()) {
stop("Cannot open " + inFile + " for read");
}
std::ofstream fot(outFile);
if (!fot.is_open()) {
stop("Cannot open " + outFile + " for read");
}
string tempString;
int counter = 1;
int counterV = 0;
while (!fin.eof()) {
if(counter == linum[counterV]){
if(colnum[counterV]==0){
fot << num[counterV] << "\n";
NEXT;
} else {
double * elements;
elements = new double [colnum[counterV]];
if(linum[counterV]!=linum[counterV+1]){
for(int i=0;i<colnum[counterV];++i){
fin >> elements[i];
if(i==colindex[counterV]){
elements[i]=num[counterV];
}
fot << elements[i] << '\t';
}
} else {
int k=0;
for(int i=0;i<colnum[counterV];++i){
fin >> elements[i];
if(i==colindex[counterV + k]){
elements[i]=num[counterV + k];
if(linum[counterV +k]==linum[counterV + k + 1]){
++k;
}
}
fot << elements[i] << '\t';
}
counterV = counterV + k;
}
fot << "\n";
delete [] elements;
NEXT;
}
++counterV;
} else {
getline(fin,tempString);
fot << tempString << "\n";
}
++counter;
}
fin.close();
fot.close();
return 0;
}
//' changeMusoC
//'
//' This function is fastly randomize values based on min and max values
//' @importFrom Rcpp evalCpp
//' @useDynLib RBBGCMuso
//' @param inFile is the big matrix
//' @param outFile is the small matrix
//' @export
// [[Rcpp::export]]
void changeMusoC(std::string inFile, std::string outFile, NumericMatrix inMat){
int numChanges = inMat.nrow();
IntegerMatrix indexes(numChanges,3);
indexes = getPositions(inMat(_,0));
fileChanger(inFile,outFile,indexes(_,0),inMat(_,1),indexes(_,1),indexes(_,2));
}

View File

@ -1,167 +0,0 @@
#include <Rcpp.h>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <ctime>
using namespace Rcpp;
using namespace std;
// [[Rcpp::plugins(cpp11)]]
NumericMatrix randTypeZero(NumericMatrix m){
/*
A typical matrix like m:
| INDEX | DEPENDENCE | MIN | MAX |
|-------+------------+------+------|
| 21 | 0 | 0 | 364 |
| 57 | 0 | bkla | asdf |
This randomization type is the easiest,
the function produces a matrix which first
column contains the indexes, and the second contains
random numbers, which was drawn from uniform distribution
with the corresponding min and max parameters, specified
by the matrix m.
*/
int n=m.nrow()-1;
NumericMatrix M(n+1,2);
M(_,0)=m(_,0);
for(int i=0;i<=n;++i){
double min=m(i,2);
double max=m(i,3);
M(i,1)=runif(1,min,max)[0];
}
return M;
}
// [[Rcpp::export]]
NumericMatrix randTypeOne(NumericMatrix m){
/*
The stucture of matrix m here is the same
as in randTypeZero.
*/
NumericVector dependence=m(_,2);
int n=m.nrow()-1;
NumericMatrix M(n+1,2);
M(_,0)=m(_,0);
M(0,1)=runif(1,m(0,2),m(0,3))[0];
for(int i=1;i<=n;++i){
int dep=m(i,1)-1;
double min=max(M(dep,1),m(i,2));
double max=m(i,3);
M(i,1)=runif(1,min,max)[0];
}
return M;
}
IntegerVector orderDec(NumericVector v){
Function f("order");
return f(v,_["decreasing"]=1);
}
NumericMatrix randTypeTwo(NumericMatrix m){
int n=m.nrow()-1;
int N=n-1;
NumericMatrix mv=m(Range(0,(n-1)),_);
NumericVector dependence=m(_,2);
NumericMatrix M(n+1,2);
M(_,0)=m(_,0);
IntegerVector indexes=orderDec(mv(_,2));
NumericVector sorban=mv(_,2);
sorban.sort(true);
NumericVector sor=cumsum(sorban);
sor.sort(true);
for(int i=0;i<=N;++i){
if(i!=N){
mv((indexes[i]-1),3)-= sor[i+1];
}
}
double rollingNumber=0;
for(int i=0;i<=N;++i){
double minimum=mv((indexes[i]-1),2);
double maximum=mv((indexes[i]-1),3)-rollingNumber;
M(i,1)=runif(1,minimum,maximum)[0];
rollingNumber+=M(i,1);
// cout << "minimum:\t" << minimum << endl;
// cout << "maximum:\t" << maximum << endl;
// cout << "indexes:\t" << indexes[i] << endl;
// cout << "rollingNumber:\t" << rollingNumber << endl;
// cout << "choosen:\t" << M(i,1) <<endl;
// cout << "sor:\t" << sor <<endl;
// cout << "\n\n\n" << mv;
}
M(n,1)=1-rollingNumber;
return M;
}
NumericMatrix copyMatSpec(NumericMatrix A, NumericMatrix B, int u, int v){
int k=0;
for(int i=u;i<=v;++i){
A(i,_)=B(k,_);
k+=1;
}
return A;
}
//' musoRandomizer
//'
//' This function is fastly randomize values based on min and max values,
//' and row indexes.
//' @importFrom Rcpp evalCpp
//' @useDynLib RBBGCMuso
//' @param A is the big matrix
//' @param B is the small matrix
//' @export
// [[Rcpp::export]]
NumericMatrix musoRandomizer(NumericMatrix A, NumericMatrix B){
NumericMatrix M(A.nrow(),2);
int nGroup = B.nrow()-1;
int k=0;
for(int i=0;i<=nGroup;++i)
{
int b=B(i,0)-1;
int till=b+k;
int t=B(i,1);
// cout << b << "\t" << t <<endl;
switch(t){
case 0:
M=copyMatSpec(M,randTypeZero(A(Range(k,till),_)),k,till);
// cout << M << endl;
break;
case 1:
M=copyMatSpec(M,randTypeOne(A(Range(k,till),_)),k,till);
// cout << M << endl;
break;
case 2:
M=copyMatSpec(M,randTypeTwo(A(Range(k,till),_)),k,till);
// cout << M << endl;
break;
}
k=till+1;
// cout << k << endl;
}
return M;
}
std::string concatenate(std::string A, std::string B){
std::string C = A + B;
return C;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.