Ejemplo n.º 1
0
    def test_KernelSmoothedFromBatchClassificationResult(self):
        """Kernel Smoothed Probability density graph from a single split"""

        testfilename = 'test_graph_kernel_smoothed.npy'
        graph = PredictedValuesGraph(self.batch_result)
        graph.KernelSmoothedDensityGraph()
        self.CompareGraphs(graph, testfilename)
Ejemplo n.º 2
0
    def test_FromDiscreteClassificationExperimentResults(self):
        """Rank Ordered Predicted values graph from an experiment result (multiple splits)"""

        testfilename = 'test_graph_rank_ordered_experiment.npy'

        # Make a smaller featureset to do multiple splits
        fs_kwargs = {}
        fs_kwargs['name'] = "DiscreteArtificialFS RANK ORDERED SHUFFLE SPLIT"
        fs_kwargs['n_samples'] = 100  # smaller
        fs_kwargs['n_classes'] = 5  # smaller, 20 samples per class
        fs_kwargs['num_features_per_signal_type'] = 10  # smaller
        fs_kwargs['initial_noise_sigma'] = 50
        fs_kwargs['noise_gradient'] = 20
        fs_kwargs['n_samples_per_group'] = 1
        fs_kwargs['interpolatable'] = True
        fs_kwargs['random_state'] = 42
        fs_kwargs['singularity'] = False
        fs_kwargs['clip'] = False

        small_fs = CreateArtificialFeatureSpace_Discrete(**fs_kwargs)

        ss_kwargs = {}
        ss_kwargs['quiet'] = True
        ss_kwargs['n_iter'] = n_iter = 10
        ss_kwargs['train_size'] = train_size = 18  # per-class
        ss_kwargs['test_size'] = test_size = 2  # per-class
        ss_kwargs['random_state'] = 42
        exp = FeatureSpaceClassificationExperiment.NewShuffleSplit(
            small_fs, **ss_kwargs)
        graph = PredictedValuesGraph(exp, use_averaged_results=False)
        graph.RankOrderedPredictedValuesGraph()
        self.CompareGraphs(graph, testfilename)
Ejemplo n.º 3
0
    def test_RankOrderedFromBatchClassificationResult(self):
        """Rank Ordered Predicted values graph from a single split"""

        testfilename = 'test_graph_rank_ordered_interpolated_discrete.npy'
        graph = PredictedValuesGraph(self.batch_result)
        graph.RankOrderedPredictedValuesGraph()
        self.CompareGraphs(graph, testfilename)
Ejemplo n.º 4
0
    def test_ErrMsgIfMatplotibNotInstalled(self):
        """Fail gracefully with informative message if matplotlib"""

        graph = PredictedValuesGraph(self.batch_result)
        with self.assertRaises(ImportError):
            graph.RankOrderedPredictedValuesGraph()
        with self.assertRaises(ImportError):
            graph.KernelSmoothedDensityGraph()
Ejemplo n.º 5
0
    def test_IfNotInterpolatable( self ):
        """You can't graph predicted values if the classes aren't interpolatable."""

        testfilename = 'ShouldntBeGraphable.png'
        small_fs = CreateArtificialFeatureSpace_Discrete( 
                        n_samples=20, n_classes=2, random_state=42, interpolatable=False )
        train_set, test_set = small_fs.Split( random_state=False, quiet=True )
        train_set.Normalize()

        fw = FisherFeatureWeights.NewFromFeatureSpace( train_set ).Threshold()
        reduced_train_set = train_set.FeatureReduce( fw )
        reduced_test_set = test_set.FeatureReduce( fw )
        test_set.Normalize( train_set, quiet=True )

        batch_result = FeatureSpaceClassification.NewWND5(
                                    reduced_train_set, reduced_test_set, fw, quiet=True )
        with self.assertRaises( ValueError ):
            graph = PredictedValuesGraph( batch_result )
Ejemplo n.º 6
0
    def test_FromHTML( self ):
        """Rank Ordered Predicted values graph from an experiment result (multiple splits)"""

        testfilename = 'test_graph_fromHTML.npy'
         # Inflate the zipped html file into a temp file
        import zipfile

        #zipped_file_path = pychrm_test_dir + sep + 'c_elegans_terminal_bulb.html'
        #import zlib
        #zf = zipfile.ZipFile( zipped_file_path + '.zip', mode='w' )
        #zf.write( zipped_file_path, compress_type=zipfile.ZIP_DEFLATED )
        #zf.close()
 
        zipped_file_path = pychrm_test_dir + sep + 'c_elegans_terminal_bulb.html.zip'
        zf = zipfile.ZipFile( zipped_file_path, mode='r' )
        zf.extractall( self.tempdir )
        htmlfilepath = self.tempdir + sep + zf.namelist()[0]
        graph = PredictedValuesGraph.NewFromHTMLReport( htmlfilepath, use_averaged_results=False ) 
        graph.RankOrderedPredictedValuesGraph()

        self.CompareGraphs( graph, testfilename )
Ejemplo n.º 7
0
rank_ordered_graphs = []
ks_density_graphs = []

norm_full_set = full_set.Normalize()
# arg _all=True performs rank order sort without reducing number of features
sorted_full_set_pearson_weights = PearsonFeatureWeights.NewFromFeatureSpace( norm_full_set ).Threshold( _all=True ) 

for bin_index in range( num_bins ):
    name = "{0}\nFeatures ranked {1}-{2}".format( full_set.source_path,
                                         bin_offset + 1, bin_offset + num_features_per_bin )
    print name

    sliced_pearson_weights = sorted_full_set_pearson_weights.Slice( bin_offset, bin_offset + num_features_per_bin )
    sliced_fs = full_set.FeatureReduce( sliced_pearson_weights )
    experiment = FeatureSpaceRegressionExperiment.NewShuffleSplit( sliced_fs )

    grapher = PredictedValuesGraph( experiment )
    grapher.RankOrderedPredictedValuesGraph( name )
    grapher.SaveToFile( "rank_ordered_features_{0:03d}-{1:03d}".format( bin_offset + 1, bin_offset + num_features_per_bin  ) )

    grapher.KernelSmoothedDensityGraph( name )
    grapher.SaveToFile( "ks_density_features_{0:03d}-{1:03d}".format( bin_offset + 1, bin_offset + num_features_per_bin ) )

    bin_offset += num_features_per_bin 


import subprocess
subprocess.call( [ "convert", "rank_ordered_features*", "rank_ordered.pdf"] )
subprocess.call( [ "convert", "ks_density_features*", "ks_density.pdf"] )

Ejemplo n.º 8
0
    figure_basename = args.figure_basename

from os.path import splitext
figure_basename, extension = splitext(figure_basename)
figure_basename = figure_basename.replace('.', '_')

print "using figure basename {0}".format(figure_basename)

text_out = None
if args.debug is 'unset':
    # unset means user doesn't need debug out
    print "User doesn't want to see debug output, sending it to /dev/null"
    from os import devnull
    text_out = devnull
elif args.debug is not None:
    text_out = args.debug
    print "Sending debug output to file {0}".format(text_out)
else:
    print "Sending debug output to STDOUT"

if text_out:
    graph = PredictedValuesGraph.NewFromHTMLFile(html_file,
                                                 output_filepath=text_out)
else:
    graph = PredictedValuesGraph.NewFromHTMLFile(html_file)

graph.KernelSmoothedDensityGraph(chart_title=html_file)
graph.SaveToFile(figure_basename + '-ks_density')
graph.RankOrderedPredictedValuesGraph(chart_title=html_file)
graph.SaveToFile(figure_basename + '-rank-ordered')