Exemplo n.º 1
0
from expWorkbench import load_results
from analysis.prim import perform_prim, write_prim_to_stdout
from analysis.prim import show_boxes_individually


def classify(data):

    result = data["total fraction new technologies"]
    classes = np.zeros(result.shape[0])
    classes[result[:, -1] > 0.8] = 1

    return classes


if __name__ == "__main__":

    results = load_results(r"CESUN_optimized_1000_new.cPickle")
    experiments, results = results
    logicalIndex = experiments["policy"] == "Optimized Adaptive Policy"
    newExperiments = experiments[logicalIndex]
    newResults = {}
    for key, value in results.items():
        newResults[key] = value[logicalIndex]
    results = (newExperiments, newResults)

    boxes = perform_prim(results, "total fraction new technologies", threshold=0.6, threshold_type=-1)

    write_prim_to_stdout(boxes)
    show_boxes_individually(boxes, results)
    plt.show()
Exemplo n.º 2
0
def perform_pca_prim(results, 
                     classify, 
                     exclude=['model', 'policy'],
                     subsets={},
                     peel_alpha = 0.05, 
                     paste_alpha = 0.05,
                     mass_min = 0.05, 
                     threshold = None, 
                     pasting=True, 
                     threshold_type=1,
                     obj_func=prim.def_obj_func):
    '''
    Perform (un)constrained PCA-PRIM. The cases of interest are identified. 
    Next, the experiments are rotated to the eigen space of the covariance 
    matrix of the experiments of interest. 
    
    :param results: the return from perform_experiments
    :param classify: the classify function to be used in PRIM
    :param exclude: The uncertainties that should be excluded, optional argument
    :param subsets: optional kwarg, expects a dictonary with group name as key
                    and a list of uncertainty names as values. If this is used,
                    a constrained PCA-PRIM is executed
                    **note:** the list of uncertainties should not contain 
                    categorical uncertainties. 
    :param classify: either a string denoting the outcome of interest to use
                     or a function. In case of a string and time series data, 
                     the end state is used.
    :param peel_alpha: parameter controlling the peeling stage (default = 0.05). 
    :param paste_alpha: parameter controlling the pasting stage (default = 0.05).
    :param mass_min: minimum mass of a box (default = 0.05). 
    :param threshold: the threshold of the output space that boxes should meet. 
    :param pasting: perform pasting stage (default=True) 
    :param threshold_type: If 1, the boxes should go above the threshold, if -1
                           the boxes should go below the threshold, if 0, the 
                           algorithm looks for both +1 and -1.
    :param obj_func: The objective function to use. Default is 
                     :func:`def_obj_func`
    :return: the rotation_matrix, the row_names, the column_names, 
             the rotated_experiments, and the boxes found by prim              
                    
    
    '''
    orig_experiments, outcomes = results
    
    #transform experiments to numpy array
    dtypes = orig_experiments.dtype.fields
    object_dtypes = [key for key, value in dtypes.items() if value[0]==np.dtype(object)]
    
    #get experiments of interest
    logical = classify(outcomes)==1
    
    # if no subsets are provided all uncertainties with non dtype object are
    # in the same subset, the name of this is r, for rotation
    if not subsets:
#        non_object_dtypes = [key for key, value in dtypes.items() if value[0].name!=np.dtype(object)]
        subsets = {"r":[key for key, value in dtypes.items() if value[0].name!=np.dtype(object)]}
    
    # remove uncertainties that are in exclude and check whether uncertainties
    # occur in more then one subset
    seen = set()
    for key, value in subsets.items():
        value = set(value) - set(exclude)
        subsets[key] = list(value)
        if (seen & value):
            raise EMAError("uncertainty occurs in more then one subset")
        else:
            seen = seen | set(value)
    
    #prepare the dtypes for the new rotated experiments recarray
    new_dtypes = []
    for key, value in subsets.items():
        assert_dtypes(value, dtypes)
        
        # the names of the rotated columns are based on the group name 
        # and an index
        [new_dtypes.append(("%s_%s" % (key, i), float)) for i in range(len(value))]
    
    #add the uncertainties with object dtypes to the end
    included_object_dtypes = set(object_dtypes)-set(exclude)
    [new_dtypes.append((name, object)) for name in included_object_dtypes ]
    
    #make a new empty recarray
    rotated_experiments = np.recarray((orig_experiments.shape[0],),dtype=new_dtypes)
    
    #put the uncertainties with object dtypes already into the new recarray 
    for name in included_object_dtypes :
        rotated_experiments[name] = orig_experiments[name]
    
    #iterate over the subsets, rotate them, and put them into the new recarray
    shape = 0
    for key, value in subsets.items():
        shape += len(value) 
    rotation_matrix = np.zeros((shape,shape))
    column_names = []
    row_names = []
    
    j = 0
    for key, value in subsets.items():
        subset_rotation_matrix, subset_experiments = rotate_subset(value, orig_experiments, logical)
        rotation_matrix[j:j+len(value), j:j+len(value)] = subset_rotation_matrix
        [row_names.append(entry) for entry in value]
        j += len(value)
        
        for i in range(len(value)):
            name = "%s_%s" % (key, i)
            rotated_experiments[name] = subset_experiments[:,i]
            [column_names.append(name)]
    
    results = rotated_experiments, outcomes
    
    #perform prim in the usual way
    
    
    boxes = prim.perform_prim(results, 
                             classify, 
                             peel_alpha=peel_alpha, 
                             paste_alpha=paste_alpha,
                             mass_min=mass_min, 
                             threshold=threshold, 
                             pasting=pasting, 
                             threshold_type=threshold_type,
                             obj_func=obj_func)
    
    return rotation_matrix, row_names, column_names, rotated_experiments, boxes
