<commit_before>#include <stdio.h>
#include <stdlib.h>

#include "globals.h"
#include "sn.h"

static void grd_init(struct grid *g,
		double xmin, double xmax, double ymin, double ymax,
		const struct grid *parent)
{
	g->xmin = xmin;
	g->xmax = xmax;
	g->ymin = ymin;
	g->ymax = ymax;
	g->cells = NULL;
	g->Nu = 0;
	g->n = g->s = 0;
	g->parent = parent;
}

static void grd_add_point(const struct sensor_network *sn, struct grid *g, int ix)
{
	g->n++;
	g->s += sn->sensors[ix].val;
}

static void grd_print_cell_walls(const struct grid *g, FILE *f, int depth)
{
	int i;

	fprintf(f, "%5.2lf %5.2lf %5.2lf 0\n", g->xmin, g->ymin, g->xmax - g->xmin);
	fprintf(f, "%5.2lf %5.2lf 0 %5.2lf\n", g->xmin, g->ymin, g->ymax - g->ymin);
	fprintf(f, "%5.2lf %5.2lf %5.2lf 0\n", g->xmin, g->ymax, g->xmax - g->xmin);
	fprintf(f, "%5.2lf %5.2lf 0 %5.2lf\n", g->xmax, g->ymin, g->ymax - g->ymin);

	for (i = 0; i < g->Nu*g->Nu && depth > 0; i++)
		grd_print_cell_walls(&g->cells[i], f, depth - 1);
}

static void grd_print_cell_vals(const struct grid *g, FILE *f, int depth)
{
	double x, y;

	if (depth > 0){
		int i;
		for (i = 0; i < g->Nu*g->Nu && depth > 0; i++)
			grd_print_cell_vals(&g->cells[i], f, depth - 1);
		return;
	}

	if (depth < 0)
		return;

	x = (g->xmin + g->xmax)/2;
	y = (g->ymin + g->ymax)/2;
	fprintf(f, "%5.2lf %5.2lf %d %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf\n",
			x, y, g->n, g->s,
			g->n_star.val, g->n_star.var, g->s_star.val, g->s_star.var,
			g->n_ave.val, g->n_ave.var, g->s_ave.val, g->s_ave.var,
			g->n_bar.val, g->n_bar.var, g->s_bar.val, g->s_bar.var);
}

static struct noisy_val nv_average2(struct noisy_val a, struct noisy_val b)
{
	double alpha = b.var / (a.var + b.var);
	struct noisy_val ret;

	ret.val = alpha * a.val + (1 - alpha) * b.val;
	ret.var = alpha * a.var;

	return ret;
}

int sensor_cmp(const void *a, const void *b)
{
	const struct sensor *sa = a, *sb = b;
	int try = double_cmp(&sa->x, &sb->x);
	if (try) return try;
	return double_cmp(&sa->y, &sb->y);
}

void sn_convert_to_grid_root(const struct sensor_network *sn, struct grid *g)
{
	int i;
	grd_init(g, sn->xmin, sn->xmax, sn->ymin, sn->ymax, NULL);

	for (i = 0; i < sn->num_s; i++)
		grd_add_point(sn, g, i);
}

void sn_read_from_file(const char *fname, struct sensor_network *sn)
{
	FILE *f = fopen(fname, "r");
	int i;

	if (!f)
		die("Invalid db filename %s", fname);

	if(fscanf(f, "%lf%lf%lf%lf%lf%lf%d",
			&sn->xmin, &sn->ymin, &sn->xmax, &sn->ymax,
			&sn->M, &sn->theta, &sn->num_s) != 7)
		die("Cannot parse header");

	sn->sensors = calloc(sn->num_s, sizeof(sn->sensors[0]));
	if (!sn->sensors)
		die("Invalid count of sensors %d sz=%lu", sn->num_s, sn->num_s * sizeof(sn->sensors[0]));

	for (i = 0; i < sn->num_s; i++)
		if (fscanf(f, "%lf%lf%lf", &sn->sensors[i].x,
				&sn->sensors[i].y, &sn->sensors[i].val) != 3)
			break;

	if (i != sn->num_s) {
		printf("File truncated, read %d of %d sensors\n", i, sn->num_s);
		sn->num_s = i;
	}

	qsort(sn->sensors, sn->num_s, sizeof(sn->sensors[0]), sensor_cmp);
	fclose(f);
}

void sn_cleanup(const struct sensor_network *sn)
{
	free(sn->sensors);
}

