annotate mds_plot.py @ 0:fe542273784f draft default tip

Uploaded
author bgruening
date Thu, 15 Aug 2013 03:39:14 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fe542273784f Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
fe542273784f Uploaded
bgruening
parents:
diff changeset
2
fe542273784f Uploaded
bgruening
parents:
diff changeset
3 import argparse
fe542273784f Uploaded
bgruening
parents:
diff changeset
4 import os
fe542273784f Uploaded
bgruening
parents:
diff changeset
5 import sklearn.manifold
fe542273784f Uploaded
bgruening
parents:
diff changeset
6 import numpy
fe542273784f Uploaded
bgruening
parents:
diff changeset
7 import math
fe542273784f Uploaded
bgruening
parents:
diff changeset
8 import pylab
fe542273784f Uploaded
bgruening
parents:
diff changeset
9
fe542273784f Uploaded
bgruening
parents:
diff changeset
10 if __name__ == "__main__":
fe542273784f Uploaded
bgruening
parents:
diff changeset
11 parser = argparse.ArgumentParser(
fe542273784f Uploaded
bgruening
parents:
diff changeset
12 description="""2D multidimenisnal scaling of NxN matrices with scatter plot"""
fe542273784f Uploaded
bgruening
parents:
diff changeset
13 )
fe542273784f Uploaded
bgruening
parents:
diff changeset
14
fe542273784f Uploaded
bgruening
parents:
diff changeset
15 parser.add_argument("-i", "--input", dest="sm",
fe542273784f Uploaded
bgruening
parents:
diff changeset
16 required=True,
fe542273784f Uploaded
bgruening
parents:
diff changeset
17 help="Path to the input file.")
fe542273784f Uploaded
bgruening
parents:
diff changeset
18 parser.add_argument("--oformat", default='png', help="Output format (png, svg)")
fe542273784f Uploaded
bgruening
parents:
diff changeset
19 parser.add_argument("-o", "--output", dest="output_path",
fe542273784f Uploaded
bgruening
parents:
diff changeset
20 help="Path to the output file.")
fe542273784f Uploaded
bgruening
parents:
diff changeset
21
fe542273784f Uploaded
bgruening
parents:
diff changeset
22 args = parser.parse_args()
fe542273784f Uploaded
bgruening
parents:
diff changeset
23 mds = sklearn.manifold.MDS( n_components=2, max_iter=300, eps=1e-6, dissimilarity='precomputed' )
fe542273784f Uploaded
bgruening
parents:
diff changeset
24 data = numpy.fromfile( args.sm )
fe542273784f Uploaded
bgruening
parents:
diff changeset
25 d = math.sqrt( len(data) )
fe542273784f Uploaded
bgruening
parents:
diff changeset
26 sm = numpy.reshape( data, ( d,d ))
fe542273784f Uploaded
bgruening
parents:
diff changeset
27 pos = mds.fit( sm ).embedding_
fe542273784f Uploaded
bgruening
parents:
diff changeset
28 pylab.scatter( pos[:,0],pos[:,1] )
fe542273784f Uploaded
bgruening
parents:
diff changeset
29 pylab.savefig( args.output_path, format=args.oformat )