这是一个关于为一组并行数据生成图像或任何其他表示的问题。不是关于绘图或 GUI 编程,而是计算位置。
首先,我将解释一下我现在所处的位置,第二张图片和示例显示了我的问题。
当前状态
exampleOne-Easy http://www.wargsang.de/text3935.png
我有一个一维的对象,但它们是通过将它们放在平行的“线”上来对齐的。让我们称这种一维对象为“事件”,它以“持续时间”为时间单位。
这些事件有一个变体,什么都不发生,对象没有数据但有持续时间;一个“间隙”对象。
所以我们得到了一个由事件和间隙组成的模拟对象的时间表,作为三个对象列表很容易处理。
可视化也很简单:遍历列表并根据其持续时间绘制每个对象。
class Event():
def __init__(self, duration, displacement = 0): #displacement is explained in the second example and the core problem of this question
self.duration = duration
self.displacement = displacement
#additional data
def self.draw(self, start_coordinate):
"""draw duration * 10 pixels in black"""
#drawing code using start_coordinate to place the drawn object. see example graphic
return duration * 10
class Gap():
def __init__(self, duration, displacement = 0):
self.duration = duration
self.displacement = displacement
#no data
def self.draw(self, start_coordinate):
"""draw duration * 10 pixels in transparent"""
#drawing code using start_coordinate to place the drawn object. see example graphic
return duration * 10
row_one = [Event(1), Gap(1), Event(1), Gap(1), Event(1), Gap(1), Event(2)]
row_two = [Event(1), Gap(2), Event(1), Event(1), Gap(1), Event(1), Gap(1), ]
row_thr = [Gap(1), Event(1), Gap(1), Event(1), Gap(1), Event(3),]
timetable = [row_one, row_two, row_thr]
for row in timetable:
pixelcounter = 0 # the current position.
for item in row:
ret = item.draw(pixelcounter) #draw on the current position. Get how width the item was
pixelcounter += ret #save width for the next iteration
#instructions to move the "drawing cursor" down a few pixels so the next row does not overlap.
row_one = [ Event(1), #1
Event(0,1), Event(1), #2
Gap(1), #3
Event(1), #4
Gap(1), #5
Event(0,1), Event(1), #6
Event(1), #7
]
row_two = [ Event(1), #1
Event(1), #2
Gap(1), #3
Event(1, 0.5), #4, 0,5 is not important. we can also simply to just ints. The important bit is that it has both values.
Event(1), #5
Event(1), #6
Event(1), #7
]
row_thr = [ Event(1), #1
Event(1), #2
Event(1), #3
Event(1), #4
Event(1), #5
Event(0,1), Event(0,1), Event(1), #6 #Please pay attention to this case.
Event(1), #7
]
最佳答案
我不完全确定,但我认为你想要的是:
0 123 4 567
AbC.D .e FG
A B.CcD EF
A BCD EfgHI
0 12
AAa
Aa B
AaaB
11 11 1 1 1 1 1 1 22 22 2 2 22 22 33 333 3 3 3 3 3 4 44444 4 4 4 45
01 2 34 5678 9 01 23 4 5 6 7 8 9 01 23 4 5 67 89 01 234 5 6 7 8 9 0 12345 6 7 8 90
AAAA BB CCC dd.. EEe Fff.. GGGGGg .... ... HHH .... IIii JJJ ... KKK LLLLl
abbbCCC DDDDDdd .. EEEEE Fff GGG HHH IIIii JJJjj KKKK LLLl Mno. PPP qR SSSSSs TT uuuVV
... AAAAA BBB CC DDDD ... EE FFFF GHhhIIII JJ. K Lll.m.... NNNO ....
...... AAAA .. .... BBB CCCCCc DDDDDd Ee FFFFff G hhhIIIII JJJ KLLLLLll M
.. AAA BBBCcc DD EE .. FFF gH IIIIIi J KKk LL MMMMM NNNNNn OOo PPQQQQ rrr...
AAAAa . BBBBbb CCCCC DDDDDd eeeFFFFF GG HH ..... IIIII JJ K LM.NNNNN .
AAAaaBBB CCCcc DDDDDdd EeFF ... GGgHHHH III JJJJ KKK llMMMm nnnOOOO PPPp ... Q
AAAAA BBBBB CCCC ..... DDD EEEEE FFFff .... GGGG HHHHhh II.... j . .
AAAaa.. BBBBbb CccDDDDD .... EEE .F GgghhhII Jj KKKK ... ... LLll ... MMMM N OooP
.... Aa ..BCCC ..... DDD EEEe FFf ..... GGGG HIIIIIii . JJ .... KKk LL
AAAAAa bbC..... DDDDD .... eeFFFFff GGGGG ... hh IIJJJ KKK L MMMMMmmNNNN
..aBBB CCCCc ..... ..... ... D. E FFFFFff ggHHhiiiJKKKk LLLLL mmmNNNOP Q RRR
AA BbCCCC DD Ee FFFFFff GGGGG HH IIIi JjjK.. LLLll MMMMmm .... . NNNOOOOOoo P
AB CCCCC ..... ddEEEE fffGgg HHHHHhh II jjKKKK LLLL MMMM nn.. OO PPPPPpp QQQQQqq
AAA BBB CCCC DDdd EE FFF gggHh IIIii JJJJ K LLLLl MMm NNOOOO . PP .QQQRRRRR
Timetable.__init__,其余大部分是 pretty-print )。from heapq import merge
from itertools import groupby, cycle, chain
from collections import defaultdict
from operator import attrgetter
from string import ascii_uppercase
# events are processed in this order:
# increasing start time, displacements (duration=0) first, and grouped by row_id
ev_sort_attrs = attrgetter("time", "duration", "row_id")
class Event:
def __init__(self, duration, displacement=0, visible=True, time=None, row_id=None):
self.duration = duration
self.displacement = displacement
self.visible = visible
self.time = time
self.row_id = row_id
self.pos = None
def draw(self, char):
return char * self.duration + char.lower() * self.displacement
def __lt__(self, other):
return ev_sort_attrs(self) < ev_sort_attrs(other)
def Gap(duration):
return Event(duration, visible=False)
class Timetable(list):
def __init__(self, *args):
""" compute positions for a list of rows of events """
list.__init__(self, *args)
# compute times for the events, and give them row_ids
for i, row in enumerate(self):
t = 0
for ev in row:
ev.time = t
t += ev.duration
ev.row_id = i
# map times to position for displacements and event
t2pos_disp = defaultdict(int) # maps times to position of synchronized start of displacements
t2pos_ev = defaultdict(int) # maps times to position of synchronized start of events and gaps
# the real work is done in the following loop
t_prev = 0
for t, g in groupby(merge(*self), key=attrgetter("time")):
# different times should have a minimum distance corresponding to their difference
t2pos_ev[t] = t2pos_disp[t] = max(t2pos_ev[t], t2pos_ev[t_prev] + t - t_prev)
t_prev = t
for (duration, row_id), g_row in groupby(g, key=attrgetter("duration", "row_id")): # process all displacements first, then the events
pos_ev = t2pos_ev[t] if duration > 0 else t2pos_disp[t] # events and displacements start at different
for ev in g_row:
ev.pos = pos_ev
pos_ev += ev.duration + ev.displacement
t2pos_ev[t + ev.duration] = max(t2pos_ev[t + ev.duration], pos_ev)
# keep our results...
self.t2pos_ev = t2pos_ev
self.t2pos_disp = t2pos_disp
@staticmethod
def str_row(row):
""" draw row, uppercase letters for real events, lower case letters for
displacements, dots for gaps"""
ev_chars = cycle(ascii_uppercase)
out = []
l = 0
for ev in row:
if ev.pos > l:
out.append(" " * (ev.pos - l))
out.append(ev.draw(next(ev_chars) if ev.visible else "."))
l = ev.pos + len(out[-1])
return "".join(out)
def __str__(self):
max_t, max_p = max(self.t2pos_ev.items())
w = len(str(max_t))
header_temp = [" " * w] * (max_p + 1)
for t, p in self.t2pos_ev.items():
header_temp[p] = "%*d" % (w, t)
headers = ("".join(header) for header in zip(*header_temp))
rows = (self.str_row(row) for row in self)
return "\n".join(chain(headers, rows))
if __name__ == "__main__":
# original example
row_one = [Event(1), Event(0,1), Event(1), Gap(1), Event(1), Gap(1), Event(0,1), Event(1), Event(1)]
row_two = [Event(1), Event(1), Gap(1), Event(1, 1), Event(1), Event(1), Event(1)]
row_thr = [Event(1), Event(1), Event(1), Event(1), Event(1), Event(0,1), Event(0,1), Event(1), Event(1)]
timetable = Timetable([row_one, row_two, row_thr])
print(timetable)
print("-" * 80)
# short example, shows ending times are not synchronized
print(Timetable([[Event(2, 1)], [Event(1, 1), Event(1)], [Event(1, 2), Event(1)]]))
print("-" * 80)
# larger random example
def random_row(l):
import random
res = []
t = 0
while t < l:
x = random.random()
if x < 0.1: res.append(Event(0, random.randint(1, 3)))
elif x < 0.8: res.append(Event(min(random.randint(1, 5), l - t), random.randint(0, 1) * random.randint(0, 2)))
else: res.append(Gap(min(random.randint(1, 5), l - t)))
t += res[-1].duration
return res
print(Timetable([random_row(50) for _ in range(15)]))
关于python - 从消耗时间的项目和不消耗时间但仍需要绘制空间的项目生成基于时间线的线性表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439763/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="