void grd_debug(const struct sensor_network *sn, const struct grid *g, FILE *f, int depth)
{
	int i;

	/* sensors first */
	fprintf(f, "# sensors: x y val\n");
	for (i = 0; i < sn->num_s; i++)
		fprintf(f, "%5.2lf %5.2lf %5.2lf\n", sn->sensors[i].x,
				sn->sensors[i].y, sn->sensors[i].val);

	/* new region in datafile */
	fprintf(f, "\n\n");

	/* grid cell walls */
	fprintf(f, "# grid lines: x y dx dy\n");
	grd_print_cell_walls(g, f, depth);

	/* new region in datafile */
	fprintf(f, "\n\n");

	/* grid cell values */
	fprintf(f, "# cell values: x y n s n* vn* s* vs* n' vn' s' vs' n^ vn^ s^ vs^\n");
	grd_print_cell_vals(g, f, depth);
}

void grd_compute_noisy(const struct sensor_network *sn, struct grid *g,
		double epsilon, double beta, struct drand48_data *buffer)
{
	double epsilon_n, epsilon_s;

	epsilon_n = beta * epsilon;
	epsilon_s = epsilon - epsilon_n;

	g->n_star.val = laplace_mechanism(g->n, epsilon_n, 1, buffer);
	g->s_star.val = laplace_mechanism(g->n, epsilon_s, sn->M, buffer);

	g->n_star.var = 2 / (epsilon_n * epsilon_n);
	g->s_star.var = 2 / (epsilon_s * epsilon_s);

	/* TODO: add negativity reduction models (see (83)):
	 *  .* -> h_t*.* -> .' + variance computation
	 *  .* ->  |.*|  -> .' + variance computation
	 */
}

