Debian Codegraph @

gbdummyfy

raw
1 #!/bin/sh
2 # gbdummyfy ver. .5 Copyright (C) 2009-2012 Giulio Bottazzi
3
4 #default settings
5 pos=1
6 del="no"
7
8 #read command line options; the position of the last option is saved
9 #in OPTIND
10 while getopts "c:d:hv-:" opt
11 do
12 case $opt in
13 -)
14 case "${OPTARG}" in
15 help) help=yes;;
16 version) version=yes;;
17 esac;;
18 c) pos=$OPTARG ;;
19 d) del=$OPTARG ;;
20 v) verbose=yes ;;
21 h) help=yes ;;
22 \?) help=yes;;
23 esac
24 done
25
26 if [ "$version" = "yes" ]; then
27
28 cat - <<EOF
29 gbdummyfy ver. 5.6
30
31 Copyright (C) 2009-2015 Giulio Bottazzi
32
33 This program is free software; you can redistribute it and/or
34 modify it under the terms of the GNU General Public License
35 (version 2) as published by the Free Software Foundation.
36
37 This program is distributed in the hope that it will be useful,
38 but WITHOUT ANY WARRANTY; without even the implied warranty of
39 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 GNU General Public License for more details.
41
42 Written by Giulio Bottazzi
43
44 Report bugs to <[email protected]>
45
46 Package home page <http://cafim.sssup.it/~giulio/software/gbutils/index.html>
47 EOF
48
49 exit
50 fi
51
52 if [ "$help" = "yes" ]; then
53
54 cat - <<EOF
55 This command reads from standard input a text file with space
56 separated columns. The entry in one column (the first by default) are
57 considered labels and expanded into a matrix of dummies , i.e. of 0
58 and 1 values. The number of columns of the matrix is equal to the
59 number of different labels. Each row contains '1' in the place of the
60 associated labels in the sorted list of labels, and '0' everywehere
61 else. Since in general one less dummy variable is required than the
62 number of labels, you can remove one column of dummies using the
63 option '-d'.
64
65 Usage: gbdummyfy [options]
66
67 Options:
68 -h print this help
69 -c set the column of labels (default 1)
70 -d which column to remove, counting from 1 (default none)
71 -v print the labels and associated positions to standard error
72
73 Examples:
74 echo "a 1\nb 2" | gbdummyfy create a 4x3 marix with dummy values
75 relative to labels 'a' and 'b'
76
77 This program requires awk or gawk. Notice that it simply expands the
78 data adding new columns. When using the resulting the resulting matrix
79 in other utilities, the user should specify explicitly which dummies
80 variable to use and how.
81
82 A simple linear dependency can be automatically generated for 'gblreg'
83 by inserting the following expression in the functional specification
84
85 \`seq 3 12 | sed 's/\(.*\)/\+d\1\*x\1/' | tr -d '\n'\`
86
87 and
88
89 \`seq 3 12 | sed 's/\(.*\)/,\1=0/' | tr -d '\n'\`
90
91 among the initial conditions. In this case there are 10 different values
92 for the dummy. They occupy column positions from 3 to 12 and their initial
93 value is zero.
94 EOF
95
96 exit
97
98 fi
99
100 #create temporary files
101 dataorig=`tempfile`
102 initial=`tempfile`
103 final=`tempfile`
104 dummies=`tempfile`
105 newdummies=`tempfile`
106
107 #store the original data removing possible initial empty spaces and
108 #shrinking any number of spaces and tab in one single space
109 cat - | tr '\t' ' ' | tr -s ' ' | sed 's/^ *//' > $dataorig
110
111 #count the number of columns
112 colnum=`head -n 1 $dataorig | gawk '{print NF}'`
113
114 #separate the input file in three parts: before the column of dummies
115 if [ $pos -gt 1 ]; then
116 cut -d ' ' -f 1-$(( pos-1 )) < $dataorig > $initial
117 fi
118 #the column of dummies itself
119 cut -d ' ' -f $pos < $dataorig > $dummies
120 #after the column of dummies
121 if [ $pos -lt $colnum ]; then
122 cut -d ' ' -f $(( pos+1 ))- < $dataorig > $final
123 fi
124
125 awk -v verbose=$verbose '
126
127 {
128
129 #store the dummy of the line
130 line[NR]=$1
131 # prepare the list of labels
132 label[$1]=1
133 }
134
135 END {
136 #sort and collect labels
137 labnum=asorti(label,labsort)
138 #print labels if verbose
139 if(verbose ~ "yes")
140 for(i=1;i<=labnum;i++)
141 print labsort[i],i | "cat 1>&2"
142 #prepare the array
143 for(i=1;i<=labnum;i++){
144 #create the string
145 string=""
146 for(j=1;j<i;j++)
147 string=string"0 "
148 string=string"1 "
149 for(j=i;j<labnum;j++)
150 string=string"0 "
151 #assign it to the appropriate label
152 label[labsort[i]]=string
153 }
154 #print output
155 for(i=1;i<=NR;i++)
156 print label[line[i]]
157 }
158
159 ' $dummies > $newdummies
160
161 #delete a column
162 if [ "$del" != "no" ]; then
163
164 labnum=`head -n 1 $newdummies | gawk '{print NF}'`
165
166 if [ $del = 1 ]; then
167 storage=`tempfile`
168 cat $newdummies > $storage
169 cut -d ' ' -f 2- < $storage > $newdummies
170 rm $storage
171 elif [ $del = $labnum ]; then
172 storage=`tempfile`
173 cat $newdummies > $storage
174 cut -d ' ' -f 1-$(( labnum-1 )) < $storage > $newdummies
175 rm $storage
176 elif test $del -gt 1 && test $del -lt $labnum; then
177 storage1=`tempfile`
178 storage2=`tempfile`
179 cut -d ' ' -f 1-$(( del-1 )) < $newdummies > $storage1
180 cut -d ' ' -f $(( del+1 ))- < $newdummies > $storage2
181 paste -d ' ' $storage1 $storage2 > $newdummies
182 rm $storage1 $storage2
183 else
184 echo "gbdummyfy: wrong column spec in option -d; request ignored" > /dev/stderr
185 fi
186
187 fi
188
189 #rebuild the file
190 if [ "$colnum" = 1 ]; then
191 cat $newdummies
192 elif [ "$pos" = 1 ]; then
193 paste -d ' ' $newdummies $final
194 elif [ "$pos" = "$colnum" ]; then
195 paste -d ' ' $initial $newdummies
196 else
197 paste -d ' ' $initial $newdummies $final
198 fi
199
200 #remove temporary files
201 rm $dataorig $initial $final $dummies $newdummies