Tuesday, 13 October 2015

python pandas - data analysis library - quickstart / introduction: 1. read data (pandas IO) 2. database style merge (inner join indexing on date+time) 3. write data.

I discovered python pandas recently.
Using to read in ifconfig logs and add up total network traffic (VM network) across multiple hosts.
I needed to combine files from multiple hosts, sort values and combine by date and time.

I was sorting and parsing into python dicts.
I looked at perl PDL and other data manipulation libs.
But looks like python pandas wins because it combines VERY FLEXIBLE file IO with a lot of methods for selecting rows/columns and manipulating the data.

I made a simple test to make sure I knew how it all worked and described it here:
http://stackoverflow.com/questions/19222043/parse-two-files-and-merge-lines-if-time-stamp-matches/33100756#33100756

A flexible and general way of manipulating data is python pandas. Worth mentioning here as it really is the right tool for the job. Allows spreadsheet or database style merges/joins/concats on selected index rows or columns.
Two example files to illustrate how it works
$ cat File1
date0,time0,data01,data02,data03
date1,time1,data11,data12,data13
date2,time2,data21,data22,data23
date3,time3,data31,data32,data33
date4,time4,data41,data42,data43
date5,time5,data51,data52,data53
$ cat File2
date1,time1,data14
date4,time4,data44
date2,time2,data24
Run python . . .
  • Use pandas read_csv to slurp in files in pandas table structure. (read_csv is very clever and can read in many formats not just csv)
  • Use pandas merge to do inner(intersection of indices) join, using date+time as indices (index list=[0,1]).
  • Use pandas to_csv to write output.
THE IMPORTANT BIT:
$ python
>>> from pandas import merge, read_csv
>>> f1=read_csv("File1",header=None)
>>> f2=read_csv("File2",header=None)
>>> merged = merge(f1, f2, how='inner', left_on=[0,1], right_on=[0,1])
>>> merged.to_csv("Out", na_rep=0, index=False, header=False)
>>> [Ctrl-D]
Job done!
$ cat Out
date1,time1,data11,data12,data13,data14
date2,time2,data21,data22,data23,data24
date4,time4,data41,data42,data43,data44
Easy as 1, 2, 3.
1. read data (pandas IO) 
2. database style merge (inner join indexing on date+time) 
3. write data. 
VERY clean, no messing. I really do love bash/grep/sed/awk also perl and python manipulating data in structures BUT right tool for the job makes the job much easier and gives much more potential for use of the data.
Breakdown:
1. read_csv A bog-standard(plain, unadorned) 'read_csv("File1")' treats first line as header names. So we use 'header=None'.
>>> f1=read_csv("File1")
>>> f1
date0  time0  data01  data02  data03
0  date1  time1  data11  data12  data13
1  date2  time2  data21  data22  data23
2  date3  time3  data31  data32  data33
3  date4  time4  data41  data42  data43
4  date5  time5  data51  data52  data53
>>> f1=read_csv("File1",header=None)
>>> f1
0      1       2       3       4
0  date0  time0  data01  data02  data03
1  date1  time1  data11  data12  data13
2  date2  time2  data21  data22  data23
3  date3  time3  data31  data32  data33
4  date4  time4  data41  data42  data43
5  date5  time5  data51  data52  data53
>>> f2=read_csv("File2",header=None)
pandas DataFrame 'describe()' gives a useful summary especially for big tables. For numeric data you also get total, max, min, mean, e.t.c.
>>> f1.describe()
0      1       2       3       4
count       6      6       6       6       6
unique      6      6       6       6       6
top     date4  time3  data01  data12  data13
freq        1      1       1       1       1
2. merge
how specifies left/right/inner/outer merge style sql join terminology. how='left' takes first file indices (date+time) as output and merge in the second files data. how='right' takes the second files indices as output and merge in the first files data. how='inner' does an intersection between each files indices (date+time) so only data for which you have entries in both files are taken. how='outer' does an union between each files indices (date+time) so all data is written, data for which you do not have entries in both files is filled with 'NaN' values.
on/left_on/right_on index select We could also use 'on=[0,1]' as our input files have the same columns of indices (and they are named the same 0 and 1 as we read in the file with 'header=None').
>>> merged = merge(f1, f2, how='inner', left_on=[0,1], right_on=[0,1])
>>> merged
0      1     2_x       3       4     2_y
0  date1  time1  data11  data12  data13  data14
1  date2  time2  data21  data22  data23  data24
2  date4  time4  data41  data42  data43  data44
>>> mergedOut = merge(f1, f2, how='outer', left_on=[0,1], right_on=[0,1])
>>> mergedOut
0      1     2_x       3       4     2_y
0  date0  time0  data01  data02  data03     NaN
1  date1  time1  data11  data12  data13  data14
2  date2  time2  data21  data22  data23  data24
3  date3  time3  data31  data32  data33     NaN
4  date4  time4  data41  data42  data43  data44
5  date5  time5  data51  data52  data53     NaN
3. to_csv we write out without and index or a header using 'index=False' and 'header=False'. See the output with index and header written to the "Out2" file:
>>> merged.to_csv("Out2")
>>> merged.to_csv("Out", na_rep=0, index=False, header=False)

