Skip to content

Commit

Permalink
Load incompatible Mat file as weights dialog will crash GeoDa GeoDaCe…
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed Oct 29, 2020
1 parent de3a34a commit 8b1053b
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Algorithms/distmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ float* gpu_distmatrix(const char* cl_path, int rows, int columns, double** data,
fp = fopen(cl_path, "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
return 0;
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
Expand Down
33 changes: 26 additions & 7 deletions Algorithms/tsne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ void TSNE::run(boost::lockfree::queue<int>& tsne_queue,
double* dY = (double*) malloc(N * no_dims * sizeof(double));
double* uY = (double*) calloc(N * no_dims , sizeof(double));
double* gains = (double*) malloc(N * no_dims * sizeof(double));
if (dY == NULL || uY == NULL || gains == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (dY == NULL || uY == NULL || gains == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int i = 0; i < N * no_dims; i++) {
gains[i] = 1.0;
}
Expand Down Expand Up @@ -296,7 +299,8 @@ double TSNE::computeGradient(int* inp_row_P, int* inp_col_P, double* inp_val_P,
double C = 0.;

if (pos_f == NULL || neg_f == NULL) {
fprintf(stderr, "Memory allocation failed!\n"); exit(1);
fprintf(stderr, "Memory allocation failed!\n");
return 0;
}

#ifdef _OPENMP
Expand Down Expand Up @@ -401,7 +405,10 @@ void TSNE::computeGaussianPerplexity(double* X, int N, int D, int** _row_P, int*
*_row_P = (int*) malloc((N + 1) * sizeof(int));
*_col_P = (int*) calloc(N * K, sizeof(int));
*_val_P = (double*) calloc(N * K, sizeof(double));
if (*_row_P == NULL || *_col_P == NULL || *_val_P == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (*_row_P == NULL || *_col_P == NULL || *_val_P == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}

/*
row_P -- offsets for `col_P` (i)
Expand Down Expand Up @@ -534,7 +541,10 @@ void TSNE::symmetrizeMatrix(int** _row_P, int** _col_P, double** _val_P, int N)

// Count number of elements and row counts of symmetric matrix
int* row_counts = (int*) calloc(N, sizeof(int));
if (row_counts == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (row_counts == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int n = 0; n < N; n++) {
for (int i = row_P[n]; i < row_P[n + 1]; i++) {

Expand Down Expand Up @@ -563,15 +573,21 @@ void TSNE::symmetrizeMatrix(int** _row_P, int** _col_P, double** _val_P, int N)
int* sym_row_P = (int*) malloc((N + 1) * sizeof(int));
int* sym_col_P = (int*) malloc(no_elem * sizeof(int));
double* sym_val_P = (double*) malloc(no_elem * sizeof(double));
if (sym_row_P == NULL || sym_col_P == NULL || sym_val_P == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (sym_row_P == NULL || sym_col_P == NULL || sym_val_P == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}

// Construct new row indices for symmetric matrix
sym_row_P[0] = 0;
for (int n = 0; n < N; n++) sym_row_P[n + 1] = sym_row_P[n] + row_counts[n];

// Fill the result matrix
int* offset = (int*) calloc(N, sizeof(int));
if (offset == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (offset == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int n = 0; n < N; n++) {
for (int i = row_P[n]; i < row_P[n + 1]; i++) { // considering element(n, col_P[i])

Expand Down Expand Up @@ -628,7 +644,10 @@ void TSNE::zeroMean(double* X, int N, int D) {

// Compute data mean
double* mean = (double*) calloc(D, sizeof(double));
if (mean == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(1); }
if (mean == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return;
}
for (int n = 0; n < N; n++) {
for (int d = 0; d < D; d++) {
mean[d] += X[n * D + d];
Expand Down
3 changes: 2 additions & 1 deletion Regression/ML_im.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,8 @@ double goldeno(const double left, const double right, const SparseMatrix &w,
Lco(y, lag, X, w, f1, sol);
Lco(y, lag, X, w, f2, sol);
Lco(y, lag, X, w, -0.0586045, sol);
exit(0);

//exit(0);
// while (fabs(x3-x0) > tol*(fabs(x1)+fabs(x2))) { // here tol is used as RELATIVE accuracy
while (fabs(x3-x0) > TOLERANCE) { // here tol is used as an ABSOLUTE accuracy
if (f1 < f2) {
Expand Down
4 changes: 2 additions & 2 deletions Regression/SparseMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ void SparseMatrix::createGAL(const GalElement* my_gal, int obs)
int oix = row[r].getIx(cnt);
if (oix < key[0].first || oix > key[dim-1].first) {
wxMessageBox("Error: value does not exist in the weights file");
exit(0);
return;
}
int lx = ik[ oix - key[0].first ];
if (lx < 0) {
wxMessageBox("Error: value does not exist in the weights file");
exit(0);
return;
}
this->row[r].setIx(cnt, lx);
}
Expand Down
2 changes: 1 addition & 1 deletion ShapeOperations/PolysToContigWeights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void PartitionM::initIx(const int incl, const double lwr, const double upr) {
if (lower < 0 || upper > cells || incl < 0 || incl >= elements)
{
// cout << "PartM: incl= " << incl << " l= " << lwr << " " << upr;
exit(1);
return;
}


Expand Down
59 changes: 32 additions & 27 deletions io/MatfileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pair<EDataType, EMatrixClass> classify(char* data, bool endianSwap) {
if (static_cast<EDataType>(dataType) != miMATRIX)
return make_pair(static_cast<EDataType>(dataType), mxINVALID);
// miMATRIX must not be small element
assert(!isSmallDataElement);
//assert(!isSmallDataElement);

ArrayFlags* af = new ArrayFlags(data+8, endianSwap);

Expand Down Expand Up @@ -147,16 +147,13 @@ DataElement* parse(char* data, bool endianSwap) {
break;
default:
cerr << "invalid EMatrixClass!\n";
exit(1);
}
break;
default:
cerr << "invalid EDataType!\n";
exit(1);
}
if (!de) {
cerr << "failed to parse!\n";
exit(1);
}
return de;
}
Expand Down Expand Up @@ -216,14 +213,12 @@ CompressedDataElement::CompressedDataElement(char* data, bool endianSwap) :
_decompressedData = new char[size];
if (!_decompressedData) {
cerr << "FlatDataElement::parseData new failed!\n";
exit(1);
}
rsize = size;
res = uncompress(reinterpret_cast<unsigned char*>(_decompressedData), &size,
reinterpret_cast<unsigned char*>(data), _numberOfBytes);
} else {
cerr << "FlatDataElement::parseData uncompress failed!\n";
exit(1);
}
}
_decompressedSize = static_cast<uint32_t>(rsize);
Expand Down Expand Up @@ -255,7 +250,7 @@ NumericArray<T>::NumericArray(char* data, bool endianSwap) :
parseImag(data);
data += _imag->totalBytes();
}
assert(data == start+_numberOfBytes+8);
//assert(data == start+_numberOfBytes+8);
}

template <typename T>
Expand Down Expand Up @@ -283,7 +278,7 @@ SparseArray<T>::SparseArray(char* data, bool endianSwap) :
parseImag(data);
data += _imag->totalBytes();
}
assert(data == start+_numberOfBytes+8);
//assert(data == start+_numberOfBytes+8);
}

Cell::Cell(char* data, bool endianSwap) : MatrixDataElement(endianSwap), _cells() {
Expand All @@ -303,11 +298,11 @@ Cell::Cell(char* data, bool endianSwap) : MatrixDataElement(endianSwap), _cells(
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _cells
cell = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(cell);
//assert(cell);
_cells.push_back(cell);
data += cell->totalBytes();
}
assert(data == start+_numberOfBytes+8);
//assert(data == start+_numberOfBytes+8);
}

Struct::Struct(char* data, bool endianSwap) :
Expand All @@ -332,11 +327,11 @@ Struct::Struct(char* data, bool endianSwap) :
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _fields
field = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(field);
//assert(field);
_fields.push_back(field);
data += field->totalBytes();
}
assert(data == start+_numberOfBytes+8);
//assert(data == start+_numberOfBytes+8);
}

Object::Object(char* data, bool endianSwap) : _className(NULL), Struct(endianSwap) {
Expand All @@ -362,11 +357,11 @@ Object::Object(char* data, bool endianSwap) : _className(NULL), Struct(endianSwa
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _fields
field = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(field);
//assert(field);
_fields.push_back(field);
data += field->totalBytes();
}
assert(data == start+_numberOfBytes+8);
//assert(data == start+_numberOfBytes+8);
}


Expand All @@ -376,13 +371,13 @@ MatfileReader::MatfileReader(std::ifstream& inputStream)
//_inputStream.open(_matfile.c_str(), ios_base::in | ios_base::binary);
if(!_inputStream.is_open()) {
cerr << "open " << _matfile.c_str() << " error!\n";
exit(1);
}
}

MatfileReader::~MatfileReader() {
for (int i = 0; i < _dataElements.size(); ++i)
for (int i = 0; i < _dataElements.size(); ++i) {
delete _dataElements[i];
}
}

void MatfileReader::parseHeader() {
Expand All @@ -392,15 +387,16 @@ void MatfileReader::parseHeader() {
memcpy(&_subsysDataOffset, _header+116, 8);
memcpy(&_version, _header+124, 2);
_endianIndicator.assign(_header+126, 2);
if (_endianIndicator.compare("IM"))
if (_endianIndicator.compare("IM")) {
_endianSwap = true;
else
} else {
_endianSwap = false;
}
}

void MatfileReader::parseDataElement() {
bool MatfileReader::parseDataElement() {
if (_inputStream.eof())
return;
return false;
uint32_t dataType;
uint32_t numberOfBytes;
bool isSmallDataElement;
Expand All @@ -417,27 +413,36 @@ void MatfileReader::parseDataElement() {
}
char* data = NULL;
DataElement* de = NULL;
if (isSmallDataElement)
if (isSmallDataElement) {
de = parse(tag, _endianSwap);
else {
if (de == NULL) {
return false;
}
} else {
data = new char[numberOfBytes+8];
if (!data) {
cerr << "MatfileReader::parseDataElement new failed!\n";
exit(1);
//cerr << "MatfileReader::parseDataElement new failed!\n";
return false;
}
memcpy(data, tag, 8);
_inputStream.read(reinterpret_cast<char*>(data+8), numberOfBytes);
de = parse(data, _endianSwap);
}
assert(de);
//assert(de);
if (de == NULL) {
return false;
}
_dataElements.push_back(de);
// padding
if (numberOfBytes % 8)
if (numberOfBytes % 8) {
_inputStream.ignore(8 - numberOfBytes % 8);
}
return true;
}

void MatfileReader::parseAllDataElements() {
gotoData();
while (!_inputStream.eof())
while (!_inputStream.eof()) {
parseDataElement();
}
}
2 changes: 1 addition & 1 deletion io/MatfileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class MatfileReader
void gotoHeader() { _inputStream.seekg(0, ios_base::beg); }
void gotoData() { _inputStream.seekg(128, ios_base::beg); }
void parseHeader();
void parseDataElement();
bool parseDataElement();
void parseAllDataElements();
bool eof() { return _inputStream.eof(); }

Expand Down
12 changes: 7 additions & 5 deletions io/matlab_mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ GalElement* ReadMatAsGal(const wxString& fname, TableInterface* table_int)
}
MatfileReader mmr(istream);
mmr.parseHeader();
// cout << mmr->descriptiveText() << endl;
// cout << mmr->subsysDataOffset() << endl;
// cout << mmr->version() << endl;
// cout << mmr->endianIndicator() << endl;
//cout << mmr.descriptiveText() << endl;
//cout << mmr.subsysDataOffset() << endl;
//cout << mmr.version() << endl;
//cout << mmr.endianIndicator() << endl;
mmr.gotoData();
mmr.parseDataElement();
if (mmr.parseDataElement() == false) {
throw WeightsNotValidException();
}
vector<DataElement*> des = mmr.dataElements();
// cout << des[0]->dataType() << endl;
DataElement* de = des[0];
Expand Down
Loading

0 comments on commit 8b1053b

Please sign in to comment.