Changeset 608


Ignore:
Timestamp:
09/01/16 17:01:37 (8 years ago)
Author:
Maciej Komosinski
Message:

Displays the fraction of variance preserved after MDS

File:
1 edited

Legend:

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

    r607 r608  
    7878                firstsquarecolumn=distances.shape[1]-minsize
    7979                distances = np.array([row[firstsquarecolumn:] for row in distances]) #this can only fix matrices with more columns than rows
    80                 print("Making it square:",distances.shape)
     80                print("Made the matrix square:",distances.shape)
    8181
    8282                #if the file has more columns than rows, assume the first extra column on the left of the square matrix has labels
     
    9393
    9494        variances = [np.var(embed[:,i]) for i in range(len(embed[0]))]
    95         percent_variances = [sum(variances[:i+1])/sum(variances) for i in range(len(variances))]
    96         for i,pv in enumerate(percent_variances):
     95        variances_fraction = [sum(variances[:i+1])/sum(variances) for i in range(len(variances))]
     96        for i,pv in enumerate(variances_fraction):
    9797                print("In",i+1,"dimensions:",pv)
    9898
     
    100100        embed = np.asarray([embed[:,i] for i in range(dim)]).T
    101101
    102         return embed
     102        return embed, variances_fraction[dim-1]
    103103
    104104
    105 def plot(coordinates, labels, dimensions, jitter=0, outname=""):
     105def plot(coordinates, labels, dimensions, variance_fraction, jitter=0, outname=""):
    106106        fig = plt.figure()
    107107
     
    138138
    139139
    140         plt.title('Phenotypes distances') #TODO add % variance information
     140        plt.title('Projection of phenotype distances, variance preserved = %.1f%%' % (variance_fraction*100))
    141141        plt.tight_layout()
    142142        plt.axis('tight')
     
    151151def main(filename, dimensions=3, outname="", jitter=0, separator='\t'):
    152152        distances,labels = read_file(filename, separator)
    153         embed = compute_mds(distances, dimensions)
     153        embed,variance_fraction = compute_mds(distances, dimensions)
    154154
    155155        if dimensions == 1:
    156156                embed = np.array([np.insert(e, 0, 0, axis=0) for e in embed])
    157157       
    158         plot(embed, labels, dimensions, jitter, outname)
     158        plot(embed, labels, dimensions, variance_fraction, jitter, outname)
    159159
    160160
Note: See TracChangeset for help on using the changeset viewer.