I have the following code :
#include <Rinternals.h>
#include <cmath>
extern "C" {
SEXP myChar(int x) {
/*PrintValue(nam);*/
SEXP colnames, result;
PROTECT(colnames = Rf_allocVector(STRSXP,2));
char buffer[20];
int n = sprintf(buffer,"%4.2f%%",2.5);
SET_STRING_ELT(colnames,0,mkChar(buffer));
n = sprintf(buffer,"%4.2f%c",5.0,0x25);
SET_STRING_ELT(colnames,1,mkChar(buffer));
PROTECT(result = Rf_allocVector(VECSXP, 2));
SEXP scol1 = PROTECT(allocVector(REALSXP,10));
SEXP scol2 = PROTECT(allocVector(REALSXP,10));
for (size_t k=0;k<10;++k){
REAL(scol1)[k] = std::pow(k,0.5);
REAL(scol2)[k] = std::pow(k,0.7);
}
SET_VECTOR_ELT(result,0,scol1);
SET_VECTOR_ELT(result,1,scol2);
setAttrib(result, R_NamesSymbol, colnames);
UNPROTECT(4);
return(result);
}
}
I compile like this :
R CMD SHLIB myChar.cpp
And run the following in my R session :
> dyn.load("myChar.so")
> out<-as.data.frame(.Call("myChar"))
> out
X2.50. X5.00.
1 0.000000 0.000000
2 1.000000 1.000000
3 1.414214 1.624505
4 1.732051 2.157669
5 2.000000 2.639016
6 2.236068 3.085169
7 2.449490 3.505144
8 2.645751 3.904529
9 2.828427 4.287094
10 3.000000 4.655537
My question is why the column names doesn't show the percent sign? I use '%%' to escape the percent sign but in the column names it is converted to a period, i.e. instead of "X2.50%" I get "X2.50.". Is that a feature of mkChar? Is there any other way to convert char array to SEXP?
There is nothing wrong with your C/C++ code. The problem arises when you coerce to data.frame. Try this:
out<-.Call("myChar")
#names are correct
names(out)
#[1] "2.50%" "5.00%"
#now correctly to data.frame, see ?data.frame
out<-data.frame(out,check.names=FALSE)
# 2.50% 5.00%
#1 0.000000 0.000000
#2 1.000000 1.000000
#3 1.414214 1.624505
#4 1.732051 2.157669
#5 2.000000 2.639016
#6 2.236068 3.085169
#7 2.449490 3.505144
#8 2.645751 3.904529
#9 2.828427 4.287094
#10 3.000000 4.655537
See the check.names argument of data.frame to get a grasp of what is going on.
You can try using \045 to represent the percentage symbol.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to replace a line of a file, but only if the 3rd (last) value is above 0.000000.
Specifically I have an Ascii .stl file, like below, where I want to be able to check the last value of each vertex line, and replace the entire line with a different string if it is above 0.000000.
I am really struggling with understanding how to create a parser using regex, to look for this. I also don't know if that's the best way.
Any help with the parsing would be very much appreciated.
facet normal -1.000000 0.000000 0.000000
outer loop
vertex 26.000000 6.000000 0.000000
vertex 26.000000 6.000000 1.000000
vertex 26.000000 7.000000 1.000000
endloop
endfacet
facet normal -1.000000 0.000000 0.000000
outer loop
vertex 26.000000 6.000000 0.000000
vertex 26.000000 7.000000 1.000000
vertex 26.000000 7.000000 0.000000
endloop
endfacet
facet normal 0.000000 1.000000 0.000000
outer loop
vertex 26.000000 7.000000 0.000000
vertex 26.000000 7.000000 1.000000
vertex 27.000000 7.000000 1.000000
endloop
endfacet
endsolid
If it matters, I'm working in Microsoft Visual Studio 2019
For something as simple as detecting which line starts with "vertex", I don't think you need something as heavy as a regular expression.
Here is a very simple example of detecting the line that starts with "vertex":
#include <sstream>
#include <string>
#include <iostream>
std::string test = "facet normal -1.000000 0.000000 0.000000\n"
"outer loop\n"
"vertex 26.000000 6.000000 0.000000\n"
"vertex 26.000000 6.000000 1.000000\n"
"vertex 26.000000 7.000000 1.000000\n"
"endloop\n"
"endfacet\n"
"facet normal -1.000000 0.000000 0.000000\n"
"outer loop\n"
"vertex 26.000000 6.000000 0.000000\n"
"vertex 26.000000 7.000000 1.000000\n"
"vertex 26.000000 7.000000 0.000000\n"
"endloop\n"
"endfacet\n"
"facet normal 0.000000 1.000000 0.000000\n"
"outer loop\n"
"vertex 26.000000 7.000000 0.000000\n"
"vertex 26.000000 7.000000 1.000000\n"
"vertex 27.000000 7.000000 1.000000\n"
"endloop\n"
"endfacet\n"
"endsolid";
int main()
{
std::istringstream strm(test);
std::string line;
int lineNumber = 0;
while (std::getline(strm, line))
{
++lineNumber;
if (line.compare( 0, 6, "vertex", 6) == 0) // check if line starts with "vertex"
{
std::istringstream strm2(line); // read the line
std::string type;
double num1, num2, num3;
strm2 >> type >> num1 >> num2 >> num3;
if ( num3 > 0 ) // checks the last number
std::cout << "Line " << lineNumber << ": " << line << "\n"; // just outputs that line
}
}
}
Output:
Line 4: vertex 26.000000 6.000000 1.000000
Line 5: vertex 26.000000 7.000000 1.000000
Line 11: vertex 26.000000 7.000000 1.000000
Line 18: vertex 26.000000 7.000000 1.000000
Line 19: vertex 27.000000 7.000000 1.000000
In lieu of a file, the code reads the data from a stringstream.
It's just a matter of detecting the line that starts with "vertex", and then using a std::istringstream to read the data.
I would agree with the comments of others that Regex may not be the best approach depending on the nature of your solution. But for a Regex to find what you're after:
vertex.+\s[0-9\.]*[1-9][0-9\.]*$
Should do the trick.
(regex101)
I have an acoustic model that successfully converted from ONNX to OpenVino. However, in OpenVino this model outputs tensor that consists of zeroes from some position.
#include <iostream>
#include <fstream>
#include <iterator>
#include <inference_engine.hpp>
typedef struct {
float* data;
size_t size;
size_t timeLen;
} Fbank;
using namespace InferenceEngine;
using std::cout;
using std::endl;
void print_arr(std::string text, const float* arr, int l, int r) {
cout << text << endl;
for (int i = l; i < r; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void doInference(ExecutableNetwork& executable_network, const std::string& input_name, const std::string& output_name, Fbank* fbank) {
InferRequest infer_request = executable_network.CreateInferRequest();
InferenceEngine::TensorDesc tDesc(InferenceEngine::Precision::FP32,
{fbank->size, fbank->timeLen}, InferenceEngine::Layout::HW);
Blob::Ptr blob = InferenceEngine::make_shared_blob<float>(tDesc, fbank->data);
infer_request.SetBlob(input_name, blob);
infer_request.Infer();
Blob::Ptr output_blob = infer_request.GetBlob(output_name);
auto dims = output_blob->getTensorDesc().getDims();
size_t batchSize = dims[0];
size_t T = dims[1];
size_t D = dims[2];
MemoryBlob::CPtr moutput = as<MemoryBlob>(output_blob);
if (!moutput) {
return;
}
auto moutputHolder = moutput->rmap();
const float *pred = moutputHolder.as<const float*>();
print_arr("AM output:", pred, D*29, D*31);
}
int main() {
Fbank* fbank = new Fbank;
fbank->size = 64;
fbank->timeLen = 2000;
fbank->data = new float[64*2000];
Core ie;
CNNNetwork network = ie.ReadNetwork("quartznet_random.xml", "quartznet_random.bin");
std::string input_name = network.getInputsInfo().begin()->first;
std::string output_name = network.getOutputsInfo().begin()->first;
network.getOutputsInfo().begin()->second->setPrecision(Precision::FP32);
ExecutableNetwork executable_network = ie.LoadNetwork(network, "cpu");
doInference(executable_network, input_name, output_name, fbank);
return 0;
}
Outputs:
AM output:
0.138650 -5.833140 -8.023724 -7.637482 -8.001101 -9.033963 -8.029905 -8.132050 -9.186495 -8.537528 -8.788505 -9.240234 -8.547676 -8.673388 0.000000 0.000000 -0.000000 0.000000 -0.000000 0.000000 0.000000 -0.000000 -0.000000 0.000000 -0.000000 0.000000 0.000000 -0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 -0.000000 0.000000 0.000000 -0.000000 0.000000 0.000000 -0.000000 -0.000000 -0.000000 -0.000000 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 0.000000 -0.000000 -0.000000 0.000000 -0.000000 0.000000 -0.000000 -0.000000 -0.000000 0.000000 -0.000000 -0.000000 0.000000 -0.000000 0.000000
If I run ONNX model in Python using onnxruntime, the output will be correct. (Example).
Is it possible to fix it?
P.S. Command to convert the model from ONNX: python3 mo_onnx.py —input_model model.onnx —output="output" —input="fbanks[64 2000]"
Tested provided ONNX model in OpenVINO for Linux, couple of findings while testing OpenVINO 2020.1 and new 2020.2 version (released today 4/14/2020, release notes).
Using same command to convert from ONNX. Although its unclear what would be the expected output (probability between 0.0 and 1.0?), OpenVINO 2020.2 seems to affect the output results.
On 2020.1, observed similar results to yours (one can assume this is the OpenVINO version you used).
AM output: -3.55062 -3.5114 -3.50925 -3.52013 -3.51791 -3.54656 -3.53908 -3.54239 -3.53626 -3.50982 -3.54193 -3.55593 -3.52877 -3.53786 -1.546e-07 -6.14673e-08 -8.56817e-08 -1.41561e-07 -6.14673e-08 -1.16415e-07 -9.30158e-08 -9.12696e-08 -1.29454e-07 -1.04774e-07 -6.14673e-08 -5.58794e-08 -1.71363e-07 -1.02445e-07 -5.7742e-08 -1.35042e-07 -9.26666e-08 -1.00583e-07 -1.04308e-07 -1.2666e-07 -1.39698e-07 -7.26432e-08 -9.68575e-08 -1.47149e-07 -9.40636e-08 -9.77889e-08 -9.49949e-08 -1.16415e-07 -9.54606e-08 -8.3819e-08 -1.28523e-07 -1.35973e-07 -7.66013e-08 -1.12224e-07 -1.546e-07 -6.14673e-08 -8.56817e-08 -1.41561e-07 -6.14673e-08 -1.16415e-07 -9.30158e-08 -9.12696e-08 -1.29454e-07 -1.04774e-07 -6.14673e-08 -5.58794e-08 -1.71363e-07 -1.02445e-07 -5.7742e-08 -1.35042e-07 -9.26666e-08 -1.00583e-07 -1.04308e-07 -1.2666e-07
On OpenVINO 2020.2 had to change ExecutableNetwork executable_network = ie.LoadNetwork(network, "cpu"); to ExecutableNetwork executable_network = ie.LoadNetwork(network, "CPU"); as Inference Engine didnt't recognize lowercase CPU device, error was "terminate called after throwing an instance of 'InferenceEngine::details::InferenceEngineException'
what(): Device with "cpu" name is not registered in the InferenceEngine
Aborted (core dumped)"
On OpenVINO 2020.2, the results differ and are not close to zero (although all seem negative).
AM output: -3.55062 -3.5114 -3.50925 -3.52013 -3.51791 -3.54656 -3.53908 -3.54239 -3.53626 -3.50982 -3.54193 -3.55593 -3.52877 -3.53786 -3.52153 -3.52563 -3.51142 -3.54885 -3.52137 -3.54384 -3.53411 -3.55188 -3.5477 -3.52514 -3.51171 -3.5022 -3.5138 -3.50823 -3.50125 -3.51817 -3.53914 -3.50173 -3.50603 -3.51917 -3.55062 -3.5114 -3.50925 -3.52013 -3.51791 -3.54656 -3.53908 -3.54239 -3.53626 -3.50982 -3.54193 -3.55593 -3.52877 -3.53786 -3.52153 -3.52563 -3.51142 -3.54885 -3.52137 -3.54384 -3.53411 -3.55188 -3.5477 -3.52514 -3.51171 -3.5022 -3.5138 -3.50823 -3.50125 -3.51817 -3.53914 -3.50173 -3.50603 -3.51917
It's uncertain if the output results of OpenVINO 2020.2 are expected/correct. I am unable to test Python example with the ONNX model using onnxruntime, script expects /kek/fbank.out file. Clarify/share what output is expected, i.e. correct AM output.
The problem was due to numerical instability in our implementation of LogSoftmax. Without log of softmax all works fine.
I am learning python.I want to calculate correlation between values.Below is my data which is a dictionary.
My_data = {1: [1450.0, -80.0, 840.0, -220.0, 630.0, 780.0, -1140.0], 2: [1450.0, -80.0, 840.0, -220.0, 630.0, 780.0, -1140.0],3:[ 720.0, -230.0, 460.0, 220.0, 710.0, -460.0, 90.0] }
This is what I expect to have in return.
1 2 3
1 1 0.69 0.77
2 1 0.54
3 1
This is the code I tried. I get TypeError:unsupported operand type(s) for /: 'list' and 'long'
I am not sure what went wrong. I would appreciate if somebody explains me and help me get the desired solution.
my_array=np.array(My_data .values())
Correlation = np.corrcoef(my_array,my_array)
Case 1: if you are open to use pandas
Using pandas (which is a wrapper of numpy), you can porceed as follows:
In [55]: import pandas as pd
In [56]: df = pd.DataFrame.from_dict(My_data, orient='index').T
In [57]: df.corr(method='pearson')
Out[57]:
1 2 3
1 1.000000 1.000000 0.384781
2 1.000000 1.000000 0.121978
3 0.384781 0.121978 1.000000
In [58]: df.corr(method='kendall')
Out[58]:
1 2 3
1 1.000000 1.000000 0.333333
2 1.000000 1.000000 0.240385
3 0.333333 0.240385 1.000000
In [59]: df.corr(method='spearman')
Out[59]:
1 2 3
1 1.000000 1.00000 0.464286
2 1.000000 1.00000 0.327370
3 0.464286 0.32737 1.000000
In [60]:
Explanation:
The following line creates a pandas.DataFrame from the dictionary My_data
df = pd.DataFrame.from_dict(My_data, orient='index').T
Which looks like this:
In [60]: df
Out[60]:
1 2 3
0 1450.0 1450.0 720.0
1 -80.0 -80.0 -230.0
2 840.0 840.0 460.0
3 -220.0 -220.0 220.0
4 630.0 630.0 710.0
5 780.0 780.0 -460.0
6 -1140.0 -1140.0 90.0
7 NaN 450.0 -640.0
8 NaN 730.0 870.0
9 NaN -810.0 -290.0
10 NaN 390.0 -2180.0
11 NaN -220.0 -790.0
12 NaN -1640.0 65.0
13 NaN -590.0 70.0
14 NaN -145.0 460.0
15 NaN -420.0 NaN
16 NaN 620.0 NaN
17 NaN 450.0 NaN
18 NaN -90.0 NaN
19 NaN 990.0 NaN
20 NaN -705.0 NaN
then df.corr() will compute the pairwise correlation between columns.
Case 2: if you want a pure numpy solution
You need to convert your data into numpy.ndarray first, then you can compute the correlation like this,
In [91]: np.corrcoef(np.asarray(new_data.values()))
Out[91]:
array([[ 1. , 1. , 0.38478131],
[ 1. , 1. , 0.38478131],
[ 0.38478131, 0.38478131, 1. ]])
In [92]:
I have written a piece of code to plot a graph using GNUplot from a data file.
It's giving a warning:
warning: Skipping data file with no valid points
The code is:
{
FILE *gnuplotPipe, *tempDataFile;
FILE * pFile;
char *tempDataFileName;
char *Datafile;
double x, y;
int i;
tempDataFileName = "Pulse.txt";
Datafile = "PulseFinal.dat";
gnuplotPipe = _popen("gnuplot", "w");
if (gnuplotPipe)
{
fprintf(gnuplotPipe, "plot \"%s\" '-' using 1:2 with lines", Datafile);
fflush(gnuplotPipe);
printf("press enter to continue...");
getchar();
fprintf(gnuplotPipe, "exit \n");
}
else
{
printf("gnuplot not found...");
}
}
The data file is:
0.000000 0.018519
1.000000 0.000000
2.000000 0.000000
3.000000 0.000000
4.000000 0.000000
5.000000 0.000000
6.000000 0.000000
7.000000 0.000000
8.000000 0.000000
9.000000 0.000000
10.000000 -0.006173
Can someone please help me with this?
You have tried
plot \"%s\" '-' using 1:2 with lines
which means for Gnuplot
plot "YPulseFinal.dat" '-' using 1:2 with lines
You can't plot a file and the stream at the same time. You can
plot "YPulseFinal.dat" using 1:2 with lines
or
plot '-' using 1:2 with lines
I recommend you
fprintf(gnuplotPipe, "plot \"%s\" using 1:2 with lines", Datafile);
I need to convert 2d planar polygonal meshes to 2D Arrangements in CGAL. for example if I have the following mesh in Wavefront obj format:
v -5.687006 -4.782805 0.000000
v 4.878987 -4.782805 0.000000
v -5.687006 4.782805 0.000000
v 4.878987 4.782805 0.000000
v -0.404010 -4.782805 0.000000
v -5.687006 0.000000 0.000000
v 4.878987 0.000000 0.000000
v -0.404010 4.782805 0.000000
v -0.404010 0.000000 0.000000
f 5 2 9
f 9 2 7
f 7 4 9
f 9 4 8
f 8 3 9
f 9 3 6
f 6 1 9
f 9 1 5
what is the simplest way I could convert it to a 2d Arrangement using the CGAL library?
Using the following example, you'll find out.
insert_in_face_interior for the first segment
insert_from_left_vertex or insert_from_right_vertex for the middle one, depending on the orientation of your polygon.
insert_at_vertices for the last one