Best Python code snippet using autotest_python
test_args_handler_predict.py
Source:test_args_handler_predict.py
1import unittest2from unittest.mock import patch3import os4import pickle5from io import StringIO6from argparse import ArgumentParser7from tests.capice.test_templates import _project_root_directory8from molgenis.capice.cli.args_handler_predict import ArgsHandlerPredict9class TestArgsHandlerPredict(unittest.TestCase):10 model_path = os.path.join(_project_root_directory,11 'tests',12 'resources',13 'xgb_booster_poc.pickle.dat')14 def setUp(self):15 with open(self.model_path, 'rb') as model_file:16 self.model = pickle.load(model_file)17 @patch('sys.stderr', new_callable=StringIO)18 @patch.object(pickle, 'load')19 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')20 def test_model_semantic_invalid_version(self, pickle_load, stderr):21 """22 Tests invalid semantic version that contains a '-' without pre-release text behind it23 (no empty identifier allowed).24 See also: https://semver.org/#spec-item-925 """26 setattr(self.model, 'CAPICE_version', '1.0.0-')27 pickle_load.return_value = self.model28 args_handler = ArgsHandlerPredict(ArgumentParser())29 with self.assertRaises(SystemExit) as cm:30 args_handler.validate_model(self.model_path)31 self.assertEqual(cm.exception.code, 2)32 self.assertIn('Model version does not adhere to correct format: 1.0.0-',33 stderr.getvalue())34 @patch('sys.stderr', new_callable=StringIO)35 @patch.object(pickle, 'load')36 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')37 def test_model_pep440_invalid_prerelease_name(self, pickle_load, stderr):38 """39 Tests invalid PEP version as pre-release format is very strict (a/b/rc<int> only).40 See also: https://peps.python.org/pep-0440/#pre-releases41 """42 setattr(self.model, 'CAPICE_version', '1.0.0pre1')43 pickle_load.return_value = self.model44 args_handler = ArgsHandlerPredict(ArgumentParser())45 with self.assertRaises(SystemExit) as cm:46 args_handler.validate_model(self.model_path)47 self.assertEqual(cm.exception.code, 2)48 self.assertIn('Model version does not adhere to correct format: 1.0.0pre1',49 stderr.getvalue())50 @patch('sys.stderr', new_callable=StringIO)51 @patch.object(pickle, 'load')52 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')53 def test_model_major_mismatch(self, pickle_load, stderr):54 """55 Tests major version mismatch between CAPICE & model (should exit).56 """57 setattr(self.model, 'CAPICE_version', '2.0.0')58 pickle_load.return_value = self.model59 args_handler = ArgsHandlerPredict(ArgumentParser())60 with self.assertRaises(SystemExit) as cm:61 args_handler.validate_model(self.model_path)62 self.assertEqual(cm.exception.code, 2)63 self.assertIn('CAPICE major version 1.0.0 does not match with the model 2.0.0!',64 stderr.getvalue())65 @patch.object(pickle, 'load')66 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')67 def test_model_minor_mismatch(self, pickle_load):68 """69 Tests minor version mismatch between CAPICE & model (should not exit).70 """71 setattr(self.model, 'CAPICE_version', '1.2.0')72 pickle_load.return_value = self.model73 args_handler = ArgsHandlerPredict(ArgumentParser())74 args_handler.validate_model(self.model_path)75 @patch.object(pickle, 'load')76 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')77 def test_model_patch_mismatch(self, pickle_load):78 """79 Tests patch version mismatch between CAPICE & model (should not exit).80 """81 setattr(self.model, 'CAPICE_version', '1.0.2')82 pickle_load.return_value = self.model83 args_handler = ArgsHandlerPredict(ArgumentParser())84 args_handler.validate_model(self.model_path)85 @patch('sys.stderr', new_callable=StringIO)86 @patch.object(pickle, 'load')87 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')88 def test_model_semantic_prerelease_mismatch(self, pickle_load, stderr):89 """90 Tests pre-release mismatch if rest of version is identical (should exit).91 """92 setattr(self.model, 'CAPICE_version', '1.0.0-rc2')93 pickle_load.return_value = self.model94 args_handler = ArgsHandlerPredict(ArgumentParser())95 with self.assertRaises(SystemExit) as cm:96 args_handler.validate_model(self.model_path)97 self.assertEqual(cm.exception.code, 2)98 self.assertIn('CAPICE prerelease version 1.0.0-rc1 does not match the model '99 'prerelease version 1.0.0-rc2 (should match for pre-releases)!',100 stderr.getvalue())101 @patch('sys.stderr', new_callable=StringIO)102 @patch.object(pickle, 'load')103 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')104 def test_model_semantic_prerelease_with_minor_mismatch(self, pickle_load, stderr):105 """106 Tests that for identical pre-release text but differing minor version, CAPICE exits.107 If 2 different pre-release versions within the same patch version should already fail108 due to instability between pre-release versions, it should not be possible to use a109 pre-release from a different major/minor/patch version either.110 """111 setattr(self.model, 'CAPICE_version', '1.2.0-rc1')112 pickle_load.return_value = self.model113 args_handler = ArgsHandlerPredict(ArgumentParser())114 with self.assertRaises(SystemExit) as cm:115 args_handler.validate_model(self.model_path)116 self.assertEqual(cm.exception.code, 2)117 self.assertIn('CAPICE minor version 1.0.0-rc1 does not match the model minor version '118 '1.2.0-rc1 (should match for pre-releases)!',119 stderr.getvalue())120 @patch('sys.stderr', new_callable=StringIO)121 @patch.object(pickle, 'load')122 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')123 def test_model_semantic_prerelease_with_patch_mismatch(self, pickle_load, stderr):124 """125 Tests that for identical pre-release text but differing patch version, CAPICE exits.126 If 2 different pre-release versions within the same patch version should already fail127 due to instability between pre-release versions, it should not be possible to use a128 pre-release from a different major/minor/patch version either.129 """130 setattr(self.model, 'CAPICE_version', '1.0.2-rc1')131 pickle_load.return_value = self.model132 args_handler = ArgsHandlerPredict(ArgumentParser())133 with self.assertRaises(SystemExit) as cm:134 args_handler.validate_model(self.model_path)135 self.assertEqual(cm.exception.code, 2)136 self.assertIn('CAPICE patch version 1.0.0-rc1 does not match the model patch version '137 '1.0.2-rc1 (should match for pre-releases)!',138 stderr.getvalue())139 @patch('sys.stderr', new_callable=StringIO)140 @patch.object(pickle, 'load')141 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0')142 def test_model_semantic_prerelease_missing_in_capice(self, pickle_load, stderr):143 """144 Tests if pre-release model (using semantic version formatting) in combination with final145 CAPICE version exits.146 If either CAPICE or model uses a pre-release version, both should contain pre-release text147 so that non-pre-release code/model is not used in combination with pre-release model/code148 due to instability of pre-releases.149 """150 setattr(self.model, 'CAPICE_version', '1.0.0-rc1')151 pickle_load.return_value = self.model152 args_handler = ArgsHandlerPredict(ArgumentParser())153 with self.assertRaises(SystemExit) as cm:154 args_handler.validate_model(self.model_path)155 self.assertEqual(cm.exception.code, 2)156 self.assertIn('CAPICE prerelease version 1.0.0 does not match the model prerelease '157 'version 1.0.0-rc1 (should match for pre-releases)!',158 stderr.getvalue())159 @patch('sys.stderr', new_callable=StringIO)160 @patch.object(pickle, 'load')161 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')162 def test_model_semantic_prerelease_missing_in_model(self, pickle_load, stderr):163 """164 Tests if pre-release CAPICE (using semantic version formatting) in combination with final165 model version exits.166 If either CAPICE or model uses a pre-release version, both should contain pre-release text167 so that non-pre-release code/model is not used in combination with pre-release model/code168 due to instability of pre-releases.169 """170 setattr(self.model, 'CAPICE_version', '1.0.0')171 pickle_load.return_value = self.model172 args_handler = ArgsHandlerPredict(ArgumentParser())173 with self.assertRaises(SystemExit) as cm:174 args_handler.validate_model(self.model_path)175 self.assertEqual(cm.exception.code, 2)176 self.assertIn('CAPICE prerelease version 1.0.0-rc1 does not match the model '177 'prerelease version 1.0.0 (should match for pre-releases)!',178 stderr.getvalue())179 @patch('sys.stderr', new_callable=StringIO)180 @patch.object(pickle, 'load')181 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0rc1')182 def test_model_pep440_prerelease_missing_in_model(self, pickle_load, stderr):183 """184 Tests if pre-release CAPICE (using PEP 440 version formatting) in combination with final185 model version exits.186 If either CAPICE or model uses a pre-release version, both should contain pre-release text187 so that non-pre-release code/model is not used in combination with pre-release model/code188 due to instability of pre-releases.189 """190 setattr(self.model, 'CAPICE_version', '1.0.0')191 pickle_load.return_value = self.model192 args_handler = ArgsHandlerPredict(ArgumentParser())193 with self.assertRaises(SystemExit) as cm:194 args_handler.validate_model(self.model_path)195 self.assertEqual(cm.exception.code, 2)196 self.assertIn('CAPICE prerelease version 1.0.0rc1 does not match the model '197 'prerelease version 1.0.0 (should match for pre-releases)!',198 stderr.getvalue())199 @patch.object(pickle, 'load')200 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0rc1')201 def test_model_pep440_prerelease(self, pickle_load):202 """203 Tests identical pre-release version using PEP 440 (should not exit).204 """205 setattr(self.model, 'CAPICE_version', '1.0.0rc1')206 pickle_load.return_value = self.model207 args_handler = ArgsHandlerPredict(ArgumentParser())208 args_handler.validate_model(self.model_path)209 @patch.object(pickle, 'load')210 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')211 def test_model_semantic_and_pep440_prerelease(self, pickle_load):212 """213 Tests identical pre-release version where formatting differs but portrays same version214 (should not exit).215 While the actual string differs, the portrayed version is identical and therefore should216 not fail.217 """218 setattr(self.model, 'CAPICE_version', '1.0.0rc1')219 pickle_load.return_value = self.model220 args_handler = ArgsHandlerPredict(ArgumentParser())221 args_handler.validate_model(self.model_path)222 @patch('sys.stderr', new_callable=StringIO)223 @patch.object(pickle, 'load')224 @patch('molgenis.capice.cli.args_handler_predict.__version__', '1.0.0-rc1')225 def test_model_semantic_and_pep440_prerelease_mismatch(self, pickle_load, stderr):226 """227 Tests mismatch in pre-release version when using differing formatting (CAPICE=semantic,228 model=PEP 440). Should exit.229 While the Strings differ, the comparison should be made based on an actual version number230 difference and therefore the error message is validated so that CAPICE is exited for the231 right reason.232 """233 setattr(self.model, 'CAPICE_version', '1.0.0rc2')234 pickle_load.return_value = self.model235 args_handler = ArgsHandlerPredict(ArgumentParser())236 with self.assertRaises(SystemExit) as cm:237 args_handler.validate_model(self.model_path)238 self.assertEqual(cm.exception.code, 2)239 self.assertIn('CAPICE prerelease version 1.0.0-rc1 does not match the model '240 'prerelease version 1.0.0rc2 (should match for pre-releases)!',241 stderr.getvalue())242if __name__ == '__main__':...
Main.py
Source:Main.py
...10#Branje ocen11# uid = UserItemData('data/user_ratedmovies.dat')12# print(uid.nratings())13# pickle_save(uid, "uid_pickle.pickle")14uid = pickle_load("uid_pickle.pickle")15print("Branje ocen, Å¡tevilo ocen (vsi podatki): ", uid.nratings())16# uid = UserItemData('data/user_ratedmovies.dat', from_date='12.1.2007', to_date='16.2.2008', min_ratings=100)17# print(uid.nratings())18# pickle_save(uid, "uid_min_ratings.pickle")19print("Branje ocen")20uid = pickle_load("uid_min_ratings.pickle")21print("Branje ocen, Å¡tevilo ocen (omejeni podatki): ", uid.nratings())22#Branje filmov23#md = MovieData('data/movies.dat')24#print(md.get_title(1))25#pickle_save(md, "movie_data.pickle")26print()27print("Branje filmov")28md = pickle_load("movie_data.pickle")29print("Film z id 1: ", md.get_title(1))30#NakljuÄni prediktor31print()32print("NakljuÄni prediktor")33uid = pickle_load("uid_pickle.pickle")34md = pickle_load("movie_data.pickle")35rp = RandomPredictor(1, 5)36rp.fit(uid)37predictions = rp.predict(78)38print(type(predictions))39movieIDs = [1, 3, 20, 50, 100]40for movieID in movieIDs:41 print("Film: {}, ocena: {}".format(md.get_title(movieID), predictions[movieID]))42#PriporoÄanje43print()44print("PriporoÄanje")45print("RandomPredictor")46predictor = RandomPredictor(1, 5)47rec = Recommender(predictor)48rec.fit(uid)49rec_items = rec.recommend(78, n=5, rec_seen=False)50for idmovie, val in rec_items:51 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))52print("AveragePredictor")53predictor = AveragePredictor(b=100)54rec = Recommender(predictor)55rec.fit(uid)56rec_items = rec.recommend(78, n=5, rec_seen=False)57for idmovie, val in rec_items:58 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))59print("ViewsPredictor")60predictor = ViewsPredictor()61rec = Recommender(predictor)62rec.fit(uid)63rec_items = rec.recommend(78, n=5, rec_seen=False)64for idmovie, val in rec_items:65 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))66#Napovedovanje ocen s podobnostjo med produkti67print()68print("Napovedovanje ocen s podobnostjo med produkti")69uid = pickle_load('uid_min_1000.pickle')70predictor = ItemBasedPredictor()71rec = Recommender(predictor)72rec.fit(uid)73print("Podobnost med filmoma 'Men in black'(1580) in 'Ghostbusters'(2716): ", predictor.similarity(1580, 2716))74print("Podobnost med filmoma 'Men in black'(1580) in 'Schindler's List'(527): ", predictor.similarity(1580, 527))75print("Podobnost med filmoma 'Men in black'(1580) in 'Independence day'(780): ", predictor.similarity(1580, 780))76print("Predictions for 78: ")77rec_items = rec.recommend(78, n=15, rec_seen=False)78for idmovie, val in rec_items:79 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))80#Najbolj podobni filmi81print()82print("Najbolj podobni filmi")83movie1_movie2_similarity = []84for movie1 in rec.predictor.all_movieIDs_set:85 for movie2 in rec.predictor.all_movieIDs_set:86 if movie1 != movie2:87 movie1_movie2_similarity.append((movie1, movie2, rec.predictor.similarity(movie1, movie2)))88movie1_movie2_similarity = sorted(movie1_movie2_similarity, key=lambda tuple: tuple[2], reverse=True)89i = 2090for movie1, movie2, similarity in movie1_movie2_similarity:91 print("Film1: ", md.get_title(movie1), "Film2: ", md.get_title(movie2), "podobnost: ", similarity)92 i -= 193 if i == 0:94 break95#PriporoÄanje glede na trenutno ogledano vsebino96print()97print("PriporoÄanje glede na trenutno ogledano vsebino")98rec_items = predictor.similarItems(4993, 10)99print('Filmi podobni "The Lord of the Rings: The Fellowship of the Ring": ')100for idmovie, val in rec_items:101 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))102#PriporoÄilo zase103print()104print("PriporoÄilo zase")105uid = pickle_load('uid_extended_min_1000.pickle')106predictor = ItemBasedPredictor()107rec = Recommender(predictor)108rec.fit(uid)109rec_items = rec.recommend(666666, n=10, rec_seen=False)110for idmovie, val in rec_items:111 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))112#Napovedovanje z metodo Slope one113print()114print("Napovedovanje z metodo Slope one")115uid = pickle_load('uid_min_1000.pickle')116predictor = SlopeOnePredictor()117rec = Recommender(predictor)118rec.fit(uid)119print("Predictions for 78: ")120rec_items = rec.recommend(78, n=15, rec_seen=False)121for idmovie, val in rec_items:122 print("Film: {}, ocena: {}".format(md.get_title(idmovie), val))123#Metoda evaluate124print()125print("Metoda evaluate")126uid = pickle_load('uid_min_1000_to_date_1-1-2008.pickle')127uid_test = pickle_load("uid_min_200_from_date_2-1-2008.pickle")128predictor = SlopeOnePredictor()129rec = Recommender(predictor)130rec.fit(uid)131rmse, mae, precision, recall, f = rec.evaluate(uid_test, 20)132print("SlopeOnePredictor - RMSE: ", rmse, " MAE: ", mae, " Precision: ", precision, " Recall: ", recall, " F: ", f)133predictor = ItemBasedPredictor()134rec = Recommender(predictor)135rec.fit(uid)136rmse, mae, precision, recall, f = rec.evaluate(uid_test, 20)...
small_talk_prediction.py
Source:small_talk_prediction.py
1# coding: utf-82# In[3]:3import pickle4import random5import numpy as np6with open('small_talk_model.pkl', 'rb') as pickle_load:7 tfidf1 = pickle.load(pickle_load)8 9with open('sparse_small_transformed.pkl', 'rb') as pickle_load:10 a1 = pickle.load(pickle_load)11 12with open('small_talk_answers.pkl', 'rb') as pickle_load:13 ans1 = pickle.load(pickle_load)14 15from sklearn.metrics.pairwise import cosine_similarity16exit_words = ['quit','end','bye','stop','exit']17def small_talk(sen):18 19 if sen in exit_words:20 return print("bot: bye bye")21 22 b1 = tfidf1.transform([sen])23 z1 = cosine_similarity(a1,b1)24 25 print("bot: ",ans1[random.choice([i for i in range(len(z1)) if z1[i] == z1[np.argmax(z1)][0]])])26 ...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!