예제 #1
0
파일: views.py 프로젝트: kahaeia1/a-plus
def shib_register(request, RegisterForm=BaseRegisterForm, register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    # In some cases, user might have Shibboleth names set but no email
    if attr[settings.SHIB_EMAIL] == None:
        attr[settings.SHIB_EMAIL] = '*****@*****.**'

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 
               'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {'form': form, 'next': redirect_url, 'shib_attrs': attr, 'was_redirected': was_redirected, }
        return render_to_response(register_template_name, context, context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    profile = user.get_profile()
    if attr["student_id"] and attr["student_id"] != "":
        # This is because a student might previously have had a student id. In that case, we don't want to erase it.
        profile.student_id = attr["student_id"].split(':')[-1]
    profile.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)
예제 #2
0
def shib_register(request, RegisterForm=BaseRegisterForm, register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 
               'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {'form': form, 'next': redirect_url, 'shib_attrs': attr, 'was_redirected': was_redirected, }
        return render_to_response(register_template_name, context, context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)
예제 #3
0
    def process_data(self, data_source):
        fp = utils.get_filepath(data_source, 'train')
        data = arff.load(open(fp, 'rb'))
        class_index, header, data_types = utils.parse_attributes(data)
        self.data = []
        self.class_sets = []
        for row in data['data']:
            self.data.append(row[:class_index])
            label = reduce(lambda x,y:x+y, row[class_index:])
            self.class_sets.append(utils.str_to_set(label))
        self.num_data = len(self.class_sets)
        self.num_classes = len(label)
        self.data = np.array(self.data)

        fp = utils.get_filepath(data_source, 'test')
        test_data = arff.load(open(fp, 'rb'))
        self.test_data = []
        self.test_class_sets = []
        for row in test_data['data']:
            self.test_data.append(row[:class_index])
            label = reduce(lambda x,y:x+y, row[class_index:])
            self.test_class_sets.append(utils.str_to_set(label))
        self.test_num_data = len(self.test_class_sets)
        self.test_data = np.array(self.test_data)
예제 #4
0
new_topk_d = {'emotions': 2, 'scene': 2, 'yeast': 2}
new_topk_loops = {'emotions': 10, 'scene': 10, 'yeast': 10}

# ================================================================
# ADAPTIVE ALGORITHM
ada_topk_best = {'emotions': 0.2427, 'scene': 0.1216}

ada_topk_N = {'emotions': 70, 'scene': 50}
# all other parameters, we'll inherit from optimal hyperparameters

# In[3]:

data_source = 'yeast'
fp = utils.get_filepath(data_source, 'train')
data = arff.load(open(fp, 'rb'))
class_index, header, data_types = utils.parse_attributes(data)
train_rows = data['data']
fp = utils.get_filepath(data_source, 'test')
data = arff.load(open(fp, 'rb'))
test_rows = data['data']

if data_source == 'mediamill_reduced':
    train_rows = train_rows[0:400]
    test_rows = test_rows[0:100]

loss = 'logistic'
num_wls = 5
num_covs = 20
gamma = 0.3
M = 100
k = 3
예제 #5
0
def shib_register(request,
                  RegisterForm=BaseRegisterForm,
                  register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {
            'form': form,
            'next': redirect_url,
            'shib_attrs': attr,
            'was_redirected': was_redirected,
        }
        return render_to_response(register_template_name,
                                  context,
                                  context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)
def main():
    seed = np.random.randint(1, 999)
    # Read params.csv file and parse the options
    params = utils.read_params()
    loss = params['loss']
    data_source = params['data_source']
    num_wls = int(params['num_wls'])
    num_covs = int(params['num_covs'])
    M = int(params['M'])
    gamma = params['gamma']

    # Load the train data
    fp = utils.get_filepath(data_source, 'train')
    data = arff.load(open(fp, 'rb'))
    class_index, _, _ = utils.parse_attributes(data)
    train_rows = data['data']

    # Load the test data
    fp = utils.get_filepath(data_source, 'test')
    data = arff.load(open(fp, 'rb'))
    test_rows = data['data']

    start = time.time()

    model = AdaOLMR(data_source, loss=loss, num_covs=num_covs, gamma=gamma)
    model.M = M
    model.gen_weaklearners(num_wls,
                           min_grace=5,
                           max_grace=20,
                           min_tie=0.01,
                           max_tie=0.9,
                           min_conf=0.01,
                           max_conf=0.9,
                           min_weight=3,
                           max_weight=10,
                           seed=seed)

    for i, row in enumerate(train_rows):
        X = row[:class_index]
        Y = row[class_index:]
        pred = model.predict(X)
        model.update(Y)

    cum_error = 0

    for i, row in enumerate(test_rows):
        X = row[:class_index]
        Y = row[class_index:]
        pred = model.predict(X)
        model.update(Y)
        cum_error += utils.rank_loss(pred, model.Y)

    end = time.time()
    runtime = round(end - start, 2)
    avg_loss = round(cum_error / float(len(test_rows)), 4)

    print 'data_source', data_source
    print 'loss', loss
    print 'gamma', gamma
    print 'num_wls', num_wls
    print 'num_covs', num_covs
    print 'M', M
    print 'seed', seed
    print 'runtime', runtime
    print 'avg_loss', avg_loss