<commit_before>#include "pv.h"
#include <columns/PVHyperCol.h>
#include <layers/PVLayer.h>

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/**
 * Constructor for a Retina (PVLayer).
 * 
 * This layer just contains the firing events for an input image (currently a circle with clutter).
 * 
 */

PVLayer* pv_new_layer_retina(PVHyperCol* hc, int index, int nx, int ny, int no)
  {
    int k;
    float dx, dy, r, r2;

    PVLayer* l = pv_new_layer(hc, index, nx, ny, no);

    eventtype_t* f = l->f;

    char filename[75];

    // If the user passed a filename on the command line use that.
    if (hc->input_filename[0] != 0)
        strcpy(filename, hc->input_filename);
    else
        strcpy(filename, INPUT_PATH);
    
    FILE* input= fopen(filename, "rb");
    if (input == NULL)
      {
	printf("Error: input file specified was not found.\n Please make sure file exists or use an input constructor to make it.\n Using default random clutter.\n");
	int k;
	for(k=0; k < l->n_neurons; k++) 
	  {
	    const float INV_RAND_MAX = 1.0 / (float) RAND_MAX;
	    r = rand() * INV_RAND_MAX; 
	    f[k] = (r < CLUTTER_PROB) ? I_MAX : 0.0;
	  }
      }
    else
      fread(f,sizeof(eventtype_t),l->n_neurons,input);
       
    fclose(input);
return l;
  }



/* PVLayer* pv_new_layer_retina(PVHyperCol* hc, int index, int nx, int ny, int no) */
/*   { */
/*     int k; */
/*     float dx, dy, r, r2; */

/*     PVLayer* l = pv_new_layer(hc, index, nx, ny, no); */<commit_msg>Only fclose() the input file if we successfuly opened it, else may crash if the file handle is invalid.<commit_after>#include "pv.h"
#include <columns/PVHyperCol.h>
#include <layers/PVLayer.h>

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/**
 * Constructor for a Retina (PVLayer).
 * 
 * This layer just contains the firing events for an input image (currently a circle with clutter).
 * 
 */

PVLayer* pv_new_layer_retina(PVHyperCol* hc, int index, int nx, int ny, int no)
  {
    int k;
    float dx, dy, r, r2;

    PVLayer* l = pv_new_layer(hc, index, nx, ny, no);

    eventtype_t* f = l->f;

    char filename[75];

    // If the user passed a filename on the command line use that.
    if (hc->input_filename[0] != 0)
        strcpy(filename, hc->input_filename);
    else
        strcpy(filename, INPUT_PATH);
    
    FILE* input= fopen(filename, "rb");
    if (input == NULL)
      {
	printf("Error: input file specified was not found.\n Please make sure file exists or use an input constructor to make it.\n Using default random clutter.\n");
	int k;
	for(k=0; k < l->n_neurons; k++) 
	  {
	    const float INV_RAND_MAX = 1.0 / (float) RAND_MAX;
	    r = rand() * INV_RAND_MAX; 
	    f[k] = (r < CLUTTER_PROB) ? I_MAX : 0.0;
	  }
      }
    else {
      fread(f,sizeof(eventtype_t),l->n_neurons,input);
      fclose(input);
    }

return l;
  }



/* PVLayer* pv_new_layer_retina(PVHyperCol* hc, int index, int nx, int ny, int no) */
/*   { */
/*     int k; */
/*     float dx, dy, r, r2; */

/*     PVLayer* l = pv_new_layer(hc, index, nx, ny, no); */
{'fraction_non_alphanumeric': 0.14244279729294232, 'fraction_numerical': 0.007089912987431518, 'mean_word_length': 2.7807551766138854, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '2806124', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1162, 'n_words': 350}