library.py 3.67 KB
Newer Older
1 2
import cPickle

Janez K's avatar
Janez K committed
3 4 5 6 7 8 9
def base_concatenate_lists(input_dict):
    lists = input_dict['lists']
    new_list = []
    for every_list in lists:
        new_list = new_list+every_list
    output_dict = {}
    output_dict['list']=new_list
10 11
    return output_dict

12 13 14
def create_list(input_dict):
    return input_dict

15 16 17 18 19 20
def create_tuple(input_dict):
    lists = input_dict['elements']
    output_dict = {}
    output_dict['tuple'] = tuple(lists)
    return output_dict

matjaz's avatar
matjaz committed
21 22 23 24 25 26 27 28 29
def create_dictionary(input_dict):
    keys = input_dict['keys']
    values = input_dict['values']
    dic = {}
    for idx in range(0,min(len(keys),len(values))):
        dic[keys[idx]] = values[idx]
    output_dict = {'dict': dic}
    return output_dict

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
def create_range(input_dict):
    output_dict = {}
    output_dict['rangeout'] = range(int(input_dict['n_range']))
    return output_dict

def delay(input_dict,widget):
    widget.progress=0
    widget.save()
    timeleft = int(input_dict['time'])
    i = 0
    import time
    import math
    while i<timeleft:
        time.sleep(1)
        i=i+1
        widget.progress = math.floor(((i*1.0)/timeleft)*100)
        widget.save()
    widget.progress=100
    widget.save()
    output_dict = {}
    output_dict['data'] = input_dict['data']
    return output_dict

# ENSEMBLE

def ensemble(input_dict):
    import math
    ens = {}
    data_inds = input_dict['data_inds']
    ens_type = input_dict['ens_type']
    # TODO ens_level = input_dict['ens_level']
    for item in data_inds:
        #det_by = item['detected_by']
        for i in item['inds']:
            if not ens.has_key(i):
                ens[i] = 1
            else:
                ens[i] += 1

    ens_out = {}
    ens_out['name'] = input_dict['ens_name']
    ens_out['inds'] = []
    n_algs = len(data_inds)
    print ens_type
    if ens_type == "consensus":
        ens_out['inds'] = sorted([x[0] for x in ens.items() if x[1] == n_algs])
    else: # majority
        ens_out['inds'] = sorted([x[0] for x in ens.items() if x[1] >= math.floor(n_algs/2+1)])

    output_dict = {}
    output_dict['ens_out'] = ens_out
    return output_dict

def pickle_object(input_dict):
    '''
    Serializes the input object.
    '''
    pkl_obj = cPickle.dumps(input_dict['object'])
    output_dict = {}
    output_dict['pickled_object'] = pkl_obj
    return output_dict

def unpickle_object(input_dict):
    '''
    Serializes the input object.
    '''
    obj = cPickle.loads(str(input_dict['pickled_object']))
    output_dict = {}
    output_dict['object'] = obj
    return output_dict

101 102 103 104 105 106 107 108 109 110 111 112 113
def stopwatch(input_dict):
    import time
    inputTime = input_dict['time_in']
    now = time.time()
    if (isinstance(inputTime, float)):
        elapsedTime = now - inputTime
    else:
        elapsedTime = None
    output_dict = {}
    output_dict['signal_out'] = input_dict['signal']
    output_dict['time_out'] = now
    output_dict['time_span'] = elapsedTime

vpodpecan's avatar
vpodpecan committed
114 115 116 117 118 119 120 121 122 123 124 125
    return output_dict


def base_safe_eval_string(input_dict):
    import ast
    sdata = str(input_dict['data'])
    try:
        result = ast.literal_eval(sdata)
    except Exception:
        raise Exception('Cannot evaluate string (remember, for safety reasons only literal structures can be evaluated: strings, numbers, tuples, lists, dicts, booleans, and None)')
    else:
        return {'evaluation_result': result}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#end


def base_ravel_list(input_dict):
    def ravel(data, result):
        for x in data:
            if not isinstance(x, list):
                result.append(x)
            else:
                if x:
                    ravel(x, result)
    #end

    ilist = input_dict['input_list']
    result = []
    ravel(ilist, result)
    return {'clean_list': result}
Janez K's avatar
Janez K committed
143 144 145 146
#end

def base_wordcloud(input_dict):
    return {}