$ cat Out2
,0,1,2_x,3,4,2_y
0,date1,time1,data11,data12,data13,data14
1,date2,time2,data21,data22,data23,data24
2,date4,time4,data41,data42,data43,data44
$ cat Out
date1,time1,data11,data12,data13,data14
date2,time2,data21,data22,data23,data24
date4,time4,data41,data42,data43,data44
Documentation to get started with:
To get pandas and install: http://pandas.pydata.org/getpandas.html
# download, unpack and:
sudo python setup.py install

Thursday, 10 September 2015

Cycling, the rules: overtaking and slipstreaming and friendly communication

To overtake or slipstream? That is the question. Whether tis a breach of ettiquette to do either when cycling at similar pace.

Also . . on communication. Body language. Vehicle language. Smile, motion and vocalise.

Perfectly justified to slipstream I think especially if overtaker slows down a bit. Also can be an opportunity to take a rest. :-) If pace too slow just speed up and overtake. Agree one should be able to put in a good +5-10% more speed before overtaking. Nothing wrong with a friendly word of communication in warning or in case of pausing together at junction. My regular commute home has a steady rise from Dublin city centre and I find there is quite a variety in pace with a good few slower cyclists, some of similar pace and the odd few extremely fast people.

When catching up with someone of similar speed I find you cannot do anything but latch on. It is that or go for overtake. If latching on then do so loosely watching well ahead of other rider, riding defensively. I would ride in line a little outside other rider protecting from overtaking traffic but move back in if front rider has to negotiate hazard ahead or someone faster comes up behind. I would be reluctant to overtake and cause inconvenience unless I could be confident of getting well away and relieving the overtakee of the sight of my rear end!

I'm quite Zen I think (and modest about it too ;)) if being overtaken or with cycling and driving generally. Car, bike or motorbike. But got to admit there's something nice and warm smugly satisfying about leaving someone (safely!) for dust if they have impinged on ettiquette. I ride a MTB no pedal clips so I don't look the fastest if not moving. Half the time in mad shorts instead of cycling gear. So beware ;-). MTB tyres so I can bash over potholes cracks cobbles manholes up down bike lanes of course. I'm much faster on my wife's slick tyres - on the straight! But I have found you have to reduce speed more to take corners! These discoveries keep us somewhat humble. But not too humble. I hope.

One thing I find with communication is maybe we could do a bit more. A lot of cycling catching up, cycling behind, passing is done without a word. With defensive driving we read others intentions by body language of other vehicle and car. But with encountering other cyclists - especially if they look like they are less confident - I think it helps if you can vocalise intentions sometimes. e.g. "*ahum* I'm - er - just going to come up on your right there." when you have space to move out but want to not scare someone. Also at junctions I would give way to pedestrians. ESPECIALLY if they have the right of way! :-) I find signalling this with showing palm forward arm out bent at elbow (like garda stop traffic signal - not the closer to body I intend going straight signal) leaves pedestrians know you are stopping also leaves bikes and traffic behind know you are giving way. Pedestrians less hesitant and happier and clear junction faster and safer leaving way clear. Yay. Win win. Making eye contact is great for defensive driving and riding and played like the kids sweet/sour game is good fun. Most people pedestrians cyclists drivers all turning out to be sweet :O)

