fis.c 8.54 KB
Newer Older
lisj's avatar
lisj committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*!
\file  
\brief A simple frequent itemset discovery program to test GKlib's routines

\date 6/12/2008
\author George
\version \verbatim $Id: fis.c 11075 2011-11-11 22:31:52Z karypis $ \endverbatim
*/

#include <GKlib.h>

/*************************************************************************/
/*! Data structures for the code */
/*************************************************************************/
typedef struct {
  ssize_t minlen, maxlen;
  ssize_t minfreq, maxfreq;
  char *filename;
  int silent;
  ssize_t nitemsets;
  char *clabelfile;
  char **clabels;
} params_t;

/*************************************************************************/
/*! Constants */
/*************************************************************************/
#define CMD_MINLEN      1
#define CMD_MAXLEN      2
#define CMD_MINFREQ     3
#define CMD_MAXFREQ     4
#define CMD_SILENT      5
#define CMD_CLABELFILE  6
#define CMD_HELP        10


/*************************************************************************/
/*! Local variables */
/*************************************************************************/
static struct gk_option long_options[] = {
  {"minlen",        1,      0,      CMD_MINLEN},
  {"maxlen",        1,      0,      CMD_MAXLEN},
  {"minfreq",       1,      0,      CMD_MINFREQ},
  {"maxfreq",       1,      0,      CMD_MAXFREQ},
  {"silent",        0,      0,      CMD_SILENT},
  {"clabels",       1,      0,      CMD_CLABELFILE},
  {"help",          0,      0,      CMD_HELP},
  {0,               0,      0,      0}
};


/*-------------------------------------------------------------------*/
/* Mini help  */
/*-------------------------------------------------------------------*/
static char helpstr[][100] = {
" ",
"Usage: fis [options] <mat-file>",
" ",
" Required parameters",
"  mat-file",
"     The name of the file storing the transactions. The file is in ",
"     Cluto's .mat format.",
" ",
" Optional parameters",
"  -minlen=int",
"     Specifies the minimum length of the patterns. [default: 1]",
" ",
"  -maxlen=int",
"     Specifies the maximum length of the patterns. [default: none]",
" ",
"  -minfreq=int",
"     Specifies the minimum frequency of the patterns. [default: 10]",
" ",
"  -maxfreq=int",
"     Specifies the maximum frequency of the patterns. [default: none]",
" ",
"  -silent",
"     Does not print the discovered itemsets.",
" ",
"  -clabels=filename",
"     Specifies the name of the file that stores the column labels.",
" ",
"  -help",
"     Prints this message.",
""
};

static char shorthelpstr[][100] = {
" ",
"   Usage: fis [options] <mat-file>",
"          use 'fis -help' for a summary of the options.",
""
};
 


/*************************************************************************/
/*! Function prototypes */
/*************************************************************************/
void print_init_info(params_t *params, gk_csr_t *mat);
void print_final_info(params_t *params);
params_t *parse_cmdline(int argc, char *argv[]);
void print_an_itemset(void *stateptr, int nitems, int *itemind, 
                      int ntrans, int *tranind);


/*************************************************************************/
/*! the entry point */
/**************************************************************************/
int main(int argc, char *argv[])
{
  ssize_t i;
  char line[8192];
  FILE *fpin;
  params_t *params;
  gk_csr_t *mat;
 
  params = parse_cmdline(argc, argv);
  params->nitemsets = 0;

  /* read the data */
  mat = gk_csr_Read(params->filename, GK_CSR_FMT_CLUTO, 1, 1);
  gk_csr_CreateIndex(mat, GK_CSR_COL);

  /* read the column labels */
  params->clabels = (char **)gk_malloc(mat->ncols*sizeof(char *), "main: clabels");
  if (params->clabelfile == NULL) {
    for (i=0; i<mat->ncols; i++) {
      sprintf(line, "%zd", i);
      params->clabels[i] = gk_strdup(line);
    }
  }
  else {
    fpin = gk_fopen(params->clabelfile, "r", "main: fpin");
    for (i=0; i<mat->ncols; i++) {
      if (fgets(line, 8192, fpin) == NULL)
        errexit("Failed on fgets.\n");
      params->clabels[i] = gk_strdup(gk_strtprune(line, " \n\t"));
    }
    gk_fclose(fpin);
  }


  print_init_info(params, mat);

  gk_find_frequent_itemsets(mat->nrows, mat->rowptr, mat->rowind,
      params->minfreq, params->maxfreq, params->minlen, params->maxlen,
      &print_an_itemset, (void *)params);

  printf("Total itemsets found: %zd\n", params->nitemsets);

  print_final_info(params);
}  



