<commit_before>#include "std_incl.h"
#include "ResultManager.h"
#include "utils.h"

ResultManager::ResultManager(const char *outfile, const char* frameInfoFile, ResultManagerConfig *cfg)
{
	config = *cfg;
	outputFile = outfile;
	this->frameInfoFile = frameInfoFile;

	startFrame = 0;
	lastSaveFrame = 0;
	processedFrames = 0;
	capturedFrames = 0;
	localizationsDone = 0;

	qtrk = 0;

	thread = Threads::Create(ThreadLoop, this);
	quit=false;

	remove(outfile);
	remove(frameInfoFile);

	dbgprintf("Allocating ResultManager with %d beads and writeinterval %d\n", cfg->numBeads, cfg->writeInterval);
}

ResultManager::~ResultManager()
{
	quit = true;
	Threads::WaitAndClose(thread);

	DeleteAllElems(frameResults);
}


void ResultManager::StoreResult(LocalizationResult *r)
{
	int index = r->job.frame - startFrame;

	if (index >= frameResults.size()) {
		dbgprintf("dropping result. Result provided for unregistered frame %d. Current frames registered: %d\n", 
			r->job.frame, startFrame + frameResults.size());
		return; // add errors?
	}

	LocalizationResult scaled = *r;
	// Make roi-centered pos
	scaled.pos = scaled.pos - vector3f( qtrk->cfg.width*0.5f, qtrk->cfg.height*0.5f, 0);
	scaled.pos = ( scaled.pos + config.offset ) * config.scaling;
	FrameResult* fr = frameResults[index];
	fr->results[r->job.zlutIndex] = scaled;
	fr->count++;

	// Advance fullFrames
	frameCountMutex.lock();
	while (processedFrames - startFrame < frameResults.size() && frameResults[processedFrames-startFrame]->count == config.numBeads)
		processedFrames ++;
	localizationsDone ++;
	frameCountMutex.unlock();
}

