Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65423
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'float': 0.07; 'calculating': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'slow.': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'csv': 0.16; 'directly?': 0.16; 'empty,': 0.16; 'len(line)': 0.16; 'message-id:@4ax.com': 0.16; 'numpy': 0.16; 'personally,': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'storing': 0.16; 'subject:Logging': 0.16; 'subject:PySerial': 0.16; 'true:': 0.16; 'code.': 0.18; 'written': 0.21; 'feb': 0.22; 'import': 0.22; 'creating': 0.23; 'filtering': 0.24; 'url:home': 0.24; 'mon,': 0.24; 'file.': 0.24; 'script': 0.25; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'scale': 0.29; "doesn't": 0.30; 'start,': 0.30; "i'm": 0.30; '(which': 0.31; 'lines': 0.31; 'file': 0.32; 'lists': 0.32; 'another': 0.32; 'quite': 0.32; 'raw': 0.33; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; 'connection': 0.35; 'convert': 0.35; 'but': 0.35; 'building': 0.35; 'there': 0.35; 'collecting': 0.36; 'subject:data': 0.36; 'done': 0.36; "didn't": 0.36; 'charset:us- ascii': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'filter': 0.38; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'read': 0.60; 'experts': 0.60; 'break': 0.61; 'length': 0.61; 'new': 0.61; 'first': 0.61; 'thomas': 0.65; 'here': 0.66; 'sample': 0.67; 'serial': 0.72; 'wipe': 0.84; 'simulation': 0.91; 'received:108': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: Logging data from Arduino using PySerial |
| Date | Tue, 04 Feb 2014 08:56:12 -0500 |
| Organization | IISS Elusive Unicorn |
| References | <e3b195f8-5a17-4536-9926-2b2ab193719c@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-108-79-220-239.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 6.00/32.1186 |
| X-No-Archive | YES |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6387.1391522176.18130.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391522176 news.xs4all.nl 2888 [2001:888:2000:d::a6]:53167 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65423 |
Show key headers only | View raw
On Mon, 3 Feb 2014 20:07:48 -0800 (PST), Thomas <t.tchorzewski@gmail.com>
declaimed the following:
>I've written a script to log data from my Arduino to a csv file. The script works well enough but it's very, very slow. I'm quite new to Python and I just wanted to put this out there to see if any Python experts could help optimise my code. Here it is:
>
> import serial
> import re
> import csv
> import numpy as np
> import matplotlib.pyplot as plt
>
> portPath = "/dev/ttyACM0"
> baud = 9600
> sample_time = 0.5
> sim_time = 30
>
>
> # Initializing Lists
> # Data Collection
> data_log = []
> line_data = []
>
> def map(x, in_min, in_max, out_min, out_max):
> return (((x - in_min) * (out_max - out_min))/(in_max - in_min)) + out_min
>
Doesn't the Arduino have a map() function internally? If you have
control over the Arduino couldn't you set it up to return the desired
mapping values directly?
> # Establishing Serial Connection
> connection = serial.Serial(portPath,baud)
>
> # Calculating the length of data to collect based on the
> # sample time and simulation time (set by user)
> max_length = sim_time/sample_time
>
> # Collecting the data from the serial port
> while True:
> data_log.append(connection.readline())
> if len(data_log) > max_length - 1:
> break
>
Here you are building up a list of raw lines...
> # Cleaning the data_log and storing it in data.csv
> with open('data.csv','wb') as csvfile:
> for line in data_log:
> line_data = re.findall('\d*\.\d*',line) # Find all digits
> line_data = filter(None,line_data) # Filter out empty strings
> line_data = [float(x) for x in line_data] # Convert Strings to float
>
> for i in range(1,len(line_data)):
> line_data[i]=map(line_data[i],0,1023,0,5)
>
> csvwrite = csv.writer(csvfile)
You are creating a new csv writer instance on each pass!
> csvwrite.writerow(line_data)
>
And then you loop over all the lines looking for particular values,
just to scale them into another range, to write to a CSV file.
Personally, I'd have opened the CSV file at the start, and done all
this filtering/transforming on each line as it was read from the Arduino.
csvfile = open("data.csv", "wb")
csvwriter = csv.writer(csvfile)
line = ""
while len(line) < max_length:
if len(line) == 0: #skip first line (which your range(1,...) does
line = connection.readline()
line = connection.readline()
# do all your filtering here
if line: #not empty, so filtering didn't wipe it out
csvwrite.writerow(line)
csvfile.close()
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Logging data from Arduino using PySerial Thomas <t.tchorzewski@gmail.com> - 2014-02-03 20:07 -0800
Re: Logging data from Arduino using PySerial Chris Angelico <rosuav@gmail.com> - 2014-02-04 15:47 +1100
Re: Logging data from Arduino using PySerial Thomas <t.tchorzewski@gmail.com> - 2014-02-03 20:57 -0800
Re: Logging data from Arduino using PySerial Chris Angelico <rosuav@gmail.com> - 2014-02-04 16:18 +1100
Re: Logging data from Arduino using PySerial Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-04 08:56 -0500
Re: Logging data from Arduino using PySerial MRAB <python@mrabarnett.plus.com> - 2014-02-04 14:05 +0000
csiph-web