http://lovelybike.blogspot.ie/2015/09/in-passing.html?spref=tw&m=1

Monday, 20 July 2015

learning to drive - teaching someone to drive - materials on internet and first lessons

Daire is 17 and has passed his driving theory test. And has a provisional licence. And is insured on a fiesta. So we are all set to start properly learning to drive. He rides a bike around quite a bit already. So has some road experience from that. A small/occasional amount of experience driving a car/tractor/lawn mower in field has given a feeling for control of driving something with an engine.

So now what do we do? :-) Ready to start driving! 

As we are new to teaching driving we looked it up a bit. There is some good info from UK and USA on teaching young people to drive. Good to start with that. This gives a few things to think about before starting. There is a useful sheet from UK driving which gives a full sheet of driving skills and a techniques. You can plan roughly what skills you will work on - so both instructor and strudent could look them up before-hand, so you can go out on the road with a plan (know where you are going and what to do). A checklist tracking progress on each skill makes sure you cover all the skills needed over time. 

A straight-forward introduction:

A very good quick-reference list of skills (reminder to teachers what to teach and work on).
And a skills/prograss chart:

Here is an electronic copy of that very useful skill progress checklist:
Learning to Drive - Driver's Record. Google sheets.
Learning2Drive_DriversRecord.pdf
Learning2Drive_DriversRecord.ods
Because paper copies are easy to misplace :-7

For the teachers: You have to be a good driver and ALSO a good teacher. We are beginner driving teachers! So a bit of thought and patience required between teachers and student! :-) 

I found learning to drive a motorbike ~ about 10 years ago ~ very useful for my driving. I had to pass the driving theory test (newly introduced to Ireland) and I have worked on learning advanced driving techniques and putting them into practice (on bikes and in car). Motorbiking and cycling give very good attention and concentration and awareness on the road. Motorbikes use defensive driving which is also very good to employ when driving any vehicle. Also a long time ago driving tractors and cars with trailers on the farm gave an awareness of how trucks and trailers and buses manouver - which is useful when close to them in traffic. 

First couple of lessons: clutch bite point, road positioning