/*************************************************************************/
/*! This function prints run parameters */
/*************************************************************************/
void print_init_info(params_t *params, gk_csr_t *mat)
{
  printf("*******************************************************************************\n");
  printf(" fis\n\n");
  printf("Matrix Information ---------------------------------------------------------\n");
  printf(" input file=%s, [%d, %d, %zd]\n", 
      params->filename, mat->nrows, mat->ncols, mat->rowptr[mat->nrows]);

  printf("\n");
  printf("Options --------------------------------------------------------------------\n");
  printf(" minlen=%zd, maxlen=%zd, minfeq=%zd, maxfreq=%zd\n",
      params->minlen, params->maxlen, params->minfreq, params->maxfreq);

  printf("\n");
  printf("Finding patterns... -----------------------------------------------------\n");
}


/*************************************************************************/
/*! This function prints final statistics */
/*************************************************************************/
void print_final_info(params_t *params)
{
  printf("\n");
  printf("Memory Usage Information -----------------------------------------------------\n");
  printf("   Maximum memory used:              %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());
  printf("   Current memory used:              %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());
  printf("********************************************************************************\n");
}


/*************************************************************************/
/*! This is the entry point of the command-line argument parser */
/*************************************************************************/
params_t *parse_cmdline(int argc, char *argv[])
{
  int i;
  int c, option_index;
  params_t *params;

  params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");

  /* initialize the params data structure */
  params->minlen     = 1;
  params->maxlen     = -1;
  params->minfreq    = 10;
  params->maxfreq    = -1;
  params->silent     = 0;
  params->filename   = NULL;
  params->clabelfile = NULL;


  /* Parse the command line arguments  */
  while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {
    switch (c) {
      case CMD_MINLEN:
        if (gk_optarg) params->minlen = atoi(gk_optarg);
        break;
      case CMD_MAXLEN:
        if (gk_optarg) params->maxlen = atoi(gk_optarg);
        break;
      case CMD_MINFREQ:
        if (gk_optarg) params->minfreq = atoi(gk_optarg);
        break;
      case CMD_MAXFREQ:
        if (gk_optarg) params->maxfreq = atoi(gk_optarg);
        break;

      case CMD_SILENT:
        params->silent = 1;
        break;

      case CMD_CLABELFILE:
        if (gk_optarg) params->clabelfile = gk_strdup(gk_optarg);
        break;

      case CMD_HELP:
        for (i=0; strlen(helpstr[i]) > 0; i++)
          printf("%s\n", helpstr[i]);
        exit(0);
        break;
      case '?':
      default:
        printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);
        exit(0);
    }
  }

  if (argc-gk_optind != 1) {
    printf("Unrecognized parameters.");
    for (i=0; strlen(shorthelpstr[i]) > 0; i++)
      printf("%s\n", shorthelpstr[i]);
    exit(0);
  }

  params->filename = gk_strdup(argv[gk_optind++]);

  if (!gk_fexists(params->filename))
    errexit("input file %s does not exist.\n", params->filename);

  return params;
}



/*************************************************************************/
/*! This is the callback function for the itemset discovery routine */
/*************************************************************************/
void print_an_itemset(void *stateptr, int nitems, int *itemids, int ntrans, 
         int *transids)
{
  ssize_t i;
  params_t *params;

  params = (params_t *)stateptr;
  params->nitemsets++;

  if (!params->silent) {
    printf("%4zd %4d %4d => ", params->nitemsets, nitems, ntrans);
    for (i=0; i<nitems; i++)
      printf(" %s", params->clabels[itemids[i]]);
    printf("\n");
    for (i=0; i<ntrans; i++)
      printf(" %d\n", transids[i]);
    printf("\n");
  }
}