473,749 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Building Time Based Bins

MCD
Hello, I'm new to python and this group and am trying to build some
bins and was wondering if any of you could kindly help me out. I'm a
bit lost on how to begin.

I have some text files that have a time filed along with 2 other fields
formatted like this >>

1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94

This goes on throughout a 12hr. period. I'd like to be able to place
the low and high values of the additional fields in a single line
divided into 5min intervals. So it would look something like this >>

1235 22 88
1240 31 94

I hope that makes sense. Should I be using a module like numarray for
this, or is it possible to just use the native functions? Any ideas
would help me very much.

Thank you - Marcus

Jul 18 '05 #1
12 1512
MCD wrote:
Hello, I'm new to python and this group and am trying to build some
bins and was wondering if any of you could kindly help me out. I'm a
bit lost on how to begin.

I have some text files that have a time filed along with 2 other fields
formatted like this >>

1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94

This goes on throughout a 12hr. period. I'd like to be able to place
the low and high values of the additional fields in a single line
divided into 5min intervals. So it would look something like this >>

1235 22 88
1240 31 94

I hope that makes sense. Should I be using a module like numarray for
this, or is it possible to just use the native functions? Any ideas
would help me very much.

Thank you - Marcus

This sort of thing would do it:

from itertools import groupby

def splitter(iterab le):
"""Takes a line-based iterator, yields a list of values per line
edit this for more sophisticated line-based parsing if required"""
for line in iterable:
yield [int(item) for item in line.split()]

def groupkey(data):
"""Groups times by 5 min resolution. Note this version doesn't work
exactly like the example - so fix if necessary"""
time = data[0]
return time / 100 * 100 + (time % 100) / 5 * 5

def grouper(iterabl e):
"""Groups and summarizes the lines"""
for time, data in groupby(iterabl e, groupkey):
data_x = zip(*data) #transform the data from cols to rows
print time, min(data_x[1]), max(data_x[2])

# Exercise it:

source = """1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94
"""
grouper(splitte r(source.splitl ines())) 1230 23 88
1235 22 94


Note this groups by the time at the end of each 5 mins, rather than the
beginning as in your example. If this needs changing, fix groupkey

HTH

Michael
Jul 18 '05 #2
On 19 Mar 2005 19:01:05 -0800, "MCD" <mc*******@wall a.com> wrote:
Hello, I'm new to python and this group and am trying to build some
bins and was wondering if any of you could kindly help me out. I'm a
bit lost on how to begin.


Are you (extremely) new to computer programming? Is this school
homework? The reason for asking is that the exercise requires no data
structure more complicated than a one-dimensional array of integers
(if one doubts that the times will always be in ascending order), and
*NO* data structures if one is trusting. It can be done easily without
any extra modules or libraries in just about any computer language
ever invented. So, it's not really a Python question. Perhaps you
should be looking at some basic computer programming learning. Python
*is* a really great language for that -- check out the Python website.

Anyway here's one way of doing it -- only the input and output
arrangements are Python-specific. And you don't need iter*.*.* (yet)
:-)

HTH,
John
=============== ============
C:\junk>type mcd.py
# Look, Ma, no imports!
lines = """\
1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94
"""
DUMMY = 9999
bintm = DUMMY
for line in lines.split('\n '): # in practice, open('input_fil e', 'r'):
if not line: continue
ilist = [int(fld) for fld in line.strip().sp lit()]
print "ilist:", ilist
klock, lo, hi = ilist
newbintm = ((klock + 4) // 5 * 5) % 2400
print "bintm = %d, klock = %d, newbintm = %d" % (bintm, klock,
newbintm)
if newbintm != bintm:
if bintm != DUMMY:
print "==>> %04d %02d %02d" % (bintm, binlo, binhi)
bintm, binlo, binhi = newbintm, lo, hi
else:
binlo = min(binlo, lo)
binhi = max(binhi, hi)
print "end of file ..."
if bintm != DUMMY:
print "==>> %4d %2d %2d" % (bintm, binlo, binhi)

C:\junk>python mcd.py
ilist: [1231, 23, 56]
bintm = 9999, klock = 1231, newbintm = 1235
ilist: [1232, 25, 79]
bintm = 1235, klock = 1232, newbintm = 1235
ilist: [1234, 26, 88]
bintm = 1235, klock = 1234, newbintm = 1235
ilist: [1235, 22, 34]
bintm = 1235, klock = 1235, newbintm = 1235
ilist: [1237, 31, 85]
bintm = 1235, klock = 1237, newbintm = 1240
==>> 1235 22 88
ilist: [1239, 35, 94]
bintm = 1240, klock = 1239, newbintm = 1240
end of file ...
==>> 1240 31 94

C:\junk>
=============== =============== ==

Jul 18 '05 #3
MCD
John Machin wrote:
Are you (extremely) new to computer programming? Is this school
homework?


Lol, yes, I am relatively new to programming... and very new to python.
I have experience working with loops, if thens, and boolean operations,
but I haven't worked with lists or array's as of yet... so this is my
first forray. This isn't homework, I'm long out of school. I've been
wanting to extend my programming abilities and I chose python as the
means to acheiving that goal... so far I really like it :-)

