通过Shell命令通过管道加快gnuplot列迭代的速度 [英] Speed up gnuplot column iteration on input piped through a shell command

查看:71
本文介绍了通过Shell命令通过管道加快gnuplot列迭代的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本可以转换原始数据以与gnuplot交互使用. Scipt接受了一些参数,这些参数使我可以对数据进行一些过滤.原则上,可以通过以下脚本进行仿真:

I have a script that converts raw data for interactive use with gnuplot. The scipt takes arguments that let me apply some filtering on the data. In principle, it could be emulated by this script:

import time, sys
print('Generating data...', file=sys.stderr)
time.sleep(1)
print('x', '100*x', 'x**3', '2**x')
for x in range(*map(int, sys.argv[1:3])):
  print(x, 100*x, x**3, 2**x)

我通过将shell命令传递给gnuplot并遍历列来绘制列中的一系列数据:

I plot the series of data in columns by piping the shell command to gnuplot and iterating over columns:

gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w l t columnhead(i)
Generating data...
Generating data...
Generating data...
gnuplot>

当前,当我注意到脚本花费太长时间无法多次运行时,我在gnuplot之外执行该脚本并将输出保存到文件中.但是,每当我想更改脚本的参数时,都必须这样做.

Currently, when I notice that the script takes too long to run it multiple times, I execute it outside of gnuplot and save the output to a file. However, it is cumbersome having to do it whenever I want to change the arguments to the script.

我希望gnuplot仅执行一次'< python3 pipe_data.py',以便在屏幕上仅打印一个Generating data....这可能吗?

I would like gnuplot to execute '< python3 pipe_data.py' only once, so that only one Generating data... is printed on the screen. Is this possible?

理想情况下,gnuplot会缓存以<开头的特殊文件名的内容.这样,可以调整图的外观,而无需重新生成数据,例如:

Ideally, gnuplot would cache contents of special filenames starting with a <. This way it would be possible to tweak the appearance of the plot, without regenerating the data, e.g.:

gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w l t columnhead(i)
Generating data...
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w lp t columnhead(i)
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 5 12' u 1:i w lp t columnhead(i)
Generating data...
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w p t columnhead(i)
gnuplot>

当原始数据更改时,这可能会成为问题,gnuplot无法知道这一点.但是我仍然希望有某种方法可以达到这种效果.如果不仅仅使用gnuplot,那么也许可以使用一些外部工具?

This could become problematic when the raw data changes, gnuplot would have no way of knowing this. But I still hope there is some way to achieve this effect. If not with just gnuplot, then maybe with some external tools?

出于记录,我使用gnuplot v4.6.

For the record, I use gnuplot v4.6.

推荐答案

我想出了一个bash脚本,可以为我解决所有问题:

I came up with a bash script that solves all the issues for me:

  • 遍历列时,处理数据的脚本仅运行一次
  • 调整图表显示时,无需等待脚本再次运行
  • 我可以将其他参数传递给脚本并缓存结果
  • 我无需等待就可以回到以前的论点
  • 它可以与任何脚本一起使用,无论它需要多少参数.
  • 如果任何文件(脚本或数据)发生更改,它将被提取并重新运行
  • 它是完全透明的,不需要其他设置或调整
  • 我不必担心清理,因为系统会为我完成

我将其命名为$(用于现金/缓存),将其命名为chmod u+x,并将其放置在PATH中:

I named it $ (for cash/cache), chmod u+x'd it, and placed it within PATH:

#!/bin/bash

# hash all arguments
KEY="$@"

# hash last modified dates of any files
for arg in "$@"
do
  if [ -f $arg ]
  then
    KEY+=`date -r "$arg" +\ %s`
  fi
done

# use the hash as a name for temporary file
FILE="/tmp/command_cache.`echo -n "$KEY" | md5sum | cut -c -10`"

# use cached file or execute the command and cache it
if [ -f $FILE ]
then
  cat $FILE
else
  $@ | tee $FILE
fi

现在我可以使用<$而不是<来利用它:

Now I can take advantage of it using <$ instead of <:

> plot for [i=2:4] '<$ python3 pipe_data.py 1 11' u 1:i w l t columnhead(i)
Generating data...
> plot for [i=2:4] '<$ python3 pipe_data.py 1 11' u 1:i w lp t columnhead(i)
> plot for [i=2:4] '<$ python3 pipe_data.py 5 12' u 1:i w lp t columnhead(i)
Generating data...
> plot for [i=2:4] '<$ python3 pipe_data.py 1 11' u 1:i w p t columnhead(i)
>

这篇关于通过Shell命令通过管道加快gnuplot列迭代的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