SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
io.c
Go to the documentation of this file.
1 /**************************************************************/
2 /*! \file
3 
4  \brief This file contains all the I/O routines
5 */
6 /**************************************************************/
7 
8 
9 #include<slim.h>
10 
11 
12 
13 /**************************************************************/
14 /*!
15  \brief Read in a constraint matrix for feature selection
16  */
17 /**************************************************************/
18 gk_csr_t * read_constraint(ctrl_t * ctrl, char * file){
19 
20  gk_csr_t * constraint = gk_csr_Read(file, GK_CSR_FMT_CSR, 1, 1);
21 
22  if (ctrl->transpose){
23  /* use the model from SLIM as constraint, which is transpose
24  of an item-item similarity matrix */
25  constraint->colptr = constraint->rowptr;
26  constraint->colind = constraint->rowind;
27  constraint->colval = constraint->rowval;
28  int tmp_ncols = constraint->ncols;
29  constraint->ncols = constraint->nrows;
30  constraint->rowptr = NULL;
31  constraint->rowind = NULL;
32  constraint->rowval = NULL;
33  constraint->nrows = tmp_ncols;
34  gk_csr_CreateIndex(constraint, GK_CSR_ROW);
35  }else{
36  /* use an item-item similarity matrix as constraint */
37  gk_csr_CreateIndex(constraint, GK_CSR_COL);
38  }
39 
40  return constraint;
41 }
42 
43 
44 
45 /**************************************************************/
46 /*! \brief Dump the csr into a file
47  */
48 /**************************************************************/
49 void csr_Write(gk_csr_t *mat, char *filename, char * mode,
50  int format, int writevals, int numbering) {
51 
52  int i, j;
53  FILE *fpout;
54 
55  if (filename)
56  fpout = gk_fopen(filename, mode, "gk_csr_Write: fpout");
57  else
58  fpout = stdout;
59 
60  if (format == GK_CSR_FMT_CLUTO) {
61  fprintf(fpout, "%d %d %d\n", mat->nrows, mat->ncols, mat->rowptr[mat->nrows]);
62  writevals = 1;
63  numbering = 1;
64  }
65 
66  for (i=0; i<mat->nrows; i++) {
67  for (j=mat->rowptr[i]; j<mat->rowptr[i+1]; j++) {
68  fprintf(fpout, " %d", mat->rowind[j]+(numbering ? 1 : 0));
69  if (writevals)
70  fprintf(fpout, " %.5f", mat->rowval[j]);
71  }
72  fprintf(fpout, "\n");
73  }
74  if (filename)
75  gk_fclose(fpout);
76 
77 }