예제 #1
0
int
main(int argc, char **argv)
{
  long seed = 1111;
  FILE *inFile,*outFile;
  struct node_gra *net = NULL;
  struct node_gra *netran = NULL;
  struct node_gra *p = NULL;
  int S;
  int c;
  struct group *part = NULL;
  struct group *roles = NULL;
  int i,t1;
  int rep = 0;
  double realmod;
  double ranmodav, ranmodst;
  double *ranmodlis;
  double Tsched = 0.995, Tf = 0.0;
  double iterfac = 1.0;
  gsl_rng *rand_gen;
  char *netF;
  /*
    ------------------------------------------------------------
    Prompt for user-defined parameters
    ------------------------------------------------------------
  */
  printf ("%d\n",argc);
  if (argc == 1) {

  printf("\n# Enter random number seed (POSITIVE integer): ");
  scanf("%d", &seed);

  printf("\n# Enter the name of the network file: ");
  scanf("%s", &netF);

  printf("\n# Enter iteration factor (recommended 1.0): ");
  scanf("%lf", &iterfac);

  printf("\n# Enter the cooling factor (recommended 0.950-0.995): ");
  scanf("%lf", &Tsched);

  printf("\n# Enter the number of randomizations: ");
  scanf("%d", &rep);
  } else {
while ((c = getopt(argc, argv, "hf:s:i:c:r:")) != -1)
	  switch (c) {
	  case 'h':
		printf(USAGE ARGUMENTS);
		return -1;
		break;
	  case 'f':
		netF = optarg;
		break;
	  case 's':
		seed = atoi(optarg);
		break;
	  case 'r':
		rep = atoi(optarg);
		break;
	  case 'i':
		iterfac = atof(optarg);
		break;
	  case 'c':
		Tsched = atof(optarg);
		break;
	  }
  }
  ranmodlis = allocate_d_vec(rep);

  /*
    ------------------------------------------------------------
    Initialize the random number generator
    ------------------------------------------------------------
  */
  rand_gen = gsl_rng_alloc(gsl_rng_mt19937);
  gsl_rng_set(rand_gen, seed);

  /*
    ------------------------------------------------------------
    Build the network
    ------------------------------------------------------------
  */
  fprintf(stderr, "\n# Creating the network\n");

  inFile=fopen(netF,"r");
  net = FBuildNetwork(inFile, 0, 0, 0, 1);
  fclose(inFile);

  S = CountNodes(net);
  fprintf(stderr, "\n# The network has %d nodes\n", S);

  /*
    ------------------------------------------------------------
    Find and print the modules
    ------------------------------------------------------------
  */
  fprintf(stderr, "\n# Starting the simulated annealing\n");
  fprintf(stderr, "\n# 1/T\tM\tT\n");

  part = SACommunityIdent(net,
			  2.0 / (double)S, Tf, Tsched,
			  iterfac, 0, 'o', 1, 's', rand_gen);

  outFile = fopen("modules.dat", "w");
  FPrintPartition(outFile, part, 0);
  realmod = Modularity(part);
  fprintf(outFile, "# Modularity = %g\n", realmod);
  fclose(outFile);

  FPrintPajekFile("network.net", net, 0, 0, 1);
  FPrintPajekPartitionFile("modules.clu", net);

  /*
    ------------------------------------------------------------
    Calculate and print node properties
    ------------------------------------------------------------
  */
  outFile=fopen("node_prop.dat","w");
  p = net;
  while ((p = p->next) != NULL) {
    fprintf(outFile,"%s %d %lf %lf\n",
	    p->label, NodeDegree(p),
	    ParticipationCoefficient(p),
	    WithinModuleRelativeDegree(p, part));
  }
  fclose(outFile);

  /*
    ------------------------------------------------------------
    Find and print the roles
    ------------------------------------------------------------
  */
  fprintf(stderr, "\n# Finding node roles\n");
  roles = CatalogRoleIdent(net, part);

  outFile = fopen("roles.dat","w");
  FPrintPartition(outFile, roles, 0);
  fclose(outFile);

  MapPartToNet(roles, net);
  FPrintPajekPartitionFile("roles.clu", net);

  /*
    ------------------------------------------------------------
    Calculate properties for the randomized graph
    ------------------------------------------------------------
  */
  ranmodav = 0.0;
  for (i=0; i<rep; i++) {
    fprintf(stderr, "\nRandomization %d\n", i+1);

    RemovePartition(part);
    if ( netran != NULL)
      RemoveGraph(netran);

    netran = RandomizeSymmetricNetwork(CopyNetwork(net), 100, rand_gen);
    part = SACommunityIdent(netran,
			    2.0 / (double)S, Tf, Tsched,
			    iterfac, 0, 'o', 1, 'n', rand_gen);
    ranmodlis[i] = Modularity(part);
    ranmodav += ranmodlis[i];
  }

  /* Average and standard deviation */
  ranmodav /= (double)rep;
  ranmodst = 0.0;
  for (i=0; i<rep; i++) {
    ranmodst += (ranmodlis[i] - ranmodav) * (ranmodlis[i] - ranmodav);
  }
  ranmodst = sqrt(ranmodst / (double)rep);

  outFile = fopen("randomized_mod.dat", "w");
  fprintf(outFile, "# M\tMrand\tsimga_Mrand\n");
  fprintf(outFile, "%lf %lf %lf\n", realmod, ranmodav, ranmodst);
  fclose(outFile);

  /*
    ------------------------------------------------------------
    Free memory
    ------------------------------------------------------------
  */
  gsl_rng_free(rand_gen);
  free_d_vec(ranmodlis);
  RemovePartition(part);
  RemovePartition(roles);
  RemoveGraph(net);
  if (netran != NULL)
    RemoveGraph(netran);
  return 0;
}
예제 #2
0
int main()
{
  FILE *infile=NULL;
  struct node_gra *net = NULL;
  int repeat = 1;
  gsl_rng *randGen;

  /*
    ------------------------------------------------------------
    Build the network
    ------------------------------------------------------------
  */
  infile = fopen("test_graph1.dat", "r");
  net = FBuildNetwork(infile, 0, 0, 0, 1);
  fclose(infile);
  FPrintPajekFile("graph1.net", net, 0, 0, 1);

  /*
    --------------------------------------------------------------
    Initialize the random number generator
    --------------------------------------------------------------
  */
  randGen = gsl_rng_alloc(gsl_rng_mt19937);
  gsl_rng_set(randGen, 3333);

  /*
    ------------------------------------------------------------
    Network properties
    ------------------------------------------------------------
  */
  double dtemp;
  int itemp;
  printf("S = %d\n", CountNodes(net));

  dtemp = ClusteringCoefficient(net);
  if (fabs(dtemp - 0.1875) > EPS)
    return 1;
  else
    printf("C = %g\tOK\n", dtemp);

  printf("A = %g\n", Assortativity(net));

  dtemp = AverageInverseDistance(net);
  if (fabs(dtemp - 19. / 45.) > EPS) {
    printf("P = %g\tWRONG!!\n", dtemp);
    return 1;
  }
  else
    printf("P = %g\tOK\n", dtemp);

  struct node_gra *n1 = net->next;
  struct node_gra *n2 = n1->next;
  struct node_gra *n4 = n2->next->next;
  struct node_gra *n5 = n4->next;
  struct node_gra *n8 = n5->next->next->next;
  dtemp = JaccardIndex(n1, n2);
  if (fabs(dtemp - 0.25) > EPS)
    return 1;
  else
    printf("J[1][2] = %g\tOK\n", dtemp);
  dtemp = JaccardIndex(n2, n4);
  if (fabs(dtemp - 0.50) > EPS)
    return 1;
  else
    printf("J[2][4] = %g\tOK\n", dtemp);
  dtemp = JaccardIndex(n5, n8);
  if (fabs(dtemp - 0.50) > EPS)
    return 1;
  else
    printf("J[5][8] = %g\tOK\n", dtemp);
  
  if ((itemp = CommonNeighbors(n1, n2)) != 1)
    return 1;
  else
    printf("C[1][2] = %d\tOK\n", itemp);
  if ((itemp = CommonNeighbors(n2, n4)) != 1)
    return 1;
  else
    printf("C[2][4] = %d\tOK\n", itemp);
  if ((itemp = CommonNeighbors(n5, n8)) != 2)
    return 1;
  else
    printf("C[5][8] = %d\tOK\n", itemp);
      
  /*
    ------------------------------------------------------------
    Partition
    ------------------------------------------------------------
  */
  struct group *part = NULL;
  part = SACommunityIdent(net, 2. / CountNodes(net), 0.0, 0.995,
			  2.0, 2, 'o', 1, 'n', randGen);
  MapPartToNet(part, net);
  printf("M = %g\n", Modularity(part));
  RemovePartition(part);

  /*
    ------------------------------------------------------------
    Free memory
    ------------------------------------------------------------
  */
  RemoveGraph(net);
  gsl_rng_free(randGen);

  return 0;
}
예제 #3
0
main(int argc, char **argv)
{
  FILE *outF, *inF;
  int rgm;
  int seed = 1111;
  int to_file = 0;
  int from_file = 0;
  struct binet *binet = NULL;
  struct group *part = NULL;
  struct group *roles_part = NULL;
  struct node_gra *projected = NULL;
  gsl_rng *randGen;
  double degree_based = 0;
  double Ti, Tf;
  double Ts = 0.97;
  double fac = 1.0;
  double modularity = 0;
  char fn_array[256];
  char fno_array[256];
  char *file_name;
  char *file_name_out;
  int invert = 0;
  int weighted = 0;
  int output_type = 0;
  extern char *optarg;
  extern int optind;
  int c; 
  
  /*
    ------------------------------------------------------------
    Arguments parsing
    ------------------------------------------------------------
  */
  
  if (argc == 1) {
	from_file = 1;
	to_file = 1;	
	file_name = fn_array;
	file_name_out = fno_array;
	
	printf("\n# Enter random number seed (POSITIVE integer): ");
	scanf("%d", &seed);
	printf("\n# Enter the name of the network file: ");
	scanf("%s", &fn_array);

	printf("\n# Enter the name of the output file: ");
	scanf("%s", &fno_array);

	printf("\n# Enter iteration factor (recommended 1.0): ");
	scanf("%lf", &fac);
  
	printf("\n# Enter the cooling factor (recommended 0.950-0.995): ");
	scanf("%lf", &Ts);

	printf("\n# Find modules from first column (0) or second column (1): ");
	scanf("%d", &invert);

	printf("\n# Use the weighted (0) or weighted (1) modularity. (Edge weight is extracted from the third column): ");
	scanf("%d", &weighted);

	printf("\n# Use the strength (0) or degree (1) based role metrics: ");
	scanf("%d", &degree_based);


	printf("\n# Choose between tabular output (0), module partition (1) or roles partition (2):");
	scanf("%d", &output_type);}
  
  
  else{
	while ((c = getopt(argc, argv, "hwprmdf:s:i:c:o:")) != -1)
	  switch (c) {
	  case 'h':
		printf("\nUsage: bipartmod_cl [-f file] [-o file] [-s seed] [-i iter] [-c cool] [-pwmrh]\n"
			   "\nIf no arguments are provided, the program will fallback in interactive mode.\n\n"
			   "\t -f file: Name of the input network file (default: standard input)\n"
			   "\t -o file: Name of the output file (default: standard output)\n"
			   "\t -s seed: Random number seed (POSITIVE Integer, default 1111) \n"
			   "\t -i iter: Iteration factor (recommended 1.0, default 1.0)\n"
			   "\t -c cool: Cooling factor (recommended 0.950-0.995, default 0.97)\n "
			   "\t -p : Find modules for the second column (default: first) \n"
			   "\t -w : Read edge weights from the input's third column and uses the weighted modularity.\n"
			   "\t -d : Use degree based role metrics (default: strength based metrics)"
			   "\t -r : Output the roles partition rather than the default tabular output.\n"
			   "\t -m : Output the modules partition rather than the default tabular output. \n"
			   "\t -h : Display this message\n");
		return -1;
		break;
	  case 'f':
		from_file = 1;
		file_name = optarg;
		//printf("input set to %s \n", file_name);
		break;
	  case 's':
		seed = atoi(optarg);
		//printf("seed set to %d \n", seed);
		break;
	  case 'i':
		fac = atof(optarg);
		//printf("iteration factor set to %f \n", fac);
		break;
	  case 'c':
		Ts = atof(optarg);
		//printf("cooling factor set to %f. \n", Ts);
		break;
	  case 'p':
		invert = 1;
		//printf("Looking at second column.\n");
		break;
	  case 'm':
		output_type = 1;
		break;
	  case 'r':
		output_type = 2;
		break;
	  case 'o':
		to_file = 1;
		file_name_out = optarg;
		break;
	  case 'w':
		weighted = 1;
		break;
	  case 'd':
		degree_based = 1;
		break;

	  }
  }

  /*
    ------------------------------------------------------------------
    Initialize the random number generator
    ------------------------------------------------------------------
  */
  randGen = gsl_rng_alloc(gsl_rng_mt19937);
  gsl_rng_set(randGen, seed);

  /*
    ------------------------------------------------------------------
    Build the network
    ------------------------------------------------------------------
  */
  if (from_file == 1) {
	inF = fopen(file_name, "r");
	if (inF == NULL)
	  {
		printf("ERROR: No such file or directory (%s). \n", file_name);
		return(1);
	  }
	binet = FBuildNetworkBipart(inF, weighted, 0);
	fclose(inF);
  }
  else
	binet = FBuildNetworkBipart(stdin, weighted, 0);
  
  if (invert == 1)
    InvertBipart(binet);

  /*
    ------------------------------------------------------------------
    Find the modules using the bipartite network
    ------------------------------------------------------------------
  */
  Ti = 1. / (double)CountNodes(binet->net1);
  Tf = 0.;

  if (weighted == 1){
	part = SACommunityIdentBipartWeighted(binet,
										  Ti, Tf, Ts, fac,
										  0, 'o', 1, 'm',
										  randGen);
  }
  else{
	part = SACommunityIdentBipart(binet,
								  Ti, Tf, Ts, fac,
								  0, 'o', 1, 'm',
								  randGen);
  }

  // Compute the role partition if we have to.
  // Note that the roles are computed (but not as a partition)
  // if the tabular output is selected (output_type==0).
  if (output_type == 2){
	projected = ProjectBipart(binet);
	if (degree_based==1)
	  part = CatalogRoleIdent(projected,part);
	else
	  part = CatalogRoleIdentStrength(projected,part);
  }

  /*
    ------------------------------------------------------------
    Output
    ------------------------------------------------------------
  */

  if (to_file == 1)	{
	outF = fopen(file_name_out, "w");
	if (outF == NULL)
	  {
		printf("ERROR: Cannot write output (%s). \n", file_name_out);
		return(1);
	  }
	if (output_type != 0){
	  // Partition-type output (a la Netcarto).
	  FPrintPartition(outF, part, 0);
	  modularity = Modularity(part);
	  fprintf(outF, "# Modularity = %g\n", modularity);
	}
	else
	  // Tabular output. 
	  FPrintTabNodesBipart(outF, binet, part, degree_based);


	fclose(outF);
  }
  else{
	if (output_type != 0){
	  FPrintPartition(stdout, part, 0);
	  modularity = Modularity(part);
	  fprintf(stdout, "# Modularity = %g\n", modularity);
	}
	else
	  FPrintTabNodesBipart(stdout, binet, part, degree_based);
  }
  
  // Free memory
  RemovePartition(part);
  RemoveBipart(binet);
  if (output_type == 2)
	RemoveGraph(projected);
  gsl_rng_free(randGen);
}