def html_reset_password(token): jwt_data = decode_jwt(token) if jwt_data['role'] != 'password': raise APIException('Access denied', 401) if request.method == 'GET': return render_template('reset_password.html', data={ 'host': os.environ.get('API_HOST'), 'token': token }) # request.method == 'PUT' body = request.get_json() check_params(body, 'email', 'password') user = Users.query.filter_by(id=jwt_data['sub'], email=body['email']).first() if not user: raise APIException('User not found', 404) user.password = sha256(body['password']) db.session.commit() return jsonify({'message': 'Your password has been updated'}), 200
def login(): req = request.get_json() check_params(req, 'email', 'password', 'device_token') user = Users.query.filter_by(email=req['email'], password=sha256(req['password'])).first() if user is None: raise APIException('Sorry you entered the wrong email or password', 404) if user.status._value_ == 'invalid': raise APIException('Email not validated', 405) if user.status._value_ == 'suspended': raise APIException('Your account is suspended', 405) is_token_registered = \ Devices.query.filter_by( token=req['device_token'] ).first() is not None profile_exists = Profiles.query.get(user.id) is not None if profile_exists and not is_token_registered: db.session.add(Devices(user_id=user.id, token=req['device_token'])) db.session.commit() return jsonify({ 'jwt': create_jwt({ 'id': user.id, 'role': 'user', 'exp': req.get('exp', 15) }) }), 200
def login(): if request.method == 'GET': return render_template('login.html', host=os.environ.get('API_HOST')) json = request.get_json() utils.check_params(json, 'email', 'password') user = Users.query.filter_by(email=json['email'], password=utils.sha256( json['password'])).first() if user is None: return jsonify({ 'login': False, 'message': 'Email and password are incorrect', }) return jsonify({ 'login': True, 'jwt': create_jwt({ 'id': user.id, 'role': 'admin' }) })
def update_email(id): if id == 'me': id = str(get_jwt()['sub']) if not id.isnumeric(): raise APIException('Invalid id: ' + id, 400) body = request.get_json() check_params(body, 'email', 'password', 'new_email') user = Users.query.filter_by(id=int(id), email=body['email'], password=sha256(body['password'])).first() if not user: raise APIException('Invalid parameters', 400) user.valid = False user.email = body['new_email'] db.session.commit() return jsonify({ 'message': 'Please verify your new email', 'validation_link': validation_link(user.id) }), 200
def create_buy_in(): body = request.get_json() check_params(body, 'flight_id', 'chips', 'table', 'seat') id = int(get_jwt()['sub']) prof = Profiles.query.get(id) if not prof: raise APIException('User not found', 404) buyin = Buy_ins(user_id=id, flight_id=body['flight_id'], chips=body['chips'], table=body['table'], seat=body['seat']) db.session.add(buyin) db.session.commit() name = prof.nickname if prof.nickname else f'{prof.first_name} {prof.last_name}' # Get the latest entry by checking the created_at buyin = Buy_ins.query.filter_by(user_id=id, flight_id=body['flight_id'], chips=body['chips'], table=body['table'], seat=body['seat']).first() return jsonify({**buyin.serialize(), "name": name}), 200
def register_user(): body = request.get_json() check_params(body, 'email', 'password', 'device_token') # If user exists and failed to validate his account user = (Users.query.filter_by(email=body['email'], password=sha256( body['password'])).first()) if user and user.valid == False: data = {'validation_link': validation_link(user.id)} send_email(type='email_validation', to=user.email, data=data) return jsonify({ 'message': 'Another email has been sent for email validation' }) elif user and user.valid: raise APIException('User already exists', 405) user = Users(email=body['email'], password=sha256(body['password'])) db.session.add(user) db.session.add(Devices(token=body['device_token'], user=user)) db.session.commit() user = Users.query.filter_by(email=body['email']).first() send_email(type='email_validation', to=user.email, data={'validation_link': validation_link(user.id)}) return jsonify({'message': 'Please verify your email'}), 200
def login_admin(): if request.method == 'GET': return render_template('login.html', host=os.environ.get('API_HOST')) json = request.get_json() utils.check_params(json, 'email', 'password') user = Users.query.filter_by(email=json['email'], password=utils.sha256( json['password'])).first() if user is None: return jsonify({ 'login': False, 'message': 'Email and password are incorrect', }) identity = {'id': user.id, 'role': 'admin', 'sub': user.id} return jsonify({ 'login': True, 'jwt': jwt.encode(identity, os.environ['JWT_SECRET_KEY'], algorithm='HS256') })
def html_reset_password(token): jwt_data = decode_jwt(token) if request.method == 'GET': user = Users.query.filter_by(id=jwt_data['sub'], email=jwt_data['role']).first() if user is None: raise APIException('User not found', 404) return render_template('reset_password.html', host=os.environ.get('API_HOST'), token=token, email=jwt_data['role']) # request.method == 'PUT' req = request.get_json() utils.check_params(req, 'email', 'password') if len(req['password']) < 6: raise APIException('Password must be at least 6 characters long') user = Users.query.filter_by(id=jwt_data['sub'], email=req['email']) if user is None: raise APIException('User not found', 404) user.password = utils.sha256(req['password']) db.session.commit() return jsonify({'message': 'Your password has been updated'}), 200
def create_buy_in(user_id): body = request.get_json() check_params(body, 'flight_id', 'chips', 'table', 'seat') prof = Profiles.query.get(user_id) buyin = Buy_ins(user_id=user_id, flight_id=body['flight_id'], chips=body['chips'], table=body['table'], seat=body['seat']) db.session.add(buyin) db.session.commit() name = prof.nickname if prof.nickname else f'{prof.first_name} {prof.last_name}' buyin = Buy_ins.query.filter_by(user_id=user_id, flight_id=body['flight_id'], chips=body['chips'], table=body['table'], seat=body['seat']).order_by( Buy_ins.id.desc()).first() return jsonify({**buyin.serialize(), "name": name}), 200
def update_email(user_id): req = request.get_json() utils.check_params(req, 'email', 'password', 'new_email') if req['email'] == req['new_email']: return jsonify( {'message': 'Your email is already ' + req['new_email']}) user = Users.query.filter_by(id=user_id, email=req['email'], password=utils.sha256( req['password'])).first() if user is None: raise APIException('User not found', 404) user.status = 'invalid' user.email = req['new_email'] db.session.commit() send_email(template='email_validation', emails=user.email, data={ 'validation_link': utils.jwt_link(user.id, role='email_change') }) return jsonify({'message': 'Please verify your new email'}), 200
def swapprofit_user(): json = request.get_json() utils.check_params(json, 'api_token', 'email', 'password', 'first_name', 'last_name') if json['api_token'] != utils.sha256(os.environ['API_TOKEN']): raise APIException('Invalid api token', 400) # Find user in db user = Users.query.filter_by(email=json['email']).first() # If no user found, create one if user is None: print('user is None', end='\n') user = Users(email=json['email'], password=json['password'], first_name=json['first_name'], last_name=json['last_name'], nickname=json.get('nickname'), hendon_url=json.get('hendon_url'), status='valid') db.session.add(user) db.session.commit() return jsonify({'pokersociety_id': user.id})
def reset_password(id): if id == 'me': id = str(get_jwt())['sub'] if not id.isnumeric(): raise APIException('Invalid id: ' + id, 400) if request.args.get('forgot') == 'true': return jsonify({ 'message': 'A link has been sent to your email to reset the password', 'link': os.environ.get('API_HOST') + '/users/reset_password/' + create_jwt({ 'id': id, 'role': 'password' }) }), 200 body = request.get_json() check_params(body, 'email', 'password', 'new_password') user = Users.query.filter_by(id=int(id), email=body['email'], password=sha256(body['password'])).first() if not user: raise APIException('Invalid parameters', 400) user.password = sha256(body['new_password']) db.session.commit() return jsonify({'message': 'Your password has been changed'}), 200
def add_device(user_id): req = request.get_json() utils.check_params(req, 'device_token') db.session.add(Devices( user_id = user_id, token = req['device_token'] )) db.session.commit() return jsonify({'message':'Device added successfully'})
def add_device(user_id): body = request.get_json() check_params(body, 'token') db.session.add(Devices(user_id=user_id, token=body['token'])) db.session.commit() return jsonify({'message': 'Device added successfully'})
def update_swap(): id = int(get_jwt()['sub']) # get sender user sender = Profiles.query.get(id) if not sender: raise APIException('User not found', 404) body = request.get_json() check_params(body, 'tournament_id', 'recipient_id') # get recipient user recipient = Profiles.query.get(body['recipient_id']) if not recipient: raise APIException('Recipient user not found', 404) # get swap swap = Swaps.query.get((id, recipient.id, body['tournament_id'])) counter_swap = Swaps.query.get((recipient.id, id, body['tournament_id'])) if not swap or not counter_swap: raise APIException('Swap not found', 404) if 'percentage' in body: percentage = abs(body['percentage']) counter = abs(body['counter_percentage'] ) if 'counter_percentage' in body else percentage sender_availability = sender.available_percentage( body['tournament_id']) if percentage > sender_availability: raise APIException(( 'Swap percentage too large. You can not exceed 50% per tournament. ' f'You have available: {sender_availability}%'), 400) recipient_availability = recipient.available_percentage( body['tournament_id']) if counter > recipient_availability: raise APIException( ('Swap percentage too large for recipient. ' f'He has available to swap: {recipient_availability}%'), 400) # So it can be updated correctly with the update_table funcion body['percentage'] = swap.percentage + percentage update_table(counter_swap, {'percentage': counter_swap.percentage + counter}) update_table( swap, body, ignore=['tournament_id', 'recipient_id', 'paid', 'counter_percentage']) db.session.commit() return jsonify(swap.serialize())
def train_(self, x_0, x, a): self.train() self.optimizer.zero_grad() loss, info = self(x_0, x, a, prior_sample=False) loss.backward() # clip_grad_norm_(self.distributions.parameters(), 1000) self.optimizer.step() if self.debug: check_params(self) return loss, info
def delete_device(user_id): req = request.get_json() utils.check_params(req, 'device_token') devices = Devices.query.filter_by(token=req['device_token']) for device in devices: db.session.delete(device) db.session.commit() return jsonify({'message': 'Device deleted successfully'})
def update_profile(user_id): prof = Profiles.query.get(user_id) req = request.get_json() utils.check_params(req) utils.update_table(prof, req, ignore=['profile_pic_url']) db.session.commit() return jsonify(prof.serialize())
def invite_users(user_id): req = request.get_json() utils.check_params(req, 'email') user = Users.query.get(user_id) send_email('invitation_email', emails=req['email'], data={'user_name': f'{user.first_name} {user.last_name}'}) return jsonify({'message': 'Invitation sent successfully'})
def update_profile(user_id): prof = Profiles.query.get(user_id) body = request.get_json() check_params(body) update_table(prof, body, ignore=['profile_pic_url']) db.session.commit() return jsonify(prof.serialize())
def svm_pred(argv): """A top level script to parse input parameters and train and predict""" assert(argv[1]=='pred') if len(argv)<6:sys.stderr.write("usage: %s pred C kernelname kernelparameters [arff|fasta] inputfiles outputfile [dna|protein] non(nucleotide|amino)converter\n" % argv[0]);sys.exit(-1) # parse input parameters C = float(argv[2]) (kernelname,kparam,argv_rest) = parse.parse_kernel_param(argv[3:],False) (trainex, trainlab, testex, argv_rest) = parse.parse_input_file_train_test(kernelname, argv_rest) (seq_source, nuc_con) = ('', '') if kernelname == 'spec' or kernelname == 'wd': if len(argv_rest)<1:sys.stderr.write("outputfile [dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<2:sys.stderr.write("[dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<3: if argv_rest[-1] == 'dna': sys.stderr.write("non-nucleotide converter like [A|T|C|G|R|Y|N] is missing. Cannot continue.\n") sys.exit(-1) elif argv_rest[-1] == 'protein': sys.stderr.write("non-amino acid converter like [G|P|A|V|L|I|M|C|F|Y|W|H|K|R|Q|N|E|D|S|T|random] is missing. Cannot continue.\n") sys.exit(-1) else: sys.stderr.write("Here expect FASTA sequence type as [dna|protein] instead of -"+ argv_rest[-1] +"- Cannot continue.\n") sys.exit(-1) if len(argv_rest)>3:sys.stderr.write("Too many arguments\n");sys.exit(-1) seq_source = argv_rest[1] nuc_con = argv_rest[2] if kernelname == 'linear' or kernelname== 'poly' or kernelname == 'gauss': if len(argv_rest)<1:sys.stderr.write("outputfile missing\n");sys.exit(-1) if len(argv_rest)>1:sys.stderr.write("Too many arguments\n");sys.exit(-1) outfilename = argv_rest[0] utils.check_params(kparam, C, len(trainex[0])) # run training and testing svmout = train_and_test(trainex, trainlab, testex, C, kernelname, kparam, seq_source, nuc_con) # write output file try: f = open(outfilename,'w') except: sys.stderr.write('Fails to open the outputfile at ' + outfilename + ' Cannot continue.\n') sys.exit(-1) res_str = '#example\toutput\n' f.write(res_str) for ix in xrange(len(svmout)): res_str = str(ix)+'\t'+str(svmout[ix])+'\n' f.write(res_str) f.close()
def swapprofit_email_update(id): json = request.get_json() utils.check_params(json, 'api_token', 'email') if json['api_token'] != utils.sha256(os.environ['API_TOKEN']): raise APIException('Invalid api token', 400) user = Users.query.get(id) user.email = json['email'] db.session.commit() return jsonify({'message': 'ok'}), 200
def add_coins(user_id): req = request.get_json() utils.check_params(req, 'coins') db.session.add( Transactions(user_id=user_id, coins=req['coins'], dollars=req.get('dollars', 0))) db.session.commit() user = Profiles.query.get(user_id) return jsonify({'total_coins': user.get_coins()})
def svm_cv(argv): """A top level script to parse input parameters and run cross validation""" assert(argv[1]=='cv') if len(argv)<5:sys.stderr.write("usage: %s cv repeat C kernelname [kernelparameters] [arff|fasta] inputfiles outputfile [dna|protein] non(nucleotide|amino)converter \n" % argv[0]);sys.exit(-1) # parse input parameters cv = int(argv[2]) C = float(argv[3]) (kernelname,kparam,argv_rest) = parse.parse_kernel_param(argv[4:],False) (examples,labels,argv_rest) = parse.parse_input_file_train(kernelname, argv_rest) (seq_source, nuc_con) = ('', '') if kernelname == 'spec' or kernelname == 'wd': if len(argv_rest)<1:sys.stderr.write("outputfile [dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<2:sys.stderr.write("[dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<3: if argv_rest[-1] == 'dna': sys.stderr.write("non-nucleotide converter like [A|T|C|G|R|Y|N] is missing. Cannot continue.\n") sys.exit(-1) elif argv_rest[-1] == 'protein': sys.stderr.write("non-amino acid converter like [G|P|A|V|L|I|M|C|F|Y|W|H|K|R|Q|N|E|D|S|T|random] is missing. Cannot continue.\n") sys.exit(-1) else: sys.stderr.write("Here expect FASTA sequence type as [dna|protein] instead of -"+ argv_rest[-1] +"- Cannot continue.\n") sys.exit(-1) if len(argv_rest)>3:sys.stderr.write("Too many arguments\n");sys.exit(-1) seq_source = argv_rest[1] nuc_con = argv_rest[2] if kernelname == 'linear' or kernelname == 'gauss' or kernelname == 'poly': if len(argv_rest)<1:sys.stderr.write("outputfile misssing\n");sys.exit(-1) if len(argv_rest)>1:sys.stderr.write("Too many arguments\n");sys.exit(-1) outfilename = argv_rest[0] utils.check_params(kparam, C, len(examples[0])) # run cross-validation (all_outputs, all_split) = crossvalidation(cv, kernelname, kparam, C, examples, labels, seq_source, nuc_con) try: f = open(outfilename, 'w+') except: sys.stderr.write('Fails to open the outputfile at ' + outfilename + ' Cannot continue.\n') sys.exit(-1) res_str = '#example\toutput\tsplit\n' f.write(res_str) for ix in xrange(len(all_outputs)): res_str = '%d\t%2.7f\t%d\n' % (ix,all_outputs[ix],all_split[ix]) f.write(res_str) f.close()
def main(config_file,mode,distributed): config = check_params(config_file) if mode in ["Train","train"]: train_dataset = Dataset(config["train_params"]["input_path"],config["train_params"]["imsize"]) if distributed: import horovod as hvd hvd.init() if hvd.rank()==0: writer = setup_tensorboard(get_params(config["train_params"],"tensorboard_location","./summary/")) train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset, num_replicas=hvd.size(), rank=hvd.rank()) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config["train_params"]["batch_size"], sampler=train_sampler, shuffle=True) model = ConvLSTM(**config["model_params"]) optimizer = hvd.DistributedOptimizer(model.optimizer, named_parameters=model.named_parameters()) hvd.broadcast_parameters(model.state_dict(), root_rank=0) train_distributed(model,train_loader,optimizer,config,writer) else: writer = setup_tensorboard(get_params(config["train_params"], "tensorboard_location", "./summary/")) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config["train_params"]["batch_size"], shuffle = True) model = ConvLSTM(**config["model_params"]) train(model,train_loader,model.optimizer,config,writer) elif mode in ["infer","Infer"]: model = ConvLSTM(**config["model_params"]) model.load_state_dict(config["infer_params"]["model_save_path"]) output_file = open(config["infer_params"]["output_path"])
def set_swap_paid(user_id): # get sender user sender = Profiles.query.get(user_id) body = request.get_json() check_params(body, 'tournament_id', 'recipient_id') swap = Swaps.query.get(user_id, body['recipient_id'], body['tournament_id']) swap.paid = True db.session.commit() return jsonify({'message': 'Swap has been paid'})
def update_buy_in(id): body = request.get_json() check_params(body) user_id = int(get_jwt()['sub']) buyin = Buy_ins.query.get(id) update_table(buyin, body, ignore=['user_id', 'flight_id', 'receipt_img_url']) db.session.commit() return jsonify(Buy_ins.query.get(id).serialize())
def update_buy_in(user_id, id): req = request.get_json() utils.check_params(req) buyin = Buy_ins.query.get(id) if buyin is None: raise APIException('Buy_in not found', 404) if buyin.status._value_ == 'busted': raise APIException('This buyin has a status of "busted"') if request.args.get('validate') == 'true': if buyin.status._value_ == 'pending': utils.check_params(req, 'chips', 'table', 'seat') buyin.status = 'active' # send_email(template='buyin_receipt', emails=buyin.user.user.email, # data={ # 'receipt_url': buyin.receipt_img_url, # 'tournament_date': buyin.flight.tournament.start_at, # 'tournament_name': buyin.flight.tournament.name # }) elif buyin.status._value_ == 'active': raise APIException('Buy in already validated') elif buyin.status._value_ == 'pending': raise APIException('This buyin has not been validated', 406) # Update status if req.get('status') == 'busted': buyin.status = 'busted' # Update chips, table and seat if req.get('chips') is not None: if req['chips'] > 999999999: raise APIException('Too many characters for chips') buyin.chips = req['chips'] if req.get('table') is not None: if len(req['table']) > 20: raise APIException('Too many characters for table') buyin.table = req['table'] if req.get('seat') is not None: buyin.seat = req['seat'] db.session.commit() return jsonify({'buy_in': buyin.serialize()})
def create_chat(user_id): req = request.get_json() utils.check_params(req, 'user2_id', 'tournament_id') chat = Chats.get(user_id, req['user2_id'], req['tournament_id']) if chat is not None: raise APIException('Chat already exists with id ' + str(chat.id), 400) chat = Chats(user1_id=user_id, user2_id=req['user2_id'], tournament_id=req['tournament_id']) db.session.add(chat) db.session.commit() return jsonify(chat.serialize())
def create_swap(): id = int(get_jwt()['sub']) # get sender user sender = Profiles.query.get(id) if not sender: raise APIException('User not found', 404) body = request.get_json() check_params(body, 'tournament_id', 'recipient_id', 'percentage') # get recipient user recipient = Profiles.query.get(body['recipient_id']) if not recipient: raise APIException('Recipient user not found', 404) if Swaps.query.get((id, body['recipient_id'], body['tournament_id'])): raise APIException('Swap already exists, can not duplicate', 400) sender_availability = sender.available_percentage(body['tournament_id']) if body['percentage'] > sender_availability: raise APIException(( 'Swap percentage too large. You can not exceed 50% per tournament. ' f'You have available: {sender_availability}%'), 400) recipient_availability = recipient.available_percentage( body['tournament_id']) if body['percentage'] > recipient_availability: raise APIException( ('Swap percentage too large for recipient. ' f'He has available to swap: {recipient_availability}%'), 400) db.session.add( Swaps(sender_id=id, tournament_id=body['tournament_id'], recipient_id=body['recipient_id'], percentage=body['percentage'])) db.session.add( Swaps(sender_id=body['recipient_id'], tournament_id=body['tournament_id'], recipient_id=id, percentage=body['percentage'])) db.session.commit() return jsonify({'message': 'ok'}), 200
def svm_poim(argv): """A top level script to parse input parameters and plot poims""" assert(argv[1]=='poim') if len(argv)<7:sys.stderr.write("usage: %s poim C poimdegree wd [kernelparameters] [arff|fasta] inputfiles poim.png [dna|protein] non(nucleotide|amino)converter\n" % argv[0]);sys.exit(-1) # parse input parameters C = float(argv[2]) poimdegree = int(argv[3]) (kernelname,kparam,argv_rest) = parse.parse_kernel_param(argv[4:], False) (examples,labels,argv_rest) = parse.parse_input_file_train(kernelname, argv_rest) if len(argv_rest)<1:sys.stderr.write("poim.png [dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<2:sys.stderr.write("[dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<3: if argv_rest[-1] == 'dna': sys.stderr.write("non-nucleotide converter like [A|T|C|G|R|Y|N] is missing. Cannot continue.\n") sys.exit(-1) elif argv_rest[-1] == 'protein': sys.stderr.write("non-amino acid converter like [G|P|A|V|L|I|M|C|F|Y|W|H|K|R|Q|N|E|D|S|T|random] is missing. Cannot continue.\n") sys.exit(-1) else: sys.stderr.write("Here expect FASTA sequence type as [dna|protein] instead of -"+ argv_rest[-1] +"- Cannot continue.\n") sys.exit(-1) if len(argv_rest)>3:sys.stderr.write("Too many arguments\n");sys.exit(-1) poimfilename = argv_rest[0] seq_source = argv_rest[1] nuc_con = argv_rest[2] utils.check_params(kparam, C, len(examples[0])) # train svm and compute POIMs (svm, kernel, feats_train, preproc) = train(examples,labels,C,kernelname,kparam,seq_source,nuc_con) print "done with training " (poim, max_poim, diff_poim, poim_totalmass) = compute_poims(svm, kernel, poimdegree, len(examples[0])) # plot poims plots.plot_poims(poimfilename, poim, max_poim, diff_poim, poim_totalmass, poimdegree, len(examples[0]))
def svm_modelsel(argv): """A top level script to parse input parameters and run model selection""" assert(argv[1]=='modelsel') if len(argv)<5:sys.stderr.write("usage: %s modelsel repeat Cs kernelname [kernelparameters] [arff|fasta] inputfiles outputfile [dna|protein] non(nucleotide|amino)converter\n" % argv[0]);sys.exit(-1) # parse input parameters cv = int(argv[2]) Cs = parse.parse_float_list(argv[3]) (kernelname,kparam,argv_rest) = parse.parse_kernel_param(argv[4:], True) (examples,labels,argv_rest) = parse.parse_input_file_train(kernelname, argv_rest) (seq_source, nuc_con) = ('', '') if kernelname == 'spec' or kernelname == 'wd': if len(argv_rest)<1:sys.stderr.write("outputfile [dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<2:sys.stderr.write("[dna|protein] non(nucleotide|amino)converter are missing\n");sys.exit(-1) if len(argv_rest)<3: if argv_rest[-1] == 'dna': sys.stderr.write("non-nucleotide converter like [A|T|C|G|R|Y|N] is missing. Cannot continue.\n") sys.exit(-1) elif argv_rest[-1] == 'protein': sys.stderr.write("non-amino acid converter like [G|P|A|V|L|I|M|C|F|Y|W|H|K|R|Q|N|E|D|S|T|random] is missing. Cannot continue.\n") sys.exit(-1) else: sys.stderr.write("Here expect FASTA sequence type as [dna|protein] instead of -"+ argv_rest[-1] +"- Cannot continue.\n") sys.exit(-1) if len(argv_rest)>3:sys.stderr.write("Too many arguments\n");sys.exit(-1) seq_source = argv_rest[1] nuc_con = argv_rest[2] if kernelname == 'linear' or kernelname == 'gauss' or kernelname== 'poly': if len(argv_rest)<1:sys.stderr.write("outputfile missing\n");sys.exit(-1) if len(argv_rest)>1:sys.stderr.write("Too many arguments\n");sys.exit(-1) outfilename = argv_rest[0] # run cross-validation mean_rocs=[] ; mean_prcs=[] ; mean_accs=[] ; all_Cs = [] ; all_kparam=[] ; if kparam["modelsel_name"]==None: for C in Cs: utils.check_params(kparam, C, len(examples[0])) (all_outputs, all_split) = crossvalidation(cv, kernelname, kparam, C, examples, labels, seq_source, nuc_con) (res_str, mean_roc, mean_prc, mean_acc) = evaluate(all_outputs, all_split, labels) mean_rocs.append(mean_roc) mean_prcs.append(mean_prc) mean_accs.append(mean_acc) all_Cs.append(C) all_kparam.append(None) else: # also optimize one kernel parameter for C in Cs: for kp in kparam["modelsel_params"]: kparam[kparam["modelsel_name"]] = kp utils.check_params(kparam, C, len(examples[0])) (all_outputs, all_split) = crossvalidation(cv, kernelname, kparam, C, examples, labels, seq_source, nuc_con) (res_str, mean_roc, mean_prc, mean_acc) = evaluate(all_outputs, all_split, labels) mean_rocs.append(mean_roc) mean_prcs.append(mean_prc) mean_accs.append(mean_acc) all_Cs.append(C) all_kparam.append(kp) max_roc=numpy.max(numpy.array(mean_rocs)) max_prc=numpy.max(numpy.array(mean_prcs)) max_acc=numpy.max(numpy.array(mean_accs)) try: f = open(outfilename, 'w+') except: sys.stderr.write('Fails to open the outputfile at ' + outfilename + ' Cannot continue.\n') sys.exit(-1) if kparam["modelsel_name"]==None or len(kparam["modelsel_params"])==1: detail_str = "\tC\tROC\tPRC\tAccuracy (at threshold 0)\n" else: detail_str = "\tC\t%s\tROC\tPRC\tAccuracy (at threshold 0)\n" % kparam["modelsel_name"] best_roc_str='' best_prc_str='' best_acc_str='' for i in xrange(len(all_Cs)): # determine the best parameter combinations if mean_rocs[i]==max_roc: rocsym='+' best_roc_str+=model2str(kparam, all_Cs[i], all_kparam[i])+'\n' else: rocsym=' ' if mean_prcs[i]==max_prc: prcsym='+' best_prc_str+=model2str(kparam, all_Cs[i], all_kparam[i])+'\n' else: prcsym=' ' if mean_accs[i]==max_acc: accsym='+' best_acc_str+=model2str(kparam, all_Cs[i], all_kparam[i])+'\n' else: accsym=' ' detail_str+=model2str(kparam, all_Cs[i], all_kparam[i], False)+'\t' if kparam["modelsel_name"]==None or len(kparam["modelsel_params"])==1: detail_str += '%c%2.1f%%\t%c%2.1f%%\t%c%2.1f%%\n' % (rocsym, 100*mean_rocs[i], prcsym, 100*mean_prcs[i], accsym, 100*mean_accs[i]) else: detail_str += '%c%2.1f%%\t%c%2.1f%%\t%c%2.1f%%\n' % (rocsym, 100*mean_rocs[i], prcsym, 100*mean_prcs[i], accsym, 100*mean_accs[i]) f.write('Best model(s) according to ROC measure:\n%s' % best_roc_str) f.write('\nBest model(s) according to PRC measure:\n%s' % best_prc_str) f.write('\nBest model(s) according to accuracy measure:\n%s' % best_acc_str) f.write('\nDetailed results:\n') f.write(detail_str) f.close()