Error in Code
Posted: Sun Oct 31, 2021 7:34 am
I am having a code that runs perfectly fine when I compile it with g++ (g++ example.cpp -o myapp), but when I try to compile it using the make command for the micropython env it gives the following error.
CODE: SELECT ALL
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted
I have provided the code below.
CODE: SELECT ALL
extern "C"{
#include <examplemodule.h>
}
mp_obj_t cppfunc() {
clock_t startTime,endTime;
startTime = clock();
vector<vector<double> > csv_data = read_csv("A00068.csv");
//printf("CSV_DATA:%d", csv_data);
//printf("rows of \"%s\" file is %d.\n",argv[1], int(csv_data.size()));
//printf("Loading the first row as ECG Series......\n");
double *ecg_series = new double[15095976];
int len = csv_data.at(0).size();
//mp_printf(&mp_plat_print, "CSV_DATA:%d", csv_data.at(1));
//get_line_from_csv(csv_data, 0, ecg_series);
//mp_printf(&mp_plat_print, "CSV_LINE:%d\n", get_line_from_csv);
double features[44];
//get_features(ecg_series,18000,features);
union Entry data[44];
float result[4];
char res[4]={'A','N','O','~'};
int index;
for(int i=0;i<44;i++)
{
data.missing=-1;
data.fvalue=features;
//printf("feature[%d]=%.8f\n",i,features);
}
//predict_multiclass(data,0,result);
index=(int)result[0];
endTime = clock();
mp_printf(&mp_plat_print, "classification result is: %C (Time cost: %.4f s)\n",res[index],(double)(endTime - startTime) / CLOCKS_PER_SEC);
return mp_const_none;
}
The line causing the error is "int len = csv_data.at(0).size();"
This is how I read the csv:
CODE: SELECT ALL
vector<vector<double> > read_csv(const char *filename)
{
ifstream read_file(filename);
vector<vector<double> > csv_data;
std::string lineStr;
while (getline(read_file, lineStr))
{
stringstream ss(lineStr);
std::string d;
vector<double> a_line_data;
while (getline(ss, d, ','))
{
a_line_data.push_back(atof(d.c_str()));
}
csv_data.push_back(a_line_data);
}
//printf("rows of csv file:%d.\n", csv_data.size());
return csv_data;
}
The error
CODE: SELECT ALL
pi@raspberrypi:~/projects/micropython/ports/unix $ ./micropython
MicroPython v1.16-222-g44818d1a3-dirty on 2021-08-26; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import cppexample
>>> cppexample.cppfunc()
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted
Command to reproduce the same:
cd to the micropython ports/unix/ folder
make clean
make -j4 USER_C_MODULES=../../examples/usercmodule
CODE: SELECT ALL
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted
I have provided the code below.
CODE: SELECT ALL
extern "C"{
#include <examplemodule.h>
}
mp_obj_t cppfunc() {
clock_t startTime,endTime;
startTime = clock();
vector<vector<double> > csv_data = read_csv("A00068.csv");
//printf("CSV_DATA:%d", csv_data);
//printf("rows of \"%s\" file is %d.\n",argv[1], int(csv_data.size()));
//printf("Loading the first row as ECG Series......\n");
double *ecg_series = new double[15095976];
int len = csv_data.at(0).size();
//mp_printf(&mp_plat_print, "CSV_DATA:%d", csv_data.at(1));
//get_line_from_csv(csv_data, 0, ecg_series);
//mp_printf(&mp_plat_print, "CSV_LINE:%d\n", get_line_from_csv);
double features[44];
//get_features(ecg_series,18000,features);
union Entry data[44];
float result[4];
char res[4]={'A','N','O','~'};
int index;
for(int i=0;i<44;i++)
{
data.missing=-1;
data.fvalue=features;
//printf("feature[%d]=%.8f\n",i,features);
}
//predict_multiclass(data,0,result);
index=(int)result[0];
endTime = clock();
mp_printf(&mp_plat_print, "classification result is: %C (Time cost: %.4f s)\n",res[index],(double)(endTime - startTime) / CLOCKS_PER_SEC);
return mp_const_none;
}
The line causing the error is "int len = csv_data.at(0).size();"
This is how I read the csv:
CODE: SELECT ALL
vector<vector<double> > read_csv(const char *filename)
{
ifstream read_file(filename);
vector<vector<double> > csv_data;
std::string lineStr;
while (getline(read_file, lineStr))
{
stringstream ss(lineStr);
std::string d;
vector<double> a_line_data;
while (getline(ss, d, ','))
{
a_line_data.push_back(atof(d.c_str()));
}
csv_data.push_back(a_line_data);
}
//printf("rows of csv file:%d.\n", csv_data.size());
return csv_data;
}
The error
CODE: SELECT ALL
pi@raspberrypi:~/projects/micropython/ports/unix $ ./micropython
MicroPython v1.16-222-g44818d1a3-dirty on 2021-08-26; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import cppexample
>>> cppexample.cppfunc()
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted
Command to reproduce the same:
cd to the micropython ports/unix/ folder
make clean
make -j4 USER_C_MODULES=../../examples/usercmodule