Exemplo n.º 3
0
    #if deceased population is higher then 1.000.000 people, classify as 1 
    classes[result[:, -1] > 1000000] = 1
    
    return classes

#load data
results = load_results(r'../analysis/1000 flu cases.cPickle')
experiments, results = results

#extract results for 1 policy
logicalIndex = experiments['policy'] == 'no policy'
newExperiments = experiments[ logicalIndex ]
newResults = {}
for key, value in results.items():
    newResults[key] = value[logicalIndex]

results = (newExperiments, newResults)

#perform prim on modified results tuple
boxes = prim.perform_prim(results, classify, 
                                    threshold=0.8, 
                                    threshold_type=1,
                                    pasting=True)

#print prim to std_out
prim.write_prim_to_stdout(boxes)

#visualize
prim.show_boxes_individually(boxes, results)
prim.show_boxes_together(boxes, results)
plt.show()
Exemplo n.º 4
0
'''
Created on Sep 3, 2012

@author: jhkwakkel
'''
import numpy as np
import matplotlib.pyplot as plt

from expWorkbench import load_results, ema_logging
from analysis import  prim

def classify(data):
    
    result = data['total fraction new technologies']    
    classes =  np.zeros(result.shape[0])
    classes[result[:, -1] < 0.6] = 1
    
    return classes

if __name__ == "__main__":
    ema_logging.log_to_stderr(ema_logging.INFO)
    
    results = load_results(r'prim data 100 cases.cPickle')
    boxes = prim.perform_prim(results, 
                      classify=classify,
                      mass_min=0.05, 
                      threshold=0.8)
    prim.show_boxes_together(boxes, results)
    plt.show()
Exemplo n.º 5
0
    #if deceased population is higher then 1.000.000 people,
    #classify as 1
    classes[result[:, -1] > 1000000] = 1

    return classes


#load data
results = load_results(r'../../../src/analysis/1000 flu cases.cPickle',
                       zipped=False)
experiments, results = results

#extract results for 1 policy
logicalIndex = experiments['policy'] == 'no policy'
newExperiments = experiments[logicalIndex]
newResults = {}
for key, value in results.items():
    newResults[key] = value[logicalIndex]

results = (newExperiments, newResults)

#perform prim on modified results tuple
prims, uncertainties, x = prim.perform_prim(results,
                                            classify,
                                            threshold=0.8,
                                            threshold_type=1)

#visualize

figure = prim.visualize_prim(prims, uncertainties, x, filter=True)
plt.show()
Exemplo n.º 6
0

def classify(data):

    result = data['total fraction new technologies']
    classes = np.zeros(result.shape[0])
    classes[result[:, -1] > 0.8] = 1

    return classes


if __name__ == '__main__':

    results = load_results(r'CESUN_optimized_1000_new.cPickle')
    experiments, results = results
    logicalIndex = experiments['policy'] == 'Optimized Adaptive Policy'
    newExperiments = experiments[logicalIndex]
    newResults = {}
    for key, value in results.items():
        newResults[key] = value[logicalIndex]
    results = (newExperiments, newResults)

    boxes = perform_prim(results,
                         'total fraction new technologies',
                         threshold=0.6,
                         threshold_type=-1)

    write_prim_to_stdout(boxes)
    show_boxes_individually(boxes, results)
    plt.show()
Exemplo n.º 7
0
    # make an empty array of length equal to number of cases
    classes = np.zeros(result.shape[0])

    # if deceased population is higher then 1.000.000 people,
    # classify as 1
    classes[result[:, -1] > 1000000] = 1

    return classes


# load data
results = load_results(r"../../../src/analysis/1000 flu cases.cPickle", zipped=False)
experiments, results = results

# extract results for 1 policy
logicalIndex = experiments["policy"] == "no policy"
newExperiments = experiments[logicalIndex]
newResults = {}
for key, value in results.items():
    newResults[key] = value[logicalIndex]

results = (newExperiments, newResults)

# perform prim on modified results tuple
prims, uncertainties, x = prim.perform_prim(results, classify, threshold=0.8, threshold_type=1)

# visualize

figure = prim.visualize_prim(prims, uncertainties, x, filter=True)
plt.show()
Exemplo n.º 8
0
'''
Created on Mar 1, 2012

@author: LocalAdmin
'''
import matplotlib.pyplot as plt
import numpy as np

from expWorkbench import load_results
from analysis.prim import perform_prim, write_prim_to_stdout
from analysis.prim import show_boxes_individually, show_boxes_together

def classify(data):
    
    result = data['total fraction new technologies']    
    classes =  np.zeros(result.shape[0])
    classes[result[:, -1] < 0.6] = 1
    
    return classes

if __name__ == '__main__':

    results = load_results(r'CESUN_optimized_1000.cPickle')
    boxes = perform_prim(results,classify, threshold=0.6, threshold_type=1)
    
    write_prim_to_stdout(boxes)
    show_boxes_individually(boxes, results)
    plt.show()