Changeset 613


Ignore:
Timestamp:
09/11/16 03:59:38 (8 years ago)
Author:
Maciej Komosinski
Message:

Introduced options to flip (invert, negate) X and/or Y axes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/mds_plot.py

    r609 r613  
    103103
    104104
    105 def plot(coordinates, labels, dimensions, variance_fraction, jitter=0, outname=""):
     105def plot(coordinates, labels, dimensions, variance_fraction, jitter, flipX, flipY, outname=""):
    106106        fig = plt.figure()
    107107
     
    111111                ax = fig.add_subplot(111, projection='3d')
    112112
    113         add_jitter = lambda tab : rand_jitter(tab, jitter) if jitter>0 else tab
    114 
    115113        x_dim = len(coordinates[0])
    116114        y_dim = len(coordinates)
    117115
     116        if flipX:
     117                coordinates=np.hstack((-coordinates[:, [0]], coordinates[:, [1]]))
     118        if flipY:
     119                coordinates=np.hstack((coordinates[:, [0]], -coordinates[:, [1]]))
     120
     121        add_jitter = lambda tab: rand_jitter(tab, jitter) if jitter>0 else tab
    118122        points = [add_jitter(coordinates[:, i]) for i in range(x_dim)]
    119123       
    120124        if labels is not None and dimensions==2: #could be ported to 3D too
    121125                ax.scatter(*points, alpha=0) #invisible points, because we will show labels instead
    122                 labelconvert={'velland':'V','velwat':'W','vpp':'P','vpa':'A'} #use this if you want to replace long names with short IDs
    123                 colors={'velland':'green','velwat':'blue','vpp':'red','vpa':'violet'}
     126                labelconvert={'velland_':'V','velwat_':'W','vpp_':'P','vpa_':'A'} #use this if you want to replace long names with short IDs
     127                colors={'velland_':'green','velwat_':'blue','vpp_':'red','vpa_':'violet'}
    124128                #for point in points:
    125129                #       print(point)
     
    154158
    155159
    156 def main(filename, dimensions=3, outname="", jitter=0, separator='\t'):
     160def main(filename, dimensions=3, outname="", jitter=0, separator='\t', flipX=False, flipY=False):
    157161        distances,labels = read_file(filename, separator)
    158162        embed,variance_fraction = compute_mds(distances, dimensions)
     
    161165                embed = np.array([np.insert(e, 0, 0, axis=0) for e in embed])
    162166       
    163         plot(embed, labels, dimensions, variance_fraction, jitter, outname)
     167        plot(embed, labels, dimensions, variance_fraction, jitter, flipX, flipY, outname)
    164168
    165169
     
    171175        parser.add_argument('--sep', required=False, help='separator of the source file')
    172176        parser.add_argument('--j', required=False, help='for j>0, random jitter is added to points in the plot')
     177        parser.add_argument('--flipX', required=False, dest='flipX', action='store_true')
     178        parser.add_argument('--flipY', required=False, dest='flipY', action='store_true')
     179        parser.set_defaults(flipX=False)
     180        parser.set_defaults(flipY=False)
    173181
    174182        args = parser.parse_args()
    175         set_value = lambda value, default : default if value == None else value
    176         main(args.input, int(set_value(args.dim, 3)), set_value(args.output, ""), float(set_value(args.j, 0)), set_value(args.sep, "\t"))
     183        set_value = lambda value, default: default if value == None else value
     184        main(args.input, int(set_value(args.dim, 3)), set_value(args.output, ""), float(set_value(args.j, 0)), set_value(args.sep, "\t"), args.flipX, args.flipY)
Note: See TracChangeset for help on using the changeset viewer.