Showing posts with label musings. Show all posts
Showing posts with label musings. Show all posts

Friday, 26 July 2013

A cooler shell prompt

In ~/.bashrc:



time: username: directory


much better!

Sunday, 20 January 2013

Patchwork quilt in Python

Here's a fun python function to make a plot of [x,y] coordinate data as a patchwork quilt with user-defined 'complexity'. It initially segments the data into 'numsegs' number of clusters by a k-means algorithm. It then takes each segment and creates 'numclass' sub-clusters based on the euclidean distance from the centroid of the cluster. Finally, it plots each subcluster a different colour and prints the result as a png file.

inputs:
'data' is a Nx2 numpy array of [x,y] points
'numsegs' and 'numclass' are integer scalars. The greater these numbers the greater the 'complexity' of the output and the longer the processing time



Some example outputs in increasing complexity:

numsegs=10, numclass=5

numsegs=15, numclass=15

numsegs=20, numclass=50



Sunday, 6 June 2010

Granular Therapy

Here's a relaxing movie I just made of spinning modelled sand grains:



I'm not going to divulge the details of the model of how to create the sand grains (a current research project of mine). I can tell you I used these rather excellent functions for MATLAB:

Volume interpolation Crust algorithm, described more here

A toolbox for Multi-Parametric Optimisation

I used 'recordmydesktop' to capture the grains in my MATLAB figure window. It created an OGV format video (which I renamed to 'beautiful_grains.ogv'). I then used the following code to decode the video into jpegs

 mkdir out_pics  
mplayer -ao null -ss 00:00:03 -endpos 50 beautiful_grains.ogv -vo jpeg:outdir=out_pics



I needed to crop the pictures to just the grains, then turn the cropped pictures back into a video. I used this:

 cd out_pics  
for image in *.jpg
do
# crop the image
convert $image -crop 600x600+350+135 grains%05d.tiff
done
# make movie from the cropped tiffs
ffmpeg -r 25 -i grains%05d.tiff -y -an spinning_grains_small.avi


et voila! Now sit back with a cuppa and relax to those spinning grains.

Saturday, 28 February 2009

Shannon's Box

Claude Shannon, father of information theory, apparently once invented a box whose sole purpose was to, when switched on, turn itself off again. Recently I've got into scripting with CHDK (Canon Hackers Development Kit) - a firmware enhancement for Canon cameras. It allows you to turn your cheap point-and-shoot camera into something better, for free. Long exposures, very rapid shots, motion detection - you name it. I can tell I'm at the start of a beautiful friendship ... look at this!

You can write and upload scripts to automate processes. The language is uBASIC - really intuitive and easy. My first script is pretty basic - it has been to tell the camera to, when switched on, automatically take 3 photos and then turn itself off! I therefore called it Shannon's Box:

 rem Author Daniel Buscombe  
@title Shannon's Box
@param a Shoot Count
@default a 3
shoot full
for n=2 to a
shoot full
if n=a then goto "shutdown"
next n
end
:shutdown
shut_down


More, I'm sure, on CHDK in the following months.

Saturday, 15 November 2008

in 5 minutes, for free (or 'balanced restored')

Originally posted in The Daily Daniel
--------------------------------------

People have said to me words along the lines of "why do you both with linux? it seems as though all it ever gives you is problems blah blah blah ..." - i don't know, I never listen to the rest. Yes, I do moan about linux a lot, and even in these pages ... well here's just 1 example of some routine thing I wanted to do where linux came to the rescue. I did it in 5 minutes

I had a folder full of pictures, and I wanted to make 2 copies of each - one a fraction of the size (for a thumbnail gallery), and one loads bigger - the images were smaller than the CCD used to capture them, so I knew I couldnt alias them. Right, well there's a linux command which does the job called 'convert'. Quick, really simple to use, e.g.:

>> convert my_image.jpg -resize 200% my_new_image.jpg

Does exactly what it says on the quick drying woodstain. But how to do the whole folder in one go? My old friend matlab comes to the rescue, and my personal favourite command 'system' - this bit of code will read in a folder of images, create 2 new folders, copy and resize each image, save it in the correct folder and append the file name with the operation done to it. All in the time it takes you to sneeze.

% find all the jpegs in the folder
>> direc=dir([pwd,filesep,'*.','jpeg']);

% create an empty cell to stick them in
>> filenames={};

% deal inputs into outputs
>> [filenames{1:length(direc),1}] = deal(direc.name);

% sort them and turn them into character arrays
>> filenames=sortrows(char(filenames{:}));

% make 2 directories - one called 'james_and_the_giant_peach' and one called 'thumblina'
>> mkdir([pwd,filesep,'james_and_the_giant_peach'])
>> mkdir([pwd,filesep,'thumblina'])

% simple loop
>> for i=1:size(filenames,1)

% giant images (500% size)
>> system(['convert ',deblank(filenames(i,:))...
,' -resize 500% ./james_and_the_giant_peach/',deblank(filenames(i,:)),'_giant.jpeg'])

% thumbnail images (20% size)
>> system(['convert ',deblank(filenames(i,:))...
,' -resize 20% ./thumblina/',deblank(filenames(i,:)),'_thumb.jpeg'])

>> end

Another example where matlab and linux combine beautifully. Matlab would have done it on its own (using imresize and imwrite commands), but it would have taken considerably longer.

Today is Boomtime. Good night

