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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
|
* Talk
I just gave a talk about this at [[https://www.socallinuxexpo.org/scale/17x][SCaLE 17x]]. Here are the [[https://www.youtube.com/watch?v=Qvb_uNkFGNQ&t=12830s][video of the talk]] and
the [[https://github.com/dkogan/talk-feedgnuplot-vnlog/blob/master/feedgnuplot-vnlog.org]["slides"]].
* Summary
Vnlog ("vanilla-log") is a toolkit for manipulating tabular ASCII data with
labelled fields using normal UNIX tools. If you regularly use =awk= and =sort=
and =uniq= and others, these tools will make you infinitely more powerful. The
vnlog tools /extend/, rather than replace the standard tooling, so minimal
effort is required to learn and use these tools.
Everything assumes a trivially simple log format:
- A whitespace-separated table of ASCII human-readable text
- A =#= character starts a comment that runs to the end of the line (like in
many scripting languages)
- The first line that begins with a single =#= (not =##= or =#!=) is a /legend/,
naming each column. This is required, and the field names that appear here are
referenced by all the tools.
- Empty fields reported as =-=
This describes 99% of the format, with some extra details [[#format-details][below]]. Example:
#+BEGIN_EXAMPLE
#!/usr/bin/whatever
# a b c
1 2 3
## comment
4 5 6
#+END_EXAMPLE
Such data can be processed directly with almost any existing tool, and /this/
toolkit allows the user to manipulate this data in a nicer way by relying on
standard UNIX tools. The core philosophy is to avoid creating new knowledge as
much as possible. Consequently, the vnlog toolkit relies /heavily/ on existing
(and familiar!) tools and workflows. As such, the toolkit is small, light, and
has a /very/ friendly learning curve.
* Synopsis
I have [[https://raw.githubusercontent.com/dkogan/vnlog/master/dji-tsla.tar.gz][two sets of historical stock data]], from the start of 2018 until now
(2018/11):
#+BEGIN_SRC sh :results output :exports both
< dji.vnl head -n 4
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume
: 2018-11-15 25061.48 25354.56 24787.79 25289.27 25289.27 383292840
: 2018-11-14 25388.08 25501.29 24935.82 25080.50 25080.50 384240000
: 2018-11-13 25321.21 25511.03 25193.78 25286.49 25286.49 339690000
And
#+BEGIN_SRC sh :results output :exports both
< tsla.vnl head -n 4
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume
: 2018-11-15 342.33 348.58 339.04 348.44 348.44 4486339
: 2018-11-14 342.70 347.11 337.15 344.00 344.00 5036300
: 2018-11-13 333.16 344.70 332.20 338.73 338.73 5448600
I can add whitespace to make the headers more legible by humans:
#+BEGIN_SRC sh :results output :exports both
< dji.vnl head -n 4 | vnl-align
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume
: 2018-11-15 25061.48 25354.56 24787.79 25289.27 25289.27 383292840
: 2018-11-14 25388.08 25501.29 24935.82 25080.50 25080.50 384240000
: 2018-11-13 25321.21 25511.03 25193.78 25286.49 25286.49 339690000
I can pull out the closing prices:
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-filter -p Close | head -n4
#+END_SRC
#+RESULTS:
: # Close
: 25289.27
: 25080.50
: 25286.49
=vnl-filter= is primarily a wrapper around =awk= or =perl=, allowing the user to
reference columns by name. I can then plot the closing prices:
#+BEGIN_SRC sh :results file link :exports both
< dji.vnl vnl-filter -p Close |
feedgnuplot --lines --unset grid
#+END_SRC
#+RESULTS:
[[file:guide-1.svg]]
Here I kept /only/ the closing price column, so the x-axis is just the row
index. The data was in reverse chronological order, so this plot is also in
reverse chronological order. Let's fix that:
#+BEGIN_SRC sh :results file link :exports both
< dji.vnl vnl-sort -k Date |
vnl-filter -p Close |
feedgnuplot --lines --unset grid
#+END_SRC
#+RESULTS:
[[file:guide-2.svg]]
The =vnl-sort= tool (and most of the other =vnl-xxx= tools) are wrappers around
the core tools already available on the system (such as =sort=, in this case).
With the primary difference being reading/writing vnlog, and referring to
columns by name.
We now have the data in the correct order, but it'd be nice to see the actual
dates on the x-axis. While we're at it, let's label the axes too:
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-filter -p Date,Close | head -n4
#+END_SRC
#+RESULTS:
: # Date Close
: 2018-11-15 25289.27
: 2018-11-14 25080.50
: 2018-11-13 25286.49
#+BEGIN_SRC sh :results file link :exports both
< dji.vnl vnl-sort -k Date |
vnl-filter -p Date,Close |
feedgnuplot --lines --unset grid --timefmt %Y-%m-%d --domain \
--xlabel 'Date' --ylabel 'Price ($)'
#+END_SRC
#+RESULTS:
[[file:guide-3.svg]]
What was the highest value of the Dow-Jones index, and when did it happen?
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-sort -rgk Close |
head -n2 |
vnl-align
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume
: 2018-10-03 26833.47 26951.81 26789.08 26828.39 26828.39 280130000
Alrighty. Looks like the high was in October. Let's zoom in on that month:
#+BEGIN_SRC sh :results file link :exports both
< dji.vnl vnl-sort -k Date |
vnl-filter 'Date ~ /2018-10/' -p Date,Close |
feedgnuplot --lines --unset grid --timefmt %Y-%m-%d --domain \
--xlabel 'Date' --ylabel 'Price ($)'
#+END_SRC
#+RESULTS:
[[file:guide-4.svg]]
OK. Is this thing volatile? What was the largest single-day gain?
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-filter -p '.,d=diff(Close)' |
head -n4 |
vnl-align
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume d
: 2018-11-15 25061.48 25354.56 24787.79 25289.27 25289.27 383292840 -
: 2018-11-14 25388.08 25501.29 24935.82 25080.50 25080.50 384240000 -208.77
: 2018-11-13 25321.21 25511.03 25193.78 25286.49 25286.49 339690000 205.99
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-filter -p '.,d=diff(Close)' |
vnl-sort -rgk d |
head -n2 |
vnl-align
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume d
: 2018-02-02 26061.79 26061.79 25490.66 25520.96 25520.96 522880000 1175.21
Whoa. So the best single-gain day was 2018-02-02: the dow gained 1175.21 points
between closing on Feb 1 and Feb 2. But it actually lost ground that day! What
if I looked at the difference between the opening and closing in a single day?
#+BEGIN_SRC sh :results output :exports both
< dji.vnl vnl-filter -p '.,d=Close-Open' |
vnl-sort -rgk d |
head -n2 |
vnl-align
#+END_SRC
#+RESULTS:
: # Date Open High Low Close AdjClose Volume d
: 2018-02-06 24085.17 24946.23 23778.74 24912.77 24912.77 823940000 827.6
I guess by that metric 2018-02-06 was better. Let's join the Dow-jones index
data and the TSLA data, and let's look at them together:
#+BEGIN_SRC sh :results output :exports both
vnl-join --vnl-autosuffix dji.vnl tsla.vnl -j Date |
head -n4 |
vnl-align
#+END_SRC
#+RESULTS:
: # Date Open_dji High_dji Low_dji Close_dji AdjClose_dji Volume_dji Open_tsla High_tsla Low_tsla Close_tsla AdjClose_tsla Volume_tsla
: 2018-11-15 25061.48 25354.56 24787.79 25289.27 25289.27 383292840 342.33 348.58 339.04 348.44 348.44 4486339
: 2018-11-14 25388.08 25501.29 24935.82 25080.50 25080.50 384240000 342.70 347.11 337.15 344.00 344.00 5036300
: 2018-11-13 25321.21 25511.03 25193.78 25286.49 25286.49 339690000 333.16 344.70 332.20 338.73 338.73 5448600
#+BEGIN_SRC sh :results output :exports both
vnl-join --vnl-autosuffix dji.vnl tsla.vnl -j Date |
vnl-filter -p '^Close' |
head -n4 |
vnl-align
#+END_SRC
#+RESULTS:
: # Close_dji Close_tsla
: 25289.27 348.44
: 25080.50 344.00
: 25286.49 338.73
#+BEGIN_SRC sh :results file link :exports both
vnl-join --vnl-autosuffix dji.vnl tsla.vnl -j Date |
vnl-filter -p '^Close' |
feedgnuplot --domain --points --unset grid \
--xlabel 'DJI price ($)' --ylabel 'TSLA price ($)'
#+END_SRC
#+RESULTS:
[[file:guide-5.svg]]
Huh. Apparently there's no obvious, strong correlation between TSLA and
Dow-Jones closing prices. And we saw that with just a few shell commands,
without dropping down into a dedicated analysis system.
* Build and installation
vnlog is a part of Debian/buster and Ubuntu/cosmic (18.10) and later. On those
boxes you can simply
#+BEGIN_EXAMPLE
$ sudo apt install vnlog libvnlog-dev libvnlog-perl python3-vnlog
#+END_EXAMPLE
to get the binary tools, the C API, the perl and python3 interfaces
respectively.
** Install on non-Debian boxes
Most of this is written in an interpreted language, so there's nothing to build
or install, and you can run the tools directly from the source tree:
#+BEGIN_EXAMPLE
$ git clone https://github.com/dkogan/vnlog.git
$ cd vnlog
$ ./vnl-filter .....
#+END_EXAMPLE
The python and perl libraries can be run from the tree by setting the
=PYTHONPATH= and =PERL5LIB= environment variables respectively. For the C
library, you should =make=, and then point your =CFLAGS= and =LDLIBS= and
=LD_LIBRARY_PATH= to the local tree.
If you do want to install to some arbitrary location to simplify the paths, do
this:
#+BEGIN_EXAMPLE
$ make
$ PREFIX=/usr/local make install
#+END_EXAMPLE
This will install /all/ the components into =/usr/local=.
* Description
Vnlog data is nicely readable by both humans and machines. Any time your
application invokes =printf()= for either diagnostics or logging, consider
writing out vnlog-formatted data. You retain human readability, but gain the
power all the =vnl-...= tools provide.
Vnlog tools are designed to be very simple and light. There's an ever-growing
list of other tools that do vaguely the same thing. Some of these:
- https://github.com/BurntSushi/xsv
- https://csvkit.readthedocs.io/
- https://github.com/johnkerl/miller
- https://github.com/jqnatividad/qsv
- https://github.com/greymd/teip
- https://github.com/eBay/tsv-utils-dlang
- https://www.gnu.org/software/datamash/
- https://stedolan.github.io/jq/
- https://github.com/benbernard/RecordStream
- https://github.com/dinedal/textql
- https://www.visidata.org/
- http://harelba.github.io/q/
- https://github.com/BatchLabs/charlatan
- https://github.com/dbohdan/sqawk
Many of these provide facilities to run various analyses, and others focus on
data types that aren't just a table (json for instance). Vnlog by contrast
doesn't analyze anything, and targets the most trivial possible data format.
This makes it very easy to run any analysis you like in any tool you like. The
main envisioned use case is one-liners, and the tools are geared for that
purpose. The above mentioned tools are much more powerful than vnlog, so they
could be a better fit for some use cases. I claim that
- 90% of the time you want to do simple things, and vnlog is a great fit for the
task
- If you really do need to do something complex, you shouldn't be in the shell
writing oneliners anymore, and a fully-fledged analysis system (numpy, etc) is
more appropriate
In the spirit of doing as little as possible, the provided tools are wrappers
around tools you already have and are familiar with. The provided tools are:
- =vnl-filter= is a tool to select a subset of the rows/columns in a vnlog
and/or to manipulate the contents. This is an =awk= wrapper where the fields
can be referenced by name instead of index. 20-second tutorial:
#+BEGIN_SRC sh :results none :exports code
vnl-filter -p col1,col2,colx=col3+col4 'col5 > 10' --has col6
#+END_SRC
will read the input, and produce a vnlog with 3 columns: =col1= and =col2=
from the input, and a column =colx= that's the sum of =col3= and =col4= in the
input. Only those rows for which /both/ =col5 > 10= is true /and/ that have a
non-null value for =col6= will be output. A null entry is signified by a
single =-= character.
#+BEGIN_SRC sh :results none :exports code
vnl-filter --eval '{s += x} END {print s}'
#+END_SRC
#+RESULTS:
will evaluate the given awk program on the input, but the column names work as
you would hope they do: if the input has a column named =x=, this would
produce the sum of all values in this column.
- =vnl-sort=, =vnl-uniq=, =vnl-join=, =vnl-tail=, =vnl-ts= are wrappers around
the corresponding commandline tools. These work exactly as you would expect
also: the columns can be referenced by name, and the legend comment is handled
properly. These are wrappers, so all the commandline options those tools have
"just work" (except options that don't make sense in the context of vnlog). As
an example, =vnl-tail -f= will follow a log: data will be read by =vnl-tail=
as it is written into the log (just like =tail -f=, but handling the legend
properly). And you already know how to use these tools without even reading
the manpages! Note: I use the Linux kernel and the tools from GNU Coreutils
exclusively, but this all has been successfully tested on FreeBSD and OSX
also. Please let me know if something doesn't work.
- =vnl-align= aligns vnlog columns for easy interpretation by humans. The
meaning is unaffected
- =Vnlog::Parser= is a simple perl library to read a vnlog
- =vnlog= is a simple python library to read a vnlog. Both python2 and python3
are supported
- =libvnlog= is a C library to simplify reading and writing a vnlog. Clearly all
you /really/ need for writing is =printf()=, but this is useful if we have
lots of columns, many containing null values in any given row, and/or if we
have parallel threads writing to a log. In my usage I have hundreds of columns
of sparse data, so this is handy
- =vnl-make-matrix= converts a one-point-per-line vnlog to a matrix of data.
I.e.
#+BEGIN_EXAMPLE
$ cat 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
$ < dat.vnl vnl-filter -p i,x | vnl-make-matrix --outdir /tmp
Writing to '/tmp/x.matrix'
$ cat /tmp/x.matrix
1 2 3
4 5 6
7 8 9
10 11 12
#+END_EXAMPLE
All the tools have manpages that contain more detail. And more tools will
probably be added with time.
* Format details
The high-level description of the vnlog format from [[#Summary][above]] is sufficient to
read/write "normal" vnlog data, but there are a few corner cases that should be
mentioned. To reiterate, the format description from above describes vnlog as:
- A whitespace-separated table of ASCII human-readable text
- A =#= character starts a comment that runs to the end of the line (like in
many scripting languages)
- The first line that begins with a single =#= (not =##= or =#!=) is a /legend/,
naming each column. This is required, and the field names that appear here are
referenced by all the tools.
- Empty fields reported as =-=
For a few years now I've been using these tools myself, and supporting others as
they were passing vnlog data around. In the process I've encountered some
slightly-weird data, and patched the tools to accept it. So today the included
vnlog tools are /very/ permissive, and accept any vnlog data that can possibly
be accepted. Other vnlog tools may not be quite as permissive, and may not be
able to interpret "weird" data. Points of note, describing the included vnlog
tools:
- Leading and trailing whitespace is ignored. Everywhere. So this data file will
be read properly, with the =x= column containing 1 and 3:
#+begin_example
# x y
1 2
3 4
#+end_example
- Empty (or whitespace-only) lines anywhere are ignored, and treated as a
comment
- An initial =#= comment without field names is treated as a comment, and we
continue looking for the legend in the following lines. So this data file will
be read properly:
#+begin_example
## comment
#
# x y
1 2
3 4
#+end_example
- Trailing comments are supported, like in most scripting languages. So this
data file will be read properly:
#+begin_example
# x y
1 2 # comment
3 4
#+end_example
- Field names are /very/ permissive: anything that isn't whitespace is
supported. So this data file will be read properly:
#+begin_example
# x y # 1+ -
1 2 3 4 5
11 12 13 14 15
#+end_example
We can pull out the =#= and =1+= and =-= columns:
#+begin_src sh
vnl-filter -p '#,1+,-'
#+end_src
And we can even operate on them, if we use whitespace to indicate field
boundaries:
#+begin_src sh
vnl-filter -p 'x=1+ + 5'
#+end_src
Note that this implies that trailing comments in a legend line are /not/
supported: the extra =#= characters will be used for field names. Field names
containing =,= or === are currently not accepted by =vnl-filter=, but /are/
accepted by the other tools (=vnl-sort= and such). I'll make =vnl-filter= able
to work with those field names too, eventually, but as a user, the simplest
thing to do is to not pass around data with such field names.
- Duplicated labels are supported whenever possible. So
#+begin_example
# x y z z
1 2 3 4
11 12 13 14
#+end_example
will work just fine, unless we're operating on =z=. With this data, both of
these commands work:
#+begin_src sh
vnl-filter -p x
vnl-filter -p z
#+end_src
Picking =z= selects both of the =z= columns. But neither of these commands can
work with the non-unique =z= column:
#+begin_src sh
vnl-filter -p s=z+1
vnl-sort -k z
#+end_src
* Workflows and recipes
** Storing disjoint data
A common use case is a complex application that produces several semi-related
subsets of data at once. Example: a moving vehicle is reporting both its own
position and the observed positions of other vehicles; at any given time any
number of other vehicles may be observed. Two equivalent workflows are possible:
- a single unified vnlog stream for /all/ the data
- several discrete vnlog streams for each data subset
Both are valid approaches
*** One unified vnlog stream
Here the application produces a /single/ vnlog that contains /all/ the columns,
from /all/ the data subsets. In any given row, many of the columns will be empty
(i.e. contain only =-= ). For instance, a row describing a vehicle own position
will not have data about any observations, and vice versa. It is inefficient to
store all the extra =-= but it makes many things much nicer, so it's often worth
it. =vnl-filter= can be used to pull out the different subsets. Sample
=joint.vnl=:
#+BEGIN_EXAMPLE
# time x_self x_observation
1 10 -
2 20 -
2 - 100
3 30 -
3 - 200
3 - 300
#+END_EXAMPLE
Here we have 3 instances in time. We have no observations at =time= 1, one
observation at =time= 2, and two observations at =time= 3. We can use
=vnl-filter= to pull out the data we want:
#+BEGIN_EXAMPLE
$ < joint.vnl vnl-filter -p time,self
# time x_self
1 10
2 20
2 -
3 30
3 -
3 -
#+END_EXAMPLE
If we only care about our own positions, the =+= modifier in picked columns in
=vnl-filter= is very useful here:
#+BEGIN_EXAMPLE
$ < joint.vnl vnl-filter -p time,+self
# time x_self
1 10
2 20
3 30
$ < joint.vnl vnl-filter -p time,+observation
# time x_observation
2 100
3 200
3 300
#+END_EXAMPLE
Note that the default is =--skipempty=, so if we're /only/ looking at =x_self=
for instance, then we don't even need to =+= modifier:
#+begin_example
$ < joint.vnl vnl-filter -p self
# x_self
10
20
30
#+end_example
Also, note that the =vnlog= C interface works very nicely to produce these
datafiles:
- You can define lots and lots of columns, but only fill some of them before
calling =vnlog_emit_record()=. The rest will be set to =-=.
- You can create multiple contexts for each type of data, and you can populate
them with data independently. And when calling =vnlog_emit_record_ctx()=,
you'll get a record with data for just that context.
*** Several discrete vnlog streams
Conversely, the application can produce /separate/ vnlog streams for /each/
subset of data. Depending on what is desired, exactly, =vnl-join= can be used to
re-join them:
#+BEGIN_EXAMPLE
$ cat self.vnl
# time x_self
1 10
2 20
3 30
$ cat observations.vnl
# time x_observation
2 100
3 200
3 300
$ vnl-join -j time -a- self.vnl observations.vnl
# time x_self x_observation
1 10 -
2 20 100
3 30 200
3 30 300
#+END_EXAMPLE
** Data statistics
A common need is to compute basic statistics from your data. Many of the
alternative toolkits listed above provide built-in facilities to do this, but
vnlog does not: it's meant to be unixy, where each tool has very limited scope.
Thus you can either do this with =awk= like you would normally, or you can use
other standalone tools to perform the needed computations. For instance, I can
generate some data:
#+BEGIN_EXAMPLE
$ seq 2 100 | awk 'BEGIN {print "# x"} {print log($1)}' > /tmp/log.vnl
#+END_EXAMPLE
Then I can compute the mean with =awk=:
#+BEGIN_EXAMPLE
$ < /tmp/log.vnl vnl-filter --eval '{sum += x} END {print sum/NR}'
3.67414
#+END_EXAMPLE
Or I can compute the mean (and other stuff) with a separate standalone tool:
#+BEGIN_EXAMPLE
$ < /tmp/log.vnl ministat
x <stdin>
+----------------------------------------------------------------------------+
| xx |
| x xxxxxxx |
| xx xxxxxxxxxxxx|
| x x xxxxxxxxxxxxxxxxxxxxxxx|
|x x x x x x x x x xx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
| |_______________A____M___________| |
+----------------------------------------------------------------------------+
N Min Max Median Avg Stddev
x 99 0.693147 4.60517 3.93183 3.6741353 0.85656382
#+END_EXAMPLE
=ministat= is not a part of the vnlog toolkit, but the vnlog format is generic
so it works just fine.
** Powershell-style filtering of common shell commands
Everything about vnlog is generic and simple, so it's easy to use it to process
data that wasn't originally meant to be used this way. For instance filtering
the output of =ls -l= to report only file names and sizes, skipping directories,
and sorting by file sizes:
#+BEGIN_EXAMPLE
$ ls -l
total 320
-rw-r--r-- 1 dima dima 5044 Aug 25 15:04 Changes
-rw-r--r-- 1 dima dima 12749 Aug 25 15:04 Makefile
-rw-r--r-- 1 dima dima 69789 Aug 25 15:04 README.org
-rw-r--r-- 1 dima dima 33781 Aug 25 15:04 README.template.org
-rw-r--r-- 1 dima dima 5359 Aug 25 15:04 b64_cencode.c
drwxr-xr-x 4 dima dima 4096 Aug 25 15:04 completions
drwxr-xr-x 3 dima dima 4096 Aug 25 15:04 lib
drwxr-xr-x 3 dima dima 4096 Aug 25 15:04 packaging
drwxr-xr-x 2 dima dima 4096 Aug 25 15:04 test
-rwxr-xr-x 1 dima dima 5008 Aug 25 15:04 vnl-align
-rwxr-xr-x 1 dima dima 56637 Aug 25 15:04 vnl-filter
-rwxr-xr-x 1 dima dima 5678 Aug 25 15:04 vnl-gen-header
-rwxr-xr-x 1 dima dima 29815 Aug 25 15:04 vnl-join
-rwxr-xr-x 1 dima dima 3631 Aug 25 15:04 vnl-make-matrix
-rwxr-xr-x 1 dima dima 8372 Aug 25 15:04 vnl-sort
-rwxr-xr-x 1 dima dima 5822 Aug 25 15:04 vnl-tail
-rwxr-xr-x 1 dima dima 4439 Aug 25 15:04 vnl-ts
-rw-r--r-- 1 dima dima 559 Aug 25 15:04 vnlog-base64.h
-rw-r--r-- 1 dima dima 8169 Aug 25 15:04 vnlog.c
-rw-r--r-- 1 dima dima 12677 Aug 25 15:04 vnlog.h
$ (echo '# permissions num_links user group size month day time name';
ls -l | tail -n +2) |
vnl-filter 'permissions !~ "^d"' -p name,size |
vnl-sort -gk size |
vnl-align
# name size
vnlog-base64.h 559
vnl-make-matrix 3631
vnl-ts 4439
vnl-align 5008
Changes 5044
b64_cencode.c 5359
vnl-gen-header 5678
vnl-tail 5822
vnlog.c 8169
vnl-sort 8372
vnlog.h 12677
Makefile 12749
vnl-join 29815
README.template.org 33781
vnl-filter 56637
README.org 69789
#+END_EXAMPLE
With a bit of shell manipulation, these tools can be applied to a whole lot of
different data streams that know nothing of vnlog.
* C interface
** Writing vnlog files
*** Basic usage
For most uses, vnlog files are simple enough to be generated with plain prints.
But then each print statement has to know which numeric column we're populating,
which becomes effortful with many columns. In my usage it's common to have a
large parallelized C program that's writing logs with hundreds of columns where
any one record would contain only a subset of the columns. In such a case, it's
helpful to have a library that can output the log files. This is available.
Basic usage looks like this:
In a shell:
#+BEGIN_SRC sh :results none :exports code
vnl-gen-header 'int w' 'uint8_t x' 'char* y' 'double z' 'void* binary' > vnlog_fields_generated.h
#+END_SRC
#+RESULTS:
In a C program test.c:
#+BEGIN_SRC C
#include "vnlog_fields_generated.h"
int main()
{
vnlog_emit_legend();
vnlog_set_field_value__w(-10);
vnlog_set_field_value__x(40);
vnlog_set_field_value__y("asdf");
vnlog_emit_record();
vnlog_set_field_value__z(0.3);
vnlog_set_field_value__x(50);
vnlog_set_field_value__w(-20);
vnlog_set_field_value__binary("\x01\x02\x03", 3);
vnlog_emit_record();
vnlog_set_field_value__w(-30);
vnlog_set_field_value__x(10);
vnlog_set_field_value__y("whoa");
vnlog_set_field_value__z(0.5);
vnlog_emit_record();
return 0;
}
#+END_SRC
Then we build and run, and we get
#+BEGIN_EXAMPLE
$ cc -o test test.c -lvnlog
$ ./test
# w x y z binary
-10 40 asdf - -
-20 50 - 0.2999999999999999889 AQID
-30 10 whoa 0.5 -
#+END_EXAMPLE
The binary field in base64-encoded. This is a rarely-used feature, but sometimes
you really need to log binary data for later processing, and this makes it
possible.
So you
1. Generate the header to define your columns
2. Call =vnlog_emit_legend()=
3. Call =vnlog_set_field_value__...()= for each field you want to set in that
row.
4. Call =vnlog_emit_record()= to write the row and to reset all fields for the
next row. Any fields unset with a =vnlog_set_field_value__...()= call are
written as null: =-=
This is enough for 99% of the use cases. Things get a bit more complex if we
have have threading or if we have multiple vnlog ouput streams in the same
program. For both of these we use vnlog /contexts/.
*** Contexts
To support independent writing into the same vnlog (possibly by multiple
threads; this is reentrant), each log-writer should create a context, and use it
when talking to vnlog. The context functions will make sure that the fields in
each context are independent and that the output records won't clobber each
other:
#+BEGIN_SRC C
void child_writer( // the parent context also writes to this vnlog. Pass NULL to
// use the global one
struct vnlog_context_t* ctx_parent )
{
struct vnlog_context_t ctx;
vnlog_init_child_ctx(&ctx, ctx_parent);
while(records)
{
vnlog_set_field_value_ctx__xxx(&ctx, ...);
vnlog_set_field_value_ctx__yyy(&ctx, ...);
vnlog_set_field_value_ctx__zzz(&ctx, ...);
vnlog_emit_record_ctx(&ctx);
}
vnlog_free_ctx(&ctx); // required only if we have any binary fields
}
#+END_SRC
If we want to have multiple independent vnlog writers to /different/ streams
(with different columns and legends), we do this instead:
=file1.c=:
#+BEGIN_SRC C
#include "vnlog_fields_generated1.h"
void f(void)
{
// Write some data out to the default context and default output (STDOUT)
vnlog_emit_legend();
...
vnlog_set_field_value__xxx(...);
vnlog_set_field_value__yyy(...);
...
vnlog_emit_record();
}
#+END_SRC
=file2.c=:
#+BEGIN_SRC C
#include "vnlog_fields_generated2.h"
void g(void)
{
// Make a new session context, send output to a different file, write
// out legend, and send out the data
struct vnlog_context_t ctx;
vnlog_init_session_ctx(&ctx);
FILE* fp = fopen(...);
vnlog_set_output_FILE(&ctx, fp);
vnlog_emit_legend_ctx(&ctx);
...
vnlog_set_field_value__a(...);
vnlog_set_field_value__b(...);
...
vnlog_free_ctx(&ctx); // required only if we have any binary fields
vnlog_emit_record();
}
#+END_SRC
Note that it's the user's responsibility to make sure the new sessions go to a
different =FILE= by invoking =vnlog_set_output_FILE()=. Furthermore, note that
the included =vnlog_fields_....h= file defines the fields we're writing to; and
if we have multiple different vnlog field definitions in the same program (as in
this example), then the different writers /must/ live in different source files.
The compiler will barf if you try to =#include= two different
=vnlog_fields_....h= files in the same source.
*** Remaining APIs
- =vnlog_printf(...)= and =vnlog_printf_ctx(ctx, ...)= write to a pipe like
=printf()= does. This exists primarily for comments.
- =vnlog_clear_fields_ctx(ctx, do_free_binary)= clears out the data in a context
and makes it ready to be used for the next record. It is rare for the user to
have to call this manually. The most common case is handled automatically
(clearing out a context after emitting a record). One area where this is useful
is when making a copy of a context:
#+BEGIN_SRC C
struct vnlog_context_t ctx1;
// .... do stuff with ctx1 ... add data to it ...
struct vnlog_context_t ctx2 = ctx1;
// ctx1 and ctx2 now both have the same data, and the same pointers to
// binary data. I need to get rid of the pointer references in ctx1
vnlog_clear_fields_ctx(&ctx1, false);
#+END_SRC
- =vnlog_free_ctx(ctx)= frees memory for an vnlog context. Do this before
throwing the context away. Currently this is only needed for context that have
binary fields, but this should be called for all contexts anyway, in case this
changes in a later revision
** Reading vnlog files
The basic usage goes like this:
#+begin_src c
#include <stdio.h>
#include <stdbool.h>
#include <vnlog/vnlog-parser.h>
bool parse_vnlog(const char* filename)
{
FILE* fp = fopen(filename);
if(fp == NULL)
return false;
vnlog_parser_t ctx;
if(VNL_OK != vnlog_parser_init(&ctx, fp))
return false;
// String in the "time" column for the most-recently-parsed row
const char*const* time_record = vnlog_parser_record_from_key(&ctx, "time");
if(time_record == NULL)
{
vnlog_parser_free(&ctx);
return false;
}
int i_record = 0;
vnlog_parser_result_t result;
while(VNL_OK == (result = vnlog_parser_read_record(&ctx, fp)))
{
for(int i=0; i<ctx.Ncolumns; i++)
printf("Record %d: %s = %s\n",
i_record,
ctx.record[i].key, ctx.record[i].value);
printf("Record %d: time = %s\n",
i_record, *time_record);
i_record++;
}
vnlog_parser_free(&ctx);
return true;
}
#+end_src
The usage should be clear from this example. See =vnlog-parser.h= for details.
** Base64 interface
The C interface supports writing base64-encoded binary data using Chris Venter's
libb64. The base64-encoder used here was slightly modified: the output appears
all on one line, making is suitable to appear in a vnlog field. If we're writing
a vnlog with =printf()= directly without using the =vnlog.h= interface described
above, we allow this modified base64 encoder to be invoked by itself. Usage:
#+BEGIN_SRC C
void* binary_buffer = ...;
int binary_buffer_len = ...;
char base64_buffer[vnlog_base64_dstlen_to_encode(binary_buffer_len)];
vnlog_base64_encode( base64_buffer, sizeof(base64_buffer),
binary_buffer, binary_buffer_len );
#+END_SRC
Clearly the above example allocates the base64 buffer on the stack, so it's only
suitable for small-ish data chunks. But if you have lots and lots of data,
probably writing it as base64 into a vnlog isn't the best thing to do.
* Python interface
Reading vnlog data into a python program is simple. The =vnlog= Python module
provides three different ways to do that:
1. slurp the whole thing into a numpy array using the =slurp()= function. Basic
usage:
#+begin_src python
import vnlog
arr,list_keys,dict_key_index = \
vnlog.slurp(filename_or_fileobject)
#+end_src
This parses out the legend, and then calls =numpy.loadtxt()=. Null data values
(=-=) are not supported
2. Iterate through the records: =vnlog= class, used as an iterator. Basic usage:
#+begin_src python
import vnlog
for d in vnlog.vnlog(filename_or_fileobject):
print(d['time'],d['height'])
#+end_src
Null data values are represented as =None=
3. Parse incoming lines individually: =vnlog= class, using the =parse()= method.
Basic usage:
#+begin_src python
import vnlog
parser = vnlog.vnlog()
for l in file:
parser.parse(l)
d = parser.values_dict()
if not d:
continue
print(d['time'],d['height'])
#+end_src
Most of the time you'd use options 1 or 2 above. Option 3 is the most general,
but also the most verbose and slowest.
** Structured dtypes in slurp()
The =slurp()= example from above is simple: it doesn't specify a =dtype= (data
type), so =float= (64-bit IEEE-754 float) is used by default, for /all/ the data
in the array. You can specify another =dtype=, for instance:
#+begin_src python
arr,list_keys,dict_key_index = \
vnlog.slurp(filename_or_fileobject,
dtype = int)
#+end_src
You can also specify a [[https://numpy.org/doc/stable/user/basics.rec.html][/structured/ dtype]] to pull out different columns, with
different individual dtypes. Most notably, this allows interpreting some columns
as strings. Let's say you have a very common vnlog data, such as this "data.vnl":
#+begin_example
# image x y z temperature
image1.png 1 2 5 34
image2.png 3 4 1 35
#+end_example
Reading this with a simple =vnlog.slurp()= will fail: the filenames are not
parseable as numerical values. We can instead do this:
#+begin_src python
dtype = np.dtype([ ('image', 'U16'),
('x y z', int, (3,)),
('temperature', float), ])
arr = vnlog.slurp("data.vnl", dtype=dtype)
#+end_src
This will read the image filename, the xyz points and the temperature into
different sub-arrays, with different types each. Accessing the result looks
like this:
#+begin_example
print(arr['image'])
---> array(['image1.png', 'image2.png'], dtype='<U16')
print(arr['x y z'])
---> array([[1, 2, 5],
[3, 4, 1]])
print(arr['temperature'])
---> array([34., 35.])
#+end_example
Notes:
- The given structured dtype defines both how to organize the data, and which
data to extract. So it can be used to read in only a subset of the available
columns. Here I could have omitted the 'temperature' column, for instance
- Sub-arrays are allowed. In the example I could say either
#+begin_src python
dtype = np.dtype([ ('image', 'U16'),
('x y z', int, (3,)),
('temperature', float), ])
#+end_src
or
#+begin_src python
dtype = np.dtype([ ('image', 'U16'),
('x', int),
('y', int),
('z', int),
('temperature', float), ])
#+end_src
The latter would read =x=, =y=, =z= into separate, individual arrays. Sometime
we want this, sometimes not.
- Nested structured dtypes are not allowed. Fields inside other fields are not
supported, since it's not clear how to map that to a flat vnlog legend
- If a structured dtype is given, =slurp()= returns the array only, since the
field names are already available in the dtype
* numpy interface
If we need to read data into numpy specifically, nicer tools are available than
the generic =vnlog= Python module. The built-in =numpy.loadtxt= =numpy.savetxt=
functions work well (with the caveat that =numpy.loadtxt()= should be followed
by =numpysane.atleast_dims(..., -2)= to make sure that a data array of shape
=(Nrows,Ncols)= is returned even if =Nrows==1=. For example to write to standard
output a vnlog with fields =a=, =b= and =c=:
#+BEGIN_SRC python
numpy.savetxt(sys.stdout, array, fmt="%g", header="a b c")
#+END_SRC
Note that numpy automatically adds the =#= to the header. To read a vnlog from a
file on disk, do something like
#+BEGIN_SRC python
array = numpysane.atleast_dims(numpy.loadtxt('data.vnl'), -2)
#+END_SRC
These functions know that =#= lines are comments, but don't interpret anything
as field headers. That's easy to do, so I'm not providing any helper libraries.
I might do that at some point, but in the meantime, patches are welcome.
* Compatibility
I use GNU/Linux-based systems exclusively, but everything has been tested
functional on FreeBSD and OSX in addition to Debian, Ubuntu and CentOS. I can
imagine there's something I missed when testing on non-Linux systems, so please
let me know if you find any issues.
* Caveats and bugs
These tools are meant to be simple, so some things are hard requirements. A big
one is that columns are whitespace-separated. There is /no/ mechanism for
escaping or quoting whitespace into a single field. I think supporting something
like that is more trouble than it's worth.
* Manpages
** vnl-filter
#+BEGIN_EXAMPLE
xxx-manpage-vnl-filter-xxx
#+END_EXAMPLE
** vnl-align
#+BEGIN_EXAMPLE
xxx-manpage-vnl-align-xxx
#+END_EXAMPLE
** vnl-sort
#+BEGIN_EXAMPLE
xxx-manpage-vnl-sort-xxx
#+END_EXAMPLE
** vnl-join
#+BEGIN_EXAMPLE
xxx-manpage-vnl-join-xxx
#+END_EXAMPLE
** vnl-tail
#+BEGIN_EXAMPLE
xxx-manpage-vnl-tail-xxx
#+END_EXAMPLE
** vnl-ts
#+BEGIN_EXAMPLE
xxx-manpage-vnl-ts-xxx
#+END_EXAMPLE
** vnl-uniq
#+BEGIN_EXAMPLE
xxx-manpage-vnl-uniq-xxx
#+END_EXAMPLE
** vnl-gen-header
#+BEGIN_EXAMPLE
xxx-manpage-vnl-gen-header-xxx
#+END_EXAMPLE
** vnl-make-matrix
#+BEGIN_EXAMPLE
xxx-manpage-vnl-make-matrix-xxx
#+END_EXAMPLE
* Repository
https://github.com/dkogan/vnlog/
* Authors
Dima Kogan (=dima@secretsauce.net=) wrote this toolkit for his work at the Jet
Propulsion Laboratory, and is delighted to have been able to release it
publically
Chris Venter (=chris.venter@gmail.com=) wrote the base64 encoder
* License and copyright
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.
Copyright 2016-2017 California Institute of Technology
Copyright 2017-2018 Dima Kogan (=dima@secretsauce.net=)
=b64_cencode.c= comes from =cencode.c= in the =libb64= project. It is written by
Chris Venter (=chris.venter@gmail.com=) who placed it in the public domain. The
full text of the license is in that file.
|