void grd_split_cells(const struct sensor_network *sn, struct grid *g)
{
	double *xlimits, *ylimits, xdelta, ydelta;
	struct sensor tmp_s;
	int i, j, k, st, en;

	xdelta = (g->xmax - g->xmin) / g->Nu;
	ydelta = (g->ymax - g->ymin) / g->Nu;

	xlimits = calloc(1 + g->Nu, sizeof(xlimits[0]));
	if (!xlimits) die("Out of memory %d sz=%lu", 1 + g->Nu, (1 + g->Nu) * sizeof(xlimits[0]));
	ylimits = calloc(1 + g->Nu, sizeof(ylimits[0]));
	if (!ylimits) die("Out of memory %d sz=%lu", 1 + g->Nu, (1 + g->Nu) * sizeof(ylimits[0]));

	for (i = 0; i < g->Nu; i++) {
		xlimits[i] = g->xmin + i * xdelta;
		ylimits[i] = g->ymin + i * ydelta;
	}
	xlimits[g->Nu] = g->xmax;
	ylimits[g->Nu] = g->ymax;<commit_msg>Remove TODO<commit_after>#include <stdio.h>
#include <stdlib.h>

#include "globals.h"
#include "sn.h"

static void grd_init(struct grid *g,
		double xmin, double xmax, double ymin, double ymax,
		const struct grid *parent)
{
	g->xmin = xmin;
	g->xmax = xmax;
	g->ymin = ymin;
	g->ymax = ymax;
	g->cells = NULL;
	g->Nu = 0;
	g->n = g->s = 0;
	g->parent = parent;
}

static void grd_add_point(const struct sensor_network *sn, struct grid *g, int ix)
{
	g->n++;
	g->s += sn->sensors[ix].val;
}

static void grd_print_cell_walls(const struct grid *g, FILE *f, int depth)
{
	int i;

	fprintf(f, "%5.2lf %5.2lf %5.2lf 0\n", g->xmin, g->ymin, g->xmax - g->xmin);
	fprintf(f, "%5.2lf %5.2lf 0 %5.2lf\n", g->xmin, g->ymin, g->ymax - g->ymin);
	fprintf(f, "%5.2lf %5.2lf %5.2lf 0\n", g->xmin, g->ymax, g->xmax - g->xmin);
	fprintf(f, "%5.2lf %5.2lf 0 %5.2lf\n", g->xmax, g->ymin, g->ymax - g->ymin);

	for (i = 0; i < g->Nu*g->Nu && depth > 0; i++)
		grd_print_cell_walls(&g->cells[i], f, depth - 1);
}

static void grd_print_cell_vals(const struct grid *g, FILE *f, int depth)
{
	double x, y;

	if (depth > 0){
		int i;
		for (i = 0; i < g->Nu*g->Nu && depth > 0; i++)
			grd_print_cell_vals(&g->cells[i], f, depth - 1);
		return;
	}

	if (depth < 0)
		return;

	x = (g->xmin + g->xmax)/2;
	y = (g->ymin + g->ymax)/2;
	fprintf(f, "%5.2lf %5.2lf %d %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf %5.2lf\n",
			x, y, g->n, g->s,
			g->n_star.val, g->n_star.var, g->s_star.val, g->s_star.var,
			g->n_ave.val, g->n_ave.var, g->s_ave.val, g->s_ave.var,
			g->n_bar.val, g->n_bar.var, g->s_bar.val, g->s_bar.var);
}

static struct noisy_val nv_average2(struct noisy_val a, struct noisy_val b)
{
	double alpha = b.var / (a.var + b.var);
	struct noisy_val ret;

	ret.val = alpha * a.val + (1 - alpha) * b.val;
	ret.var = alpha * a.var;

	return ret;
}

int sensor_cmp(const void *a, const void *b)
{
	const struct sensor *sa = a, *sb = b;
	int try = double_cmp(&sa->x, &sb->x);
	if (try) return try;
	return double_cmp(&sa->y, &sb->y);
}

void sn_convert_to_grid_root(const struct sensor_network *sn, struct grid *g)
{
	int i;
	grd_init(g, sn->xmin, sn->xmax, sn->ymin, sn->ymax, NULL);

	for (i = 0; i < sn->num_s; i++)
		grd_add_point(sn, g, i);
}

void sn_read_from_file(const char *fname, struct sensor_network *sn)
{
	FILE *f = fopen(fname, "r");
	int i;

	if (!f)
		die("Invalid db filename %s", fname);

	if(fscanf(f, "%lf%lf%lf%lf%lf%lf%d",
			&sn->xmin, &sn->ymin, &sn->xmax, &sn->ymax,
			&sn->M, &sn->theta, &sn->num_s) != 7)
		die("Cannot parse header");

	sn->sensors = calloc(sn->num_s, sizeof(sn->sensors[0]));
	if (!sn->sensors)
		die("Invalid count of sensors %d sz=%lu", sn->num_s, sn->num_s * sizeof(sn->sensors[0]));

	for (i = 0; i < sn->num_s; i++)
		if (fscanf(f, "%lf%lf%lf", &sn->sensors[i].x,
				&sn->sensors[i].y, &sn->sensors[i].val) != 3)
			break;

	if (i != sn->num_s) {
		printf("File truncated, read %d of %d sensors\n", i, sn->num_s);
		sn->num_s = i;
	}

	qsort(sn->sensors, sn->num_s, sizeof(sn->sensors[0]), sensor_cmp);
	fclose(f);
}

void sn_cleanup(const struct sensor_network *sn)
{
	free(sn->sensors);
}

void grd_debug(const struct sensor_network *sn, const struct grid *g, FILE *f, int depth)
{
	int i;

	/* sensors first */
	fprintf(f, "# sensors: x y val\n");
	for (i = 0; i < sn->num_s; i++)
		fprintf(f, "%5.2lf %5.2lf %5.2lf\n", sn->sensors[i].x,
				sn->sensors[i].y, sn->sensors[i].val);

	/* new region in datafile */
	fprintf(f, "\n\n");

	/* grid cell walls */
	fprintf(f, "# grid lines: x y dx dy\n");
	grd_print_cell_walls(g, f, depth);

	/* new region in datafile */
	fprintf(f, "\n\n");

	/* grid cell values */
	fprintf(f, "# cell values: x y n s n* vn* s* vs* n' vn' s' vs' n^ vn^ s^ vs^\n");
	grd_print_cell_vals(g, f, depth);
}

void grd_compute_noisy(const struct sensor_network *sn, struct grid *g,
		double epsilon, double beta, struct drand48_data *buffer)
{
	double epsilon_n, epsilon_s;

	epsilon_n = beta * epsilon;
	epsilon_s = epsilon - epsilon_n;

	g->n_star.val = laplace_mechanism(g->n, epsilon_n, 1, buffer);
	g->s_star.val = laplace_mechanism(g->n, epsilon_s, sn->M, buffer);

	g->n_star.var = 2 / (epsilon_n * epsilon_n);
	g->s_star.var = 2 / (epsilon_s * epsilon_s);
}

void grd_split_cells(const struct sensor_network *sn, struct grid *g)
{
	double *xlimits, *ylimits, xdelta, ydelta;
	struct sensor tmp_s;
	int i, j, k, st, en;

	xdelta = (g->xmax - g->xmin) / g->Nu;
	ydelta = (g->ymax - g->ymin) / g->Nu;

	xlimits = calloc(1 + g->Nu, sizeof(xlimits[0]));
	if (!xlimits) die("Out of memory %d sz=%lu", 1 + g->Nu, (1 + g->Nu) * sizeof(xlimits[0]));
	ylimits = calloc(1 + g->Nu, sizeof(ylimits[0]));
	if (!ylimits) die("Out of memory %d sz=%lu", 1 + g->Nu, (1 + g->Nu) * sizeof(ylimits[0]));

	for (i = 0; i < g->Nu; i++) {
		xlimits[i] = g->xmin + i * xdelta;
		ylimits[i] = g->ymin + i * ydelta;
	}
	xlimits[g->Nu] = g->xmax;
	ylimits[g->Nu] = g->ymax;
{'fraction_non_alphanumeric': 0.2162296384584823, 'fraction_numerical': 0.020460866110448946, 'mean_word_length': 3.453339230429014, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 249, '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': '24532', 'n_tokens_mistral': 4948, 'n_tokens_neox': 4608, 'n_words': 1134}