#! /usr/bin/gawk -f # Last edited on 2020-12-27 07:23:28 by jstolfi BEGIN \ { # Reads Nilton Kamiji's network description provided to Jorge Stolfi on 2020-12-03. # User must define with "-v" the variable {nne}, the expected number of # neurons in the network. # Writes to stdout a table with the outdegree (number of output synapses) # of each neuron. Each line has two entries "{ine} {nse_out}" where # {ine} is a neuron index (from 0) and {nse_out} is the outdegree. abort = -1; if (nne == "") { arg_error("must define {nne}"); } else { nne += 0 } split("", nse_out_per_ne) ine_max = -1 # Max neuron index seen. # Start scanning the synapses: nse_read = 0; } (abort >= 0) { exit(abort); } /N_layers/ { next; } /^ *[0-9]+ +[0-9]+ *$/ { next; } /^ *$/ { next; } /^ *[0-9]+[,] *[0-9]+[,] *[-.0-9]+[,] *[.0-9]+ *$/ \ { gsub(/^ +/, "", $0); gsub(/ +$/, "", $0); gsub(/ *[,] */, " ", $0); ine_pre = $1 - 1; ine_pos = $2 - 1; if ((ine_pre < 0) || (ine_pre >= nne)) { data_error(("bad ine_pre = \"" ine_pre "\""), $0); } if ((ine_pos < 0) || (ine_pos >= nne)) { data_error(("bad ine_pos = \"" ine_pos "\""), $0); } # Accumulate in table: if (! (ine_pre in nse_out_per_ne)) { nse_out_per_ne[ine_pre] = 1; } else { nse_out_per_ne[ine_pre]++; } nse_read++; # Remember largest index seen: if (ine_pre > ine_max) { ine_max = ine_pre; } if (ine_pos > ine_max) { ine_max = ine_pos; } next } // { data_error("bad line format", $0); } END \ { if (abort >= 0) { exit(abort); } if (ine_max != nne-1) { data_error(("wrong max neuron index = \"" ine_max "\""), ""); } # Print outdegree table to standard output: for (ine = 0; ine < nne; ine++) { if (! (ine in nse_out_per_ne)) { data_error(("no synapses for neuron " ine), ""); } printf "%d %d\n", ine, nse_out_per_ne[ine]; } fflush() printf "done.\n" > "/dev/stderr" } function data_error(msg,lin) { printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; if (lin != "") { printf " [[%s]]\n", lin > "/dev/stderr"; } abort = 1; exit(1) }