Improved VarSet and the GIBBS alias
[libdai.git] / scripts / convert-fastInf-DAI.pl
1 #! /usr/local/bin/perl
2 #
3 # This file is part of libDAI - http://www.libdai.org/
4 #
5 # libDAI is licensed under the terms of the GNU General Public License version
6 # 2, or (at your option) any later version. libDAI is distributed without any
7 # warranty. See the file COPYING for more details.
8 #
9 # Copyright (C) 2010 Ofer Meshi
10
11 use strict;
12 use warnings;
13
14 my $HOME = $ENV{HOME} ;
15
16 if ($#ARGV == 0) {
17 push (@ARGV,"$ARGV[0].fg") ;
18 }
19
20 my $expectedArgc = 2 ;
21 if ($#ARGV != $expectedArgc-1) {
22 die ("Usage: $0 <inModelFile> <outModelFile>\n") ;
23 }
24
25 my ($inModelFile,$outModelFile) = @ARGV ;
26
27 # First we read the model:
28 my (@variables,@cliques,@measures,%cliq2meas,$truePartition) ;
29 &readModel ($inModelFile,\@variables,\@cliques,\@measures,\%cliq2meas,\$truePartition) ;
30
31
32 open (OUT, ">$outModelFile") or die ("[ERROR] Cannot open output file $outModelFile\n") ;
33
34 print OUT "# Converted $inModelFile from fastInf format into DAI format\n" ;
35
36 print OUT scalar(@cliques), "\n\n" ;
37
38 for (my $i=0 ; $i<scalar(@cliques) ; $i++) {
39 my $cliqLine = $cliques[$i] ;
40 my @splitCliq = split (/\t/, $cliqLine) ;
41 #print "Num vars = " , $splitCliq[1] , " vars: " , $splitCliq[2] , "\n" ;
42 print OUT $splitCliq[1], "\n" ;
43 print OUT $splitCliq[2], "\n" ;
44
45 my $meas = $cliq2meas{$i} ;
46 my $measLine = $measures[$meas] ;
47 my @splitMeas = split (/\t/, $measLine) ;
48 print OUT $splitMeas[2], "\n" ;
49
50 my @params = split (/\s+/, $splitMeas[3]) ;
51 print OUT scalar(@params) , "\n" ;
52 for (my $j=0 ; $j<scalar(@params) ; $j++) {
53 print OUT $j, " ", $params[$j], "\n" ;
54 }
55 print OUT "\n" ;
56 }
57
58 close OUT ;
59
60
61
62 #################
63 ## Subroutines ##
64 #################
65 sub readModel {
66 my ($modelFile,$variables,$cliques,$measures,$cliq2meas,$truePartition) = @_ ;
67
68 open (MOD_IN , "<$modelFile") or die ("[ERROR] Cannot read model from $modelFile\n") ;
69
70 while (defined (my $line=<MOD_IN>)) {
71
72 if ($line =~ m/^\@Variables/) {
73 &readLines (*MOD_IN , $variables) ;
74 }
75 elsif ($line =~ m/^\@Cliques/) {
76 &readLines (*MOD_IN , $cliques) ;
77 }
78 elsif ($line =~ m/^\@Measures/) {
79 &readLines (*MOD_IN , $measures) ;
80 }
81 elsif ($line =~ m/^\@CliqueToMeasure/) {
82 &readCliq2Meas (*MOD_IN , $cliq2meas) ;
83 }
84 elsif ($line =~ m/^# Exact Partitition function:/) {
85 my $partLine ;
86 if (not defined ($partLine = <MOD_IN>)) { die ("[ERROR] Cannot read exact partition function."); }
87 if ($partLine =~ m/^# (\S+)/) {
88 $$truePartition = $1 ;
89 }
90 }
91 # else do nothing
92
93 } # while read line from model file
94
95 close MOD_IN ;
96 }
97
98 sub readLines {
99 my ($IN,$variables) = @_ ;
100
101 my $endReached = 0 ;
102 while (defined (my $line = <$IN>)) {
103 if ($line =~ m/^\@End/) {
104 $endReached = 1 ;
105 last ;
106 }
107 else {
108 push (@$variables,$line) ;
109 }
110 }
111
112 if (not $endReached) { die ("[ERROR] Could not find \@End statement.\n") ; }
113 }
114
115 sub readCliq2Meas {
116 my ($IN,$cliq2meas) = @_ ;
117
118 my $endReached = 0 ;
119 while (defined (my $line = <$IN>)) {
120 if ($line =~ m/^\@End/) {
121 $endReached = 1 ;
122 last ;
123 }
124 else {
125 my @splitLine = split(/\t/,$line) ;
126 my $cliq = $splitLine[0] ;
127 my $meas = $splitLine[1] ;
128 $cliq2meas->{$cliq} = $meas ;
129 }
130 }
131
132 if (not $endReached) { die ("[ERROR] Could not find \@End statement.\n") ; }
133 }