File: clmorder.c

package info (click to toggle)
mcl 1%3A06-021-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,276 kB
  • ctags: 2,975
  • sloc: ansic: 32,352; sh: 3,556; perl: 1,437; makefile: 338
file content (201 lines) | stat: -rw-r--r-- 5,261 bytes parent folder | download | duplicates (2)
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
/*      Copyright (C) 2001, 2002, 2003, 2004, 2005 Stijn van Dongen
 *
 * This file is part of MCL.  You can redistribute and/or modify MCL under the
 * terms of the GNU General Public License; either version 2 of the License or
 * (at your option) any later version.  You should have received a copy of the
 * GPL along with MCL, in the file COPYING.
*/

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

#include "report.h"

#include "impala/matrix.h"
#include "impala/vector.h"
#include "impala/io.h"
#include "impala/compose.h"
#include "impala/iface.h"
#include "impala/app.h"
#include "mcl/clm.h"

#include "util/io.h"
#include "util/types.h"
#include "util/err.h"
#include "util/opt.h"


/*  This program reorders indices so that that ordering represents
    blocks from different clusterings (which are preferably more or
    less hierarchically organized).

    It first transforms a list of input matrices by taking successive
    meets, yielding a perfectly nested list of clusterings.  The
    clusterings should be given ordered from coarse to fine-grained.
    The meets are all internally reordered so that largest clusters
    come first.

    The resulting Babushka structure is then traversed to create the
    ordering.
*/

mcxstatus rerank
(  long     clid
,  int      depth
,  mclx**   clxs
,  mclx**   maps
,  mclv*    ordv
,  double*  ord
)  ;


int main
(  int                  argc
,  const char*          argv[]
)
   {  mcxIO       *xfin    =  mcxIOnew("-", "r")
   ;  mcxIO       *xfout   =  mcxIOnew("out.order", "w")
   ;  mcxTing     *fname   =  NULL
   ;  mclv*       ordv     =  mclvInit(NULL)
   ;  double      ord      =  1.0
   ;  long        i        =  0

   ;  mclx       **clxs    =  NULL
   ;  mclx       **maps    =  NULL
   ;  mclx        *cls     =  NULL
   ;  int         n_clxs   =  0

   ;  const char  *me      =  "clmorder"
   ;  int         a        =  1

   ;  mclxIOsetQMode("MCLXIOVERBOSITY", MCL_APP_VB_YES)

   ;  clxs = mcxAlloc(argc*sizeof(mclx*), EXIT_ON_FAIL)
   ;  maps = mcxAlloc(argc*sizeof(mclx*), EXIT_ON_FAIL)

   ;  while(a<argc)
      {  mcxIOnewName(xfin, argv[a])
      ;  cls = mclxRead(xfin, EXIT_ON_FAIL)
      ;  mclxColumnsRealign(cls, mclvSizeRevCmp)
      ;  mclxMakeCharacteristic(cls)
      ;  mcxIOclose(xfin)

/* mcx /out.order-1 lm tp /out.order-2 lm mul tp /x1 wm
 */
      ;  if (!n_clxs)
         clxs[n_clxs] = cls
      ;  else
         {  mclx* clinv, *map

         ;  clxs[n_clxs]   =  mclcMeet(clxs[n_clxs-1], cls, EXIT_ON_FAIL)
         ;  mclxColumnsRealign(clxs[n_clxs], mclvSizeRevCmp)

         ;  clinv          =  mclxTranspose(clxs[n_clxs])
         ;  map            =  mclxCompose(clinv, clxs[n_clxs-1], 0)
         ;  maps[n_clxs-1] =  map

         ;  fname = mcxTingPrint(fname, "out.map.%d-%d", (int) a-1, (int) a)
         ;  mcxIOnewName(xfout, fname->str)
         ;  mclxWrite(map, xfout, MCLXIO_VALUE_GETENV, EXIT_ON_FAIL)
         ;  mcxIOclose(xfout)

         ;  mclxFree(&cls)
         ;  mclxFree(&clinv)
      ;  }

         fname = mcxTingPrint(fname, "out.order.%d", (int) a)
      ;  mcxIOnewName(xfout, fname->str)
      ;  mclxWrite(clxs[n_clxs], xfout, MCLXIO_VALUE_NONE, EXIT_ON_FAIL)
      ;  mcxIOclose(xfout)

      ;  n_clxs++
      ;  a++
   ;  }

      if (!n_clxs)
      return 0

   ;  ordv           =  mclvClone(clxs[0]->dom_rows)
   ;  clxs[n_clxs]   =  NULL
   ;  maps[n_clxs-1] =  NULL

   ;  {  mclx* cls0  =  clxs[0]
      ;  for (i=0;i<cls0->dom_cols->n_ivps;i++)
         if (rerank(cls0->dom_cols->ivps[i].idx, 0,  clxs, maps, ordv, &ord))
         break

      ;  if (i == cls0->dom_cols->n_ivps)
         {  mcxTell
            (me, "reordering successful (%ld entries)", (long) ordv->n_ivps)
         ;  mclvSort(ordv, mclpValCmp)
         ;  mclvaDump2(ordv, stdout, 0, -1, "\n", MCLVA_DUMP_EOV_NO)
      ;  }
      }

      return(0)
;  }


mcxstatus rerank
(  long     clid
,  int      depth
,  mclx**   clxs
,  mclx**   maps
,  mclv*    ordv
,  double*  ord
)
   {  mclx* cls = clxs[depth]
   ;  mclx* map = maps[depth]
   ;  mcxstatus status = STATUS_FAIL
   ;  mclv* domv
   ;  const char* errmsg = "all's well"

;if (0) fprintf(stderr, "in at %ld/%d %f\n", clid, depth, *ord) 

   ;  while (1)
      {  if (!cls)
         {  errmsg = "no cls"
         ;  break
      ;  }

         domv = mclxGetVector(cls, clid, RETURN_ON_FAIL, NULL)

      ;  if (!domv)
         {  errmsg = "no domv"
         ;  break
      ;  }

         if (map)
         {  mclv* mapv = mclxGetVector(map, clid, RETURN_ON_FAIL, NULL)
         ;  long i
         ;  if (!map)
            {  errmsg = "no mapv"
            ;  break
         ;  }

            for (i=0;i<mapv->n_ivps;i++)
            if (rerank(mapv->ivps[i].idx, depth+1, clxs, maps, ordv, ord))
            break

         ;  if (i != mapv->n_ivps)
            break
      ;  }
         else
         {  long i
         ;  for (i=0;i<domv->n_ivps;i++)
            {  mclvInsertIdx(ordv, domv->ivps[i].idx, *ord)
            ;  (*ord)++
         ;  }
         }

         status = STATUS_OK
      ;  break
   ;  }
      
      if (status)
      {  mcxErr("rerank", "panic <%s> clid/depth %ld/%d", errmsg, clid, depth)
      ;  return STATUS_FAIL
   ;  }
      return STATUS_OK
;  }