First up is find a safe location to start the engine and get moving. There is a big and quiet car park not too far away from where we live which was good for us. 2 sessions and we have found getting moving forward (finding bite point of clutch and balancing. Fionnuala did the first and I did the second. Daire did both! 

To explain clutch bite point we had to think about it! Jump in the driver seat. Move off. Think about what we were doing with our legs. The important thing (that is automatic to us) with clutch leg is we hold our clutch leg quite stiffly, not allowing it to move up or down (except as we want it to) and as car starts moving we do not allow leg to wobble but maintain control over it. Gentle or no acceleration needed if moving off downhill. A little bit of acceleration needed on flat of uphill.

During these first lessons there is ALOT to learn. We are also learning instrument controls, observation (of parked cars and the occasional other learner car in car park), road positioning - while moving forward in between practicing starting - positioning in straight and when turning. And the skills have to be applied simultaneously! They become automatic to experienced drivers. So it is important to take things at a very steady pace. 

There were some slight inclines in the quiet car-park so once Daire was getting the hang of starting on the flat we started stopping on the incline and practiced starting when a bit more acceleration was required.

And we added in reversing straight and reversing while turning. 

Next lessons, Introduction to quiet roads - industrial estate - junctions - moving off and turning

A driver has to be very skilled at moving off (straight or at left or right turn into traffic) to drive on public roads. Every stop at a junction or in traffic involves a move-off again. So next we are working on moving off (still working on clutch control). In industrial state, there are some T junctions to handle, and we pull into a car park. Stop in parking spot. Reverse and pull out onto road again. There are a mix of inclines on road and in car parks. And of course observation and signalling is being practiced continually.

At the end today we went out of industrial estate (straight through a set of traffic lights). Then straight - left at junction - right at set of traffic lights (three lanes in road). Lane positioning needs work. At the start today we started from home in housing estate (tricky!) and mixed up driving on roads.

He's going at quite a fast pace once he gets moving. A bit too fast in turns and on corners. A common problem apparently for learners. I think because it is difficult to drive quite slowly with control. So will have to work control moving car nice and slowly. 

Next next . . . 
 + Work on good vehicle control moving off and road positioning when manouvering. 
 + Practice of proper observation and signalling is happening together with driving.
 - Work on physical accuracy with moving the car forwards and backwards - straight and in turns is needed. 
    So narrow roads/lanes lined with - cones - or something would be ideal.
 - More reversing and introduce 3-point-turns
 - . . . 

That will all be needed for the official driving lessons and tests as well as for good driving.


I find it good to read about the rules of road and theory of the techniques you are working on as I am learning. There is some good material on youtube and internet (and a lot of not so good!). The advanced driving techniques from police driving instructors are good. It keeps skills fresh. 

Some more links:


A parent & teen learning to drive guide from California . . .
https://www.dmv.ca.gov/portal/wcm/connect/b615c5c3-5a73-4e7b-b571-9850032730be/dl603.pdf?MOD=AJPERES

This has some more good info on Moving Off and Stopping techniques:
http://www.drivingtesttips.biz/moving-off-stopping.html

There are some good driving learning videos on youtube . . .
e.g. reversing (slowly but very controlled) https://www.youtube.com/watch?v=IKd0GXZ39U8
But there are also some bad ones!
Ones from driving schools are probably better.

https://en.wikipedia.org/wiki/Motorcycle_training

Good book: Motorcycle Roadcraft: The Police Rider's Handbook to Better Motorcycling

Friday, 19 June 2015

stackexchange.com and Does rubbing a nettle sting with a dock leaf actually help? How?

stackexchange.com and stackoverflow.com are great sites for finding answers to technical questions on software and computers. Their aim is to have the correct (and up to date) answers for any question. Community voted and moderated. It really works well.

I most frequently find a good answer for a problem. Vote it up and that's it. Sometimes it's a bit more complicated so I would do some commenting or other answer or question editing. On rare occasion I have found no satisfactory answer and I have actually added an answer of my own.

Until now I never found a question that was not asked!

stackexchange.com have now been adding sites on a myriad of different topics like english, mathematics, bicycles, physics, parenting and health to name just a few. See a neat word/icon - cloud - map - thingy  here: http://stackexchange.com/sites

So my first question:

http://health.stackexchange.com/questions/1290/does-rubbing-a-nettle-sting-with-a-dock-leaf-actually-help-how
From a young age we were taught that the treatment for a nettle sting was to rub it with a dock leaf. Conveniently, docks usually grow close to where there are nettles.
  • Does this actually do something to help?
  • Is the effect entirely/mostly placebotic?
  • How does it work? (and what is the best technique to use?)
  • Would washing/rubbing with water be as/more effective?
As an adult if I get stung I have used the same treatment. It does seem to gradually calm the sting but it is difficult to say if the sting would be better without the treatment.
Psychological action? A bit of magic and a bit of placebo.
The treatment especially seems to help kids. Looking around for dock leaves is a great distraction. Kids are really happy to be able to help and do something about the problem. I have also heard that getting stung with a nettle is "lucky" (think magic fairy type of lucky here - woo!) and also that it gives a boost to the immune system (now that sounds much more scientific!). These two things are another good distraction and comfort to kids especially.
Physical action ?
Does rubbing the sting with juice of dock leaf simple rub away the sting needles? And does the juice dilute the chemicals injected in the sting?
In the summer when the nettles are out and it is common to get stung I have found several times that no water is convenient (even in Ireland) so the best source of something cooling is a crushed dock leaf.
Chemical action ?
I believed nettles had an acid in the sting and that dock leaves had a base. And a simple acid+base=>salt+water neturalising reaction took place to calm the sting.
However, reading a bit more about this shows up different and sometimes contradictory information. It is hard to determine what are actual facts. What chemicals are involved depend on what article you are reading!
I think the facts on nettle sting are: Stinging nettles have sharp needles of silica(glass). There is a mix of chemicals which are injected by the needles. From wikipedia : several chemicals: acetylcholine, histamine, 5-HT (serotonin), moroidin, leukotrienes, and possibly formic acid.
Doc leaves are related to sorrel (and related to rhubarb). The leaves contain oxalic acid and tannins. It is also said they contains an antihistamine.

Some more links / references / info . . . 

http://www.dailymail.co.uk/sciencetech/article-3112201/Why-stinging-nettles-hurt-Chemist-shows-tiny-hairs-inject-venom-produce-pain-dock-leaves-WON-T-help.html  The Chemistry of stinging nettles.

http://herbs-treatandtaste.blogspot.ie/2011/11/dock-leaf-antidote-for-nettle-stings.html

http://gurumagazine.org/askaguru/why-do-dock-leaves-cure-nettle-stings/

http://www.thenakedscientists.com/forum/index.php?topic=20709.0
".I get severe raised whelps that hurt and last for two to three days...... unless I treat it pretty straight way with milk weed... I am not sure if that is the same as your Dock weed plant.. but I find it really takes the sting out.. bu it does not work for me if I delay treatment.  It needs to be done straight way.. with crushed milk weed juice...."
" The antihistamine from the dock leaf together with the natural healing properties of saliva will ease the stinging sensation."

http://www.wikihow.com/Treat-a-Sting-from-a-Stinging-Nettle

"An astringent substance is a chemical compound that tends to shrink or constrict body tissues. The word "astringent" derives from Latin adstringere, meaning "to bind fast". Two common examples arecalamine lotion and witch hazel." 
"Astringency is also the dry, puckering mouthfeel caused by tannins found in many fruits such as blackthorn (sloe berries),Aronia chokeberry, chokecherry, bird cherry, quince and persimmon fruits, and banana skins. The tannins (which are types of polyphenols) bind the salivary proteins, causing them to precipitate or aggregate[2] and lead to a rough "sandpapery" or dry sensation in the mouth." 


Dock leaves ~= sorrel and stinging nettles as remedies:







Friday, 8 May 2015

Ideas for support for 30k speed limit. Slogan idea: "70% Living - 30k Traffic"

"70% Living - 30k Traffic"   
 - I like this one. Implies that:
   - if you go at 20k Living gets up to 80%  :-) 
   - if you go at 40k Living goes down up to 60%   :-(

"70% Living - 30k Traffic"   
Traffic, Please Allow some Space for Living.
Be Gentle in Residential Areas.

we can draw a graph showing dominance of traffic . . . sorry! very scribbley!

Displaying DSC_0304.JPG



"The Dublin Cycling Campaign, together with Cyclist.ie, strongly support the introduction of widespread 30Kp/h zones in the city, particularly in residential streets and in the vicinity of schools. In fact we are calling for 30Kp/h to be the default speed limit in urban areas, with higher speed only allowed where they are shown to be safe."

I started with something like "Thirty Together".
But the emphasis on the speed is wrong.
The emphasis should be on Bigger Traffic taking care of Less Powerful People.

"100% Living Area - 30k Traffic Zone"

"70% Living Area - 30k Traffic Zone"

"30k for a better way" - mbleh :-P but fits in with changing priority from traffic to living

"Living Area - Traffic take care at 30k"

"Living Area - Traffic Welcome up to 30k"

Considerate traffic will already be doing 30k or less where there are vulnerable road users around.

"Take a moment to enjoy this living area at a max of 30k . . . " meh

"Welcome to this Living Area - Traffic Please be Gentle with our People"

Thirty Together - Priority for Life, Living People - Welcome to this Living Area
Traffic Tolerated at max 30kph in this Shared Living Area
Welcome to our Shared Living Area.
Priority for Living - Playing - Walking.

Emphasising 
 - sharing of space and that priority should be for living - playing - walking not for transport.
Pointing out 
 - that traffic (of all kinds bike, car, bus, truck) is bigger, more powerful than people
 - that traffic is a guest in the area
 - that traffic should treat those less powerful gently

The streets need to look like a pedestrianised district - underfoot, layout and signage.
Emphasising that motorised traffic does not have ownership of the road in the area.

With Inspiration from the Dutch woonerf - "shared street" and "car is the guest" ideas.


Inline image 1

living, residential

area, farmyard

http://m.interglot.com/nl/en/toegestaan
allowable, permitted, accepted, tolerated, admissible, permissible, tolerable, afforded