Thank you both for the code. I ended up working with John's because
it's a bit easier for me to get through. I very much appreciate the
code... it taught me quite a few things about how python converts
string's to integers and vice versa. I didn't expect to get thorugh it,
but after looking at it a bit, I did, and was able to modify it so that
I could work with my own files. Yeah!

The only question I have is in regards to being able to sum a field in
a bin. Using sum(hi) returns only the last value... I'm uncertain how
to cumulatively add up the values as the script runs through each line.
Any pointers?

Thank you again for all your help.
Marcus

Jul 18 '05 #4
MCD
Never mind about the summing... I learned that you can do this:

sumhi = 0
sumhi += hi

Cool!

Thanks again.

Jul 18 '05 #5
MCD wrote:
This goes on throughout a 12hr. period. I'd like to be able to place
the low and high values of the additional fields in a single line
divided into 5min intervals. So it would look something like this >>

1235 22 88
1240 31 94


what about a sane list comprehension madness ? <g>

lines = """\
1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94
"""

input = lines.split('\n ') # this is your input

div = lambda x: (x-1)/5

l = dict([
(div(x), []) for x,y,z in [
tuple([int(x) for x in x.split()]) for x in input if x
]
])

[
l[x[0]].append(x[1]) for x in
[
[div(x), (x,y,z)] for x,y,z in
[
tuple([int(x) for x in x.split()]) for x in input if x
]
]
]

print [
[max([x[0] for x in l[j]]),
min([x[1] for x in l[j]]),
max([x[2] for x in l[j]])
] for j in dict([
(div(x), []) for x,y,z in [
tuple([int(x) for x in x.split()]) for x in input
if x
]
]).keys()
]
i think it's a bit memory hungry, though

cya,
--
Alessandro "oggei" Ogier <al************ **@unimib.it>
gpg --keyserver pgp.mit.edu --recv-keys EEBB4D0D
Jul 18 '05 #6
MCD
Thanks Alessandro... I'll have to try that as well.

I have a modified working version of John's code (thanks John!). I'm
able to output the bins by 5min intervals, sum one of the fields, and
get the high and low of each field. So far I'm really happy with how it
works. Thank you to everybody.

The only thing that I'd like to do, which I've been racking my brain on
how to do in python... is how to keep track of the bins, so that I can
refer back to them. For instance, if I wanted to get "binlo" from two
bins back... in the scripting language I was working with (pascal
based) you could create a counting series:

for binlo = binlo - 1 do
begin

2binlosBack = (binlo - 2)

# if it was 12:00, I'd be looking back to 11:50

I would really appreciat if anyone could explain to me how this could
be accomplished using python grammar... or perhaps some other method
"look back" which I'm unable to conceive of.

Many thanks,
Marcus

Jul 18 '05 #7
MCD wrote:
Thanks Alessandro... I'll have to try that as well.

I have a modified working version of John's code (thanks John!). I'm
able to output the bins by 5min intervals, sum one of the fields, and
get the high and low of each field. So far I'm really happy with how it
works. Thank you to everybody.

The only thing that I'd like to do, which I've been racking my brain on
how to do in python... is how to keep track of the bins, so that I can
refer back to them. For instance, if I wanted to get "binlo" from two
bins back... in the scripting language I was working with (pascal
based) you could create a counting series:

for binlo = binlo - 1 do
begin

2binlosBack = (binlo - 2)

# if it was 12:00, I'd be looking back to 11:50

I would really appreciat if anyone could explain to me how this could
be accomplished using python grammar... or perhaps some other method
"look back" which I'm unable to conceive of.

Many thanks,
Marcus

Just append the results to a list as you go:
bins = []

for bin in ... # whichever method you use to get each new bin
bins.append(bin )

Then refer to previous bins using negative index (starting at -1 for the most
recent):
e.g., 2binlosBack = bins[-3]

Michael
Jul 18 '05 #8
MCD
Hi Michael, thanks for responding. I actually don't use a method to get
each bin... the bin outputs are nested in the loop. Here's my code:

