Last active
January 16, 2018 18:03
-
-
Save marenaud/f1c6ed220e63a5939b0d9d32530bebcd to your computer and use it in GitHub Desktop.
Loading doses in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <fstream> | |
#include <iostream> | |
#include <chrono> | |
#include <string.h> | |
#include <stdlib.h> | |
class Dose | |
{ | |
public: | |
Dose(){}; | |
void print_statistics() { | |
int num_nonzero = 0; | |
float avg_dose = 0.0; | |
float avg_uncert = 0.0; | |
for (size_t i = 0; i < this->dose.size(); i++) { | |
if (this->dose[i] > 0.0) { | |
num_nonzero += 1; | |
avg_dose += this->dose[i]; | |
avg_uncert += this->uncerts[i]; | |
} | |
} | |
avg_dose /= num_nonzero; | |
avg_uncert /= num_nonzero; | |
std::cout << "Number of nonzero dose voxels: " << num_nonzero << std::endl; | |
std::cout << "Average dose of nonzero voxels: " << avg_dose << std::endl; | |
std::cout << "Average uncert of nonzero voxels: " << avg_uncert << std::endl; | |
} | |
void from_3ddose(std::string filename) | |
{ | |
char *token; | |
char *buf_dup; | |
std::string buffer; | |
std::cout << "Loading " << filename << std::endl; | |
std::ifstream dose_file(filename); | |
std::getline(dose_file, buffer); // number of voxels | |
sscanf(buffer.c_str(), "%i %i %i", &num_voxels[0], &num_voxels[1], &num_voxels[2]); | |
std::getline(dose_file, buffer); // x voxel coordinates | |
std::getline(dose_file, buffer); // y voxel coordinates | |
std::getline(dose_file, buffer); // z voxel coordinates | |
std::getline(dose_file, buffer); // Dose values | |
size_t total_vox = this->num_voxels[0] * this->num_voxels[1] * this->num_voxels[2]; | |
this->dose = std::vector<float>(total_vox, 0.0); | |
this->uncerts = std::vector<float>(total_vox, 0.99); | |
buf_dup = strdup(buffer.c_str()); | |
size_t idx = 0; | |
token = strtok(buf_dup, " "); | |
while (token != NULL) | |
{ | |
this->dose[idx] = atof(token); | |
token = strtok(NULL, " "); | |
idx += 1; | |
} | |
free(buf_dup); | |
std::getline(dose_file, buffer); // Uncert values | |
buf_dup = strdup(buffer.c_str()); | |
idx = 0; | |
token = strtok(buf_dup, " "); | |
while (token != NULL) | |
{ | |
this->uncerts[idx] = atof(token); | |
token = strtok(NULL, " "); | |
idx += 1; | |
} | |
free(buf_dup); | |
dose_file.close(); | |
} | |
void from_bindos(std::string filename) | |
{ | |
int num_nonzero; | |
std::cout << "Loading " << filename << std::endl; | |
std::ifstream dose_file(filename, std::ios::in | std::ios::binary); | |
dose_file.read((char *)this->num_voxels, 3 * sizeof(int)); | |
size_t num_ignore = this->num_voxels[0] + this->num_voxels[1] + this->num_voxels[2] + 3; | |
dose_file.ignore(num_ignore * sizeof(float)); | |
dose_file.read((char *)&num_nonzero, sizeof(int)); | |
int *voxels = new int[num_nonzero]; | |
float *nonzero_doses = new float[num_nonzero]; | |
float *nonzero_uncerts = new float[num_nonzero]; | |
dose_file.read((char *)voxels, num_nonzero * sizeof(int)); | |
dose_file.read((char *)nonzero_doses, num_nonzero * sizeof(float)); | |
dose_file.read((char *)nonzero_uncerts, num_nonzero * sizeof(float)); | |
size_t total_vox = this->num_voxels[0] * this->num_voxels[1] * this->num_voxels[2]; | |
this->dose = std::vector<float>(total_vox, 0.0); | |
this->uncerts = std::vector<float>(total_vox, 0.99); | |
for (int i = 0; i < num_nonzero; i++) | |
{ | |
this->dose[voxels[i]] = nonzero_doses[i]; | |
this->uncerts[voxels[i]] = nonzero_uncerts[i]; | |
} | |
dose_file.close(); | |
delete[] voxels; | |
delete[] nonzero_doses; | |
delete[] nonzero_uncerts; | |
} | |
void from_randydos(std::string filename) | |
{ | |
int num_nonzero; | |
int num_blocks; | |
std::cout << "Loading " << filename << std::endl; | |
std::ifstream dose_file(filename, std::ios::in | std::ios::binary); | |
dose_file.read((char *)this->num_voxels, 3 * sizeof(int)); | |
size_t num_ignore = this->num_voxels[0] + this->num_voxels[1] + this->num_voxels[2] + 3; | |
dose_file.ignore(num_ignore * sizeof(float)); | |
dose_file.read((char *)&num_nonzero, sizeof(int)); | |
dose_file.read((char *)&num_blocks, sizeof(int)); | |
int *blocks = new int[2*num_blocks]; | |
float *nonzero_doses = new float[num_nonzero]; | |
float *nonzero_uncerts = new float[num_nonzero]; | |
dose_file.read((char *)blocks, 2 * num_blocks * sizeof(int)); | |
dose_file.read((char *)nonzero_doses, num_nonzero * sizeof(float)); | |
dose_file.read((char *)nonzero_uncerts, num_nonzero * sizeof(float)); | |
size_t total_vox = this->num_voxels[0] * this->num_voxels[1] * this->num_voxels[2]; | |
this->dose = std::vector<float>(total_vox, 0.0); | |
this->uncerts = std::vector<float>(total_vox, 0.99); | |
int voxels_processed = 0; | |
for (int i = 0; i < 2 * num_blocks; i += 2) | |
{ | |
for (int j = blocks[i]; j < blocks[i+1]; j++) { | |
this->dose[j] = nonzero_doses[voxels_processed]; | |
this->uncerts[j] = nonzero_uncerts[voxels_processed]; | |
voxels_processed += 1; | |
} | |
} | |
dose_file.close(); | |
delete[] blocks; | |
delete[] nonzero_doses; | |
delete[] nonzero_uncerts; | |
} | |
private: | |
int num_voxels[3]; | |
std::vector<float> dose; | |
std::vector<float> uncerts; | |
}; | |
int main() { | |
std::chrono::steady_clock::time_point start_time; | |
std::chrono::steady_clock::time_point end; | |
int elapsed; | |
start_time = std::chrono::steady_clock::now(); | |
Dose dose; | |
dose.from_3ddose("dummy.3ddose"); | |
//dose.print_statistics(); | |
end = std::chrono::steady_clock::now(); | |
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_time).count(); | |
std::cout << "Time to load 3ddose: " << elapsed << " ms" << std::endl << std::endl; | |
start_time = std::chrono::steady_clock::now(); | |
Dose dose2; | |
dose2.from_bindos("dummy.bindos"); | |
//dose2.print_statistics(); | |
end = std::chrono::steady_clock::now(); | |
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_time).count(); | |
std::cout << "Time to load bindos: " << elapsed << " ms" << std::endl << std::endl; | |
start_time = std::chrono::steady_clock::now(); | |
Dose dose3; | |
dose3.from_randydos("dummy.randydos"); | |
//dose3.print_statistics(); | |
end = std::chrono::steady_clock::now(); | |
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_time).count(); | |
std::cout << "Time to load randydos: " << elapsed << " ms" << std::endl << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment