예제 #1
0
def post_dataset(DataSet):
    DataSet['weight'] = DataSet.apply(
            lambda x: get_weights(x['tweetRetweetCt'], x['tweetFavoriteCt'], x['sentiment'],
                                  x['verified'], x['followerCount']), axis=1)

    DataSet.drop_duplicates(subset='tweetText', keep='first', inplace=True)
    DataSet.sort_values('weight', ascending=False)
    return DataSet
예제 #2
0
def format_data_lighthousestudio(csv_data, csv_labels, weighing_method):
    ''' Format data from Lighthouse Studio from Sawtooth Software
    '''
    num_of_attributes = len(csv_labels)
    attr_index = 1

    for attribute_name, attribute in csv_labels.items():
        # Generate level data
        # Initiate empty lists
        attribute['values'] = []
        attribute['pos_error'] = []
        attribute['neg_error'] = []

        # Loop over each level and calculate mean and std
        for level in attribute['level_col_names']:
            level_data = csv_data[level]
            level_mean = level_data.mean()
            level_std = level_data.std()

            attribute['values'].append(level_mean)

            if level_mean + level_std < attribute['max_value_level']:
                attribute['pos_error'].append(level_std)
            else:
                attribute['pos_error'].append(attribute['max_value_level'] - level_mean)

            if level_mean - level_std > attribute['min_value_level']:
                attribute['neg_error'].append(level_std)
            else:
                attribute['neg_error'].append(abs(attribute['min_value_level'] - level_mean))

        # Generate rank score for attribute, see https://help.surveymonkey.com/articles/en_US/kb/How-do-I-create-a-Ranking-type-question
        attribute_rank = 0

        for i in range(1, num_of_attributes + 1):
            attribute_rank += csv_data['Ranking_' + str(i)].tolist().count(attribute['ranking_id']) * (num_of_attributes + 1 - i)

        attribute['rank'] = attribute_rank

    # Generate actual weights list from method
    weights = get_weights(weighing_method, num_of_attributes)

    # Sort csv_labels by highest rank and add weights and index into attributes by iterating
    attributes = dict(sorted(csv_labels.items(), key=lambda k: -k[1]['rank']))

    for attribute_name, attribute in attributes.items():
        attribute['weight'] = weights[0]
        weights.pop(0)

        attribute['index'] = attr_index
        attr_index += 1

    return attributes
예제 #3
0
    def _get_cparameters(self, model): # creates a c-array which holds parameter values (expected by c-model)
        is32bit = sizeof(c_size_t) == 4

        #print model_info
        #print parameters
        # determine size and offsets
        data_size = 0
        offsets   = []
        poly = {}
        for p in model.model_info.parameters:
            offsets.append(data_size)
            if p.name not in model.dispersion:
                data_size += sizeof(c_size_t) # size_t type = ParameterType.Simple;
                data_size += sizeof(c_double) # double value;
            else:
                relative = (p.flags & ~ParameterFlags.Polydisperse) != 0
                w = get_weights(model.dispersion[p.name], model.params[p.name],
                                p.dispmin, p.dispmax, relative)
                poly[p.name] = w
                npoints = len(w)/2
                
                data_size += sizeof(c_size_t)            # size_t type = ParameterType.Polydisperse;
                data_size += sizeof(c_size_t)            # size_t npoints;
                data_size += sizeof(c_double) * npoints  # double values[npoints];
                data_size += sizeof(c_double) * npoints  # double weights[npoints];

        offsets.append(data_size)
        data_size += sizeof(c_size_t) # size_t type = ParameterType.End;

        # determine header size
        header_size  = sizeof(c_size_t)                # size_t count;
        header_size += sizeof(c_size_t) * len(offsets) # size_t offsets[count];

        # create buffer
        buffer = create_string_buffer(header_size + data_size)
        # write header
        if is32bit:
            struct.pack_into('I%iI' % len(offsets), buffer, 0, len(offsets), *offsets)
        else:
            struct.pack_into('Q%iQ' % len(offsets), buffer, 0, len(offsets), *offsets)
        # wrtie data
        offset = header_size
        for p in model.model_info.parameters:
            if p.name not in model.dispersion:
                if is32bit:
                    struct.pack_into('I', buffer, offset, ParameterType.Simple)
                else:
                    struct.pack_into('Q', buffer, offset, ParameterType.Simple)
                offset += sizeof(c_size_t) # size_t type = ParameterType.Simple;

                struct.pack_into('=d', buffer, offset, model.params[p.name])
                offset += sizeof(c_double) # double value;
            else:
                w = poly[p.name]
                npoints = len(w)/2

                if is32bit:
                    struct.pack_into('II', buffer, offset, ParameterType.Polydisperse, npoints)
                else:
                    struct.pack_into('QQ', buffer, offset, ParameterType.Polydisperse, npoints)
                offset += sizeof(c_size_t) # size_t type = ParameterType.Polydisperse;
                offset += sizeof(c_size_t) # size_t npoints;

                format = '=%id' % npoints
                struct.pack_into(format, buffer, offset, *w[0::2])
                offset += sizeof(c_double) * npoints  # double values[npoints];
                struct.pack_into(format, buffer, offset, *w[1::2])
                offset += sizeof(c_double) * npoints  # double weights[npoints];

        # write end
        if is32bit:
            struct.pack_into('I', buffer, offset, ParameterType.End)
        else:
            struct.pack_into('Q', buffer, offset, ParameterType.End)

        return buffer
예제 #4
0
    def _get_cparameters(
        self, model
    ):  # creates a c-array which holds parameter values (expected by c-model)
        is32bit = sizeof(c_size_t) == 4

        #print model_info
        #print parameters
        # determine size and offsets
        data_size = 0
        offsets = []
        poly = {}
        for p in model.model_info.parameters:
            offsets.append(data_size)
            if p.name not in model.dispersion:
                data_size += sizeof(
                    c_size_t)  # size_t type = ParameterType.Simple;
                data_size += sizeof(c_double)  # double value;
            else:
                relative = (p.flags & ~ParameterFlags.Polydisperse) != 0
                w = get_weights(model.dispersion[p.name], model.params[p.name],
                                p.dispmin, p.dispmax, relative)
                poly[p.name] = w
                npoints = len(w) / 2

                data_size += sizeof(
                    c_size_t)  # size_t type = ParameterType.Polydisperse;
                data_size += sizeof(c_size_t)  # size_t npoints;
                data_size += sizeof(
                    c_double) * npoints  # double values[npoints];
                data_size += sizeof(
                    c_double) * npoints  # double weights[npoints];

        offsets.append(data_size)
        data_size += sizeof(c_size_t)  # size_t type = ParameterType.End;

        # determine header size
        header_size = sizeof(c_size_t)  # size_t count;
        header_size += sizeof(c_size_t) * len(
            offsets)  # size_t offsets[count];

        # create buffer
        buffer = create_string_buffer(header_size + data_size)
        # write header
        if is32bit:
            struct.pack_into('I%iI' % len(offsets), buffer, 0, len(offsets),
                             *offsets)
        else:
            struct.pack_into('Q%iQ' % len(offsets), buffer, 0, len(offsets),
                             *offsets)
        # wrtie data
        offset = header_size
        for p in model.model_info.parameters:
            if p.name not in model.dispersion:
                if is32bit:
                    struct.pack_into('I', buffer, offset, ParameterType.Simple)
                else:
                    struct.pack_into('Q', buffer, offset, ParameterType.Simple)
                offset += sizeof(
                    c_size_t)  # size_t type = ParameterType.Simple;

                struct.pack_into('=d', buffer, offset, model.params[p.name])
                offset += sizeof(c_double)  # double value;
            else:
                w = poly[p.name]
                npoints = len(w) / 2

                if is32bit:
                    struct.pack_into('II', buffer, offset,
                                     ParameterType.Polydisperse, npoints)
                else:
                    struct.pack_into('QQ', buffer, offset,
                                     ParameterType.Polydisperse, npoints)
                offset += sizeof(
                    c_size_t)  # size_t type = ParameterType.Polydisperse;
                offset += sizeof(c_size_t)  # size_t npoints;

                format = '=%id' % npoints
                struct.pack_into(format, buffer, offset, *w[0::2])
                offset += sizeof(c_double) * npoints  # double values[npoints];
                struct.pack_into(format, buffer, offset, *w[1::2])
                offset += sizeof(
                    c_double) * npoints  # double weights[npoints];

        # write end
        if is32bit:
            struct.pack_into('I', buffer, offset, ParameterType.End)
        else:
            struct.pack_into('Q', buffer, offset, ParameterType.End)

        return buffer
예제 #5
0
def result():
    list = weights.get_weights(users.user_id())
    return render_template("result.html", messages=list)