-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvnl-make-matrix
executable file
·166 lines (128 loc) · 3.55 KB
/
vnl-make-matrix
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use autodie;
my $usage = "$0 [--prefix prefix] --outdir dir";
my %options;
GetOptions(\%options,
"prefix=s",
"outdir=s",
"help") or die($usage);
if( defined $options{help} )
{
print "$usage\n";
exit 0;
}
my $outdir = $options{outdir};
if( ! -d $outdir || ! -w $outdir )
{
die "Usage: $usage\nThe directory must be writeable";
}
my $prefix = $options{prefix} // '';
my $slow_prev_value;
my @fields;
my @fds;
while(<>)
{
chomp;
if(!@fields)
{
if(/^ *# (.*?) *$/)
{
@fields = split(/ /, $1);
if( @fields < 2 )
{
die "Each line must have at least two fields: the least-significant index, and at least one field of values";
}
shift @fields;
@fds = map { my $fd;
my $filename = "$outdir/${prefix}$_.matrix";
print STDERR "Writing to '$filename'\n";
open $fd, '>', $filename;
$fd;
} @fields;
next;
}
next if /^ *#/;
die "Got line of data before a legend: '$_'";
}
next if /^ *#/;
# have legend and have data
my @F = split;
if( !defined $slow_prev_value )
{
$slow_prev_value = $F[0];
}
elsif( $F[0] != $slow_prev_value )
{
$slow_prev_value = $F[0];
foreach my $fd(@fds)
{
print $fd "\n";
}
}
foreach my $ifd (0..$#fds)
{
my $fd = $fds[$ifd];
print $fd "$F[$ifd+1] ";
}
}
foreach my $fd (@fds)
{
print $fd "\n";
close $fd;
}
__END__
=head1 NAME
vnl-make-matrix - create a matrix from a one-point-per-record vnlog
=head1 SYNOPSIS
$ cat /tmp/dat.vnl
# i j x
0 0 1
0 1 2
0 2 3
1 0 4
1 1 5
1 2 6
2 0 7
2 1 8
2 2 9
3 0 10
3 1 11
3 2 12
$ </tmp/dat.vnl vnl-filter -p i,x | vnl-make-matrix --outdir /tmp --prefix test_
Writing to '/tmp/test_x.matrix'
$ cat /tmp/test_x.matrix
1 2 3
4 5 6
7 8 9
10 11 12
=head1 DESCRIPTION
Vnlog represents each "data item" as a line of text. This is very often what
one wants, but at times it isn't. One example of this is matrix data: we want
each line to contain a whole row of a matrix. This script exists for
convenience, to bridge this gap.
The input is an vnlog, coming in on STDIN and/or in files given on the
commandline. This vnlog must have at least two fields: the least-significant
(slowest-changing) index of each point (must be the I<first> field), and as many
value fields as desired. These points must be written out in order, and it is
assumed that all entries in the matrix are specified. The output is a set of
(non-vnlog) matrix files in the directory given in the C<--outdir> argument.
These files are named C<PREFIX_XXX.matrix> where C<PREFIX> comes from --prefix
(or empty) and C<XXX> is the field name. These matrices can be loaded into any
analysis tool (numpy for instance), or plotted directly with gnuplot:
set size ratio -1
plot "/tmp/test_x.matrix" matrix with image
pause -1
=head1 REPOSITORY
https://github.com/dkogan/vnlog/
=head1 AUTHOR
Dima Kogan C<< <[email protected]> >>
=head1 LICENSE AND COPYRIGHT
Copyright 2016 California Institute of Technology.
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.
=cut