data_file = open('G:\file.t xt')
DUMMY = 9999
bintm = DUMMY
for line in data_file:
fields = line.strip().sp lit()
if not line: continue
ilist = [int(time), int(a)]
# print "ilist:", ilist
klock, a = ilist
newbintm = ((klock + 4) // 5 * 5 ) % 2400
print "bintm = %d, newbintm = %d, a = %d" % (bintm, newbintm, a)
# the above is the raw data and now the bin loop
if bintm == 9999:
bintm = newbintm
binlo = a
elif bintm == newbintm:
binlo = min(binl, t)
else:
print " ==>> %04d %2d" % (bintm, binl) ## this is the bin
bintm = newbintm
binl = a

#-------------------

the input file is in my first post in this thread, the output looks
like:

bintm = 9999, newbintm = 1235, a = 23
bintm = 1235, newbintm = 1235, a = 25
bintm = 1235, newbintm = 1235, a = 26
bintm = 1235, newbintm = 1240, a = 22
==>> 1235 23
bintm = 1240, newbintm = 1240, a = 31
bintm = 1240, newbintm = 1240, a = 35

#---------------------

I'm not sure where I could create the new list without it getting
overwritten in the bin loop. Confused as to how to add the append
method in a for loop without a defined method for the current bin.
Anyway, I'll keep at it, but I'm not sure how to execute it. Thank you
very much for your suggestion.

Marcus

Jul 18 '05 #9
MCD wrote:
Hi Michael, thanks for responding. I actually don't use a method to get
each bin...
That's because you picked the wrong suggestion ;-) No, seriously, you can do it
easily with this approach:
the bin outputs are nested in the loop. Here's my code: data_file = open('G:\file.t xt')
DUMMY = 9999
bintm = DUMMY bins = [] for line in data_file:
fields = line.strip().sp lit()
if not line: continue
ilist = [int(time), int(a)] (BTW, there must be more to your code than you have shared for the above line to
execute without raising an exception - where are 'time' and 'a' initially bound?
BTW2, 'time' is the name of a stdlib module, so it's bad practice to use it as
an identifier) # print "ilist:", ilist
klock, a = ilist
newbintm = ((klock + 4) // 5 * 5 ) % 2400
print "bintm = %d, newbintm = %d, a = %d" % (bintm, newbintm, a)
# the above is the raw data and now the bin loop
if bintm == 9999:
bintm = newbintm
binlo = a
elif bintm == newbintm:
binlo = min(binl, t)
else:
print " ==>> %04d %2d" % (bintm, binl) ## this is the bin This is where you've declared that you have a bin, so add it to the bins cache:
bins.append((bi ntm, binl)) bintm = newbintm
binl = a

Michael

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
6700
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am really puzzled why it doesn't work public PlanarImage thresholding (PlanarImage source) { // set up the histogram int bins = { 256 }; double low = { 0.0D };
8
1380
by: querypk | last post by:
X-No-Archive: yes Can some one help me improve this block of code...this jus converts the list of data into tokens based on the range it falls into...but it takes a long time.Can someone tell me what can i change to improve it... def Tkz(tk,data): no_of_bins = 10 tkns = dmax = max(data)+1
3
4864
by: Mark Wheadon | last post by:
Hello, We have a 'standard letters' type app that can produce the letter as an XSL-FO document. We have used FOP to produce PDFs and are quite impressed. We also need to be able to print the letter and also choose paper from different printer bins. This is mostly so that headed paper can be used for the first page and continuation for the rest. Does anyone know if it is possible to define this with XSL-FO and if FOP, RenderX or...
38
2558
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my lead(I have just started working) he said we should not use dynamic allocation in real time systems, the code will not run in predictable time period.Can anybody tell what does he mean?Why the execution time becomes unpredictable? Thanks
17
2067
by: Nick | last post by:
I am doing some research into building web applications using Object Oriented techniques. I have found the excellent patterns section on the MSDN site, but other than that I cannot find any good, concrete examples. I know Microsoft are really pushing OO with the .NET Framework and C#, but for web stuff I am finding good examples sparse. Can anyone offer some links to articles, whitepapers, sites etc that maybe give a good overview of the...
12
1495
by: scottrm | last post by:
Is there a way to generate a list of say textbox controls dynamically at run time, based on say a value coming out of a database which could vary each time the code is run. In traditional asp you could just have run a loop to do something like that. I am aware in you can use something like Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you still need to declare each individual textbox control as far as I can...
0
1254
by: Kevin | last post by:
I'm trying to convert my VB6 program to VB2005. In my VB6 program I'm able to retrieve the paper bins for the selected printer and set the bin when printing. I'm using the DeviceCapabilities API and would like an example of it in NET. I've spent hours searching Google and only find VB6 examples.
1
2330
by: Deadvacahead | last post by:
I am rather new to programming and am coming across a problem with my current program. The program shows bins full of parts. It shows the name of the bin and then how many parts are in the bin. The program then asks for the user to update the data in the bins. The problem I cannot figure out is how to update the data in the array of bins. For instance, the program runs, I choose to add 5 parts to the Valve bin, how do I make th valve bin show...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.