A tour through home 'pageranking' - try it yerself!

Originally posted in The Daily Daniel
_____________________________

Now I live in the foothills of silicon valley, I thought I'd write a piece on one of its most famous and useful and revolutionary spawns, Google, and specifically it's PageRank algorithm. This is the piece of code which Google Search engines use to find webpages that are most relevant to your enquiry, and rank them so the most relevant are near the top. Its a bloody marvel, as we all know (remember the bad old days using old lycos and yahoo searches etc - pah!)

Instead of writing stuff on it, which has been done elsewhere numerous times, I thought I would show you how to do it using Matlab

First, you need to get the code. Cleve Moler at Mathworks has written a library which contains a primitive attempt at the proprietary algorithm. You need to get it, unzip it and untar it. This can all be done within Matlab, like so:

>> url='http://www.mathworks.com/moler/ncm.tar.gz';
>> gunzip(url,'ncm')
>> untar('ncm/ncm.tar','ncm')

Next, cd to where you unzipped it to, which is probably -

>> cd([pwd,filesep,'ncm'])

Then run the surfer algorithm on a website of your choosing, this one for example, and tell it to search for n nodes (in this case, 200 - warning, it takes a while)

>> [U,G]=surfer('http://www.thedailydanielblog.blogspot.com',200);

U is a list of websites connected to yours, arranged in matrix G. My website surfer matrix looks a little like this:




I was interested in what these sites were so I wrote a bit of code to print them to a text file:

>> fid=fopen('dansweb.txt','wt');
>> for i=1:size(char(U),1), fprintf(fid,'%s\n',char(U(i,:))); end
>> fclose(fid)

dansweb.txt then contains a list of 200 websites

The pagerank algorithm was then applied to U and G to find

>> pagerank(U,G)

The results are unsurprising - it charts links i have made, blogspot pages associated with my labels, and widgets i have used. There is even a bar chart output:
which looks slightly better if you add the websites to it

page-rank in out url
190 0.0432 9 2 http://www.usgs.gov
191 0.0383 4 2 http://www.usgs.gov/ask
192 0.0383 4 2 http://search.usgs.gov
72 0.0137 21 0 http://www.blogger.com
79 0.0131 40 0 http://purl.org/syndication/thread/1.0
71 0.0129 20 0 http://www.blogger.com/profile/
184 0.0098 5 0 http://planet.ubuntu.com
5 0.0092 60 6 http://www.blogger.com/profile/02981038739002302942
189 0.0092 1 0 http://wiki.octave.org
193 0.0091 6 5 http://marine.usgs.gov
10 0.0087 61 0 http://ims.ucsc.edu
3 0.0086 59 117 http://thedailydanielblog.blogspot.com/feeds/posts/default
4 0.0085 58 118 http://www.blogger.com/feeds/1936258002310927863/posts/default
8 0.0082 57 1 http://www.gnu.org/software/octave
11 0.0082 57 0 http://twitter.com
12 0.0082 57 0 http://hypem.com
63 0.0082 57 7 http://www.ourblogtemplates.com
21 0.0081 56 54 http://thedailydanielblog.blogspot.com/search/label/photos
24 0.0081 56 50 http://thedailydanielblog.blogspot.com/search/label/santa%20cruz
23 0.0081 56 49 http://thedailydanielblog.blogspot.com/search/label/san%20francisco
26 0.0081 56 46 http://thedailydanielblog.blogspot.com/search/label/travel
22 0.0081 56 43 http://thedailydanielblog.blogspot.com/search/label/preparations
25 0.0081 56 42 http://thedailydanielblog.blogspot.com/search/label/santa%20news
14 0.0080 56 36 http://thedailydanielblog.blogspot.com/search/label/internet
15 0.0080 56 36 http://thedailydanielblog.blogspot.com/search/label/job
17 0.0080 56 36 http://thedailydanielblog.blogspot.com/search/label/map
18 0.0080 56 36 http://thedailydanielblog.blogspot.com/search/label/matlab
20 0.0080 56 35 http://thedailydanielblog.blogspot.com/search/label/musings
16 0.0080 56 34 http://thedailydanielblog.blogspot.com/search/label/linux
27 0.0080 56 34 http://thedailydanielblog.blogspot.com/search/label/work
19 0.0080 56 33 http://thedailydanielblog.blogspot.com/search/label/move
6 0.0078 21 0 http://www.blogger.com/openid-server.g
67 0.0069 20 0 http://www.blogger.com/openid-server.g\42 /\76\n
196 0.0057 3 4 http://walrus.wr.usgs.gov/infobank
197 0.0057 3 2 http://mrib.usgs.gov
183 0.0053 4 1 http://fridge.ubuntu.com
194 0.0052 3 0 http://vineyard.er.usgs.gov/query.html
66 0.0052 17 0 http://www.blogger.com/profile/02981038739002302942\42 /\76\n

So, I have a pagerank of 0 (more or less) which is the lowest possible. The highest is 10, so this isn't a great example, admittedly, but I'll let interested readers have a play.

I discovered that facebook has a 9 and plymouth.ac.uk has a 7. Now, before you comment, i know that there are already loads of web-based pagerank checkers out there, but this is more fun, and here you get a breakdown of what sites contribute to each other's scores, OK?

Said in a Neil Buchanon from Art Attack (ITV since the 1990s) - 'try it yerself'!!

>> clear, clc, exit