void ResultManager::Write()
{
	FILE* f = fopen(outputFile.c_str(), "a");
	FILE* finfo = fopen(frameInfoFile.c_str(), "a");
	
	resultMutex.lock();
	if (config.binaryOutput) {
		for (uint j=lastSaveFrame; j<processedFrames;j++)
		{
			auto fr = frameResults[j-startFrame];
			fwrite(&j, sizeof(uint), 1, f);
			fwrite(&fr->timestamp, sizeof(double), 1, f);
			fwrite(&fr->timestamp, sizeof(double), 1, finfo);
			for (int i=0;i<config.numBeads;i++) 
			{
				LocalizationResult *r = &fr->results[i];
				fwrite(&r->pos, sizeof(vector3f), 1, f);
			}
		}
	}
	else {
		for (uint k=lastSaveFrame; k<processedFrames;k++)
		{
			auto fr = frameResults[k-startFrame];
			fprintf(f,"%d\t%f\t", k, fr->timestamp);
			fprintf(finfo,"%d\t%f\t", k, fr->timestamp);
			for (int i=0;i<config.numFrameInfoColumns;i++)
				fprintf(finfo, "%.5f\t", fr->frameInfo[i]);
			fputs("\n", finfo);
			for (int i=0;i<config.numBeads;i++) 
			{
				LocalizationResult *r = &fr->results[i];
				fprintf(f, "%.5f\t%.5f\t%.5f\t", r->pos.x,r->pos.y,r->pos.z);
			}
			fputs("\n", f);
		}
	}

	dbgprintf("Saved frame %d to %d\n", lastSaveFrame, processedFrames);

	fclose(f);
	fclose(finfo);
	frameCountMutex.lock();
	lastSaveFrame = processedFrames;
	frameCountMutex.unlock();

	resultMutex.unlock();<commit_msg>C++: ResultManager: Test for valid output filename / frame info filename Fix OfflineQTrk build for new result manager<commit_after>#include "std_incl.h"
#include "ResultManager.h"
#include "utils.h"

ResultManager::ResultManager(const char *outfile, const char* frameInfoFile, ResultManagerConfig *cfg)
{
	config = *cfg;
	outputFile = outfile;
	this->frameInfoFile = frameInfoFile;

	startFrame = 0;
	lastSaveFrame = 0;
	processedFrames = 0;
	capturedFrames = 0;
	localizationsDone = 0;

	qtrk = 0;

	thread = Threads::Create(ThreadLoop, this);
	quit=false;

	remove(outfile);
	remove(frameInfoFile);

	dbgprintf("Allocating ResultManager with %d beads and writeinterval %d\n", cfg->numBeads, cfg->writeInterval);
}

ResultManager::~ResultManager()
{
	quit = true;
	Threads::WaitAndClose(thread);

	DeleteAllElems(frameResults);
}


void ResultManager::StoreResult(LocalizationResult *r)
{
	int index = r->job.frame - startFrame;

	if (index >= frameResults.size()) {
		dbgprintf("dropping result. Result provided for unregistered frame %d. Current frames registered: %d\n", 
			r->job.frame, startFrame + frameResults.size());
		return; // add errors?
	}

	LocalizationResult scaled = *r;
	// Make roi-centered pos
	scaled.pos = scaled.pos - vector3f( qtrk->cfg.width*0.5f, qtrk->cfg.height*0.5f, 0);
	scaled.pos = ( scaled.pos + config.offset ) * config.scaling;
	FrameResult* fr = frameResults[index];
	fr->results[r->job.zlutIndex] = scaled;
	fr->count++;

	// Advance fullFrames
	frameCountMutex.lock();
	while (processedFrames - startFrame < frameResults.size() && frameResults[processedFrames-startFrame]->count == config.numBeads)
		processedFrames ++;
	localizationsDone ++;
	frameCountMutex.unlock();
}

void ResultManager::Write()
{
	FILE* f = outputFile.empty () ? 0 : fopen(outputFile.c_str(), "a");
	FILE* finfo = frameInfoFile.empty() ? 0 : fopen(frameInfoFile.c_str(), "a");
	
	resultMutex.lock();
	if (config.binaryOutput) {
		for (uint j=lastSaveFrame; j<processedFrames;j++)
		{
			auto fr = frameResults[j-startFrame];
			if (f) {
				fwrite(&j, sizeof(uint), 1, f);
				fwrite(&fr->timestamp, sizeof(double), 1, f);
				for (int i=0;i<config.numBeads;i++) 
				{
					LocalizationResult *r = &fr->results[i];
					fwrite(&r->pos, sizeof(vector3f), 1, f);
				}
			}
			if (finfo)
				fwrite(&fr->timestamp, sizeof(double), 1, finfo);
		}
	}
	else {
		for (uint k=lastSaveFrame; k<processedFrames;k++)
		{
			auto fr = frameResults[k-startFrame];
			if (f) {
				fprintf(f,"%d\t%f\t", k, fr->timestamp);
				for (int i=0;i<config.numBeads;i++) 
				{
					LocalizationResult *r = &fr->results[i];
					fprintf(f, "%.5f\t%.5f\t%.5f\t", r->pos.x,r->pos.y,r->pos.z);
				}
				fputs("\n", f);
			}
			if (finfo) {
				fprintf(finfo,"%d\t%f\t", k, fr->timestamp);
				for (int i=0;i<config.numFrameInfoColumns;i++)
					fprintf(finfo, "%.5f\t", fr->frameInfo[i]);
				fputs("\n", finfo);
			}
		}
	}

	dbgprintf("Saved frame %d to %d\n", lastSaveFrame, processedFrames);

	if(f) fclose(f);
	if(finfo) fclose(finfo);
	frameCountMutex.lock();
	lastSaveFrame = processedFrames;
	frameCountMutex.unlock();

	resultMutex.unlock();
{'fraction_non_alphanumeric': 0.17026578073089702, 'fraction_numerical': 0.008305647840531562, 'mean_word_length': 4.434115523465704, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942799', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2217, 'n_words': 437}