Best Python code snippet using toolium_python
test_analysis.py
Source:test_analysis.py
...5from nlweb.image_utils import (6 download_images, download_image, resample_images7)8from .nv_test_data import TARGET_DATA_IMG_IDS9def compare_image_files(img_a, img_b):10 assert img_a.get_data().shape == img_b.get_data().shape11 assert (img_a.get_data() == img_b.get_data()).all()12def test_train_model(tmpdir):13 algorithm = 'ridge'14 output_dir = str(tmpdir)15 cache = FileCache('cache')16 client = HTTPClient(cache)17 image_list = download_images(client, TARGET_DATA_IMG_IDS,18 output_dir)19 image_list = resample_images(cache, image_list, output_dir)20 cv = {'type': 'kfolds', 'n_folds': 10}21 # cv = {'type': 'loso'}22 result = analysis.train_model(image_list, algorithm, cv, output_dir)23 filename = '%s_weightmap.nii.gz' % algorithm24 assert result['weightmap'] == filename25 sample_img = nib.load(os.path.join(os.path.dirname(__file__), filename))26 result_img = nib.load(os.path.join(output_dir, filename))27 compare_image_files(sample_img, result_img)28def test_masked_train_model(tmpdir):29 algorithm = 'ridge'30 mask_image_id = 1865031 output_dir = str(tmpdir)32 cache = FileCache('cache')33 client = HTTPClient(cache)34 image_list = download_images(client, TARGET_DATA_IMG_IDS,35 output_dir)36 cv = {'type': 'kfolds', 'n_folds': 10}37 mask_filepath = download_image(client, mask_image_id, output_dir)38 result = analysis.train_model(39 image_list, algorithm, cv, output_dir,40 file_path_key='original_file',41 mask=mask_filepath)42 filename = '%s_weightmap.nii.gz' % algorithm43 assert result['weightmap'] == filename44 sample_img = nib.load(os.path.join(os.path.dirname(__file__), filename))45 result_img = nib.load(os.path.join(output_dir, filename))46 compare_image_files(sample_img, result_img)47def test_model_test(tmpdir):48 cache = FileCache('cache')49 client = HTTPClient(cache)50 output_dir = str(tmpdir)51 weight_map_filename = os.path.join(os.path.dirname(__file__),52 'ridge_weightmap.nii.gz')53 image_list = download_images(client, [TARGET_DATA_IMG_IDS[0],54 TARGET_DATA_IMG_IDS[30],55 TARGET_DATA_IMG_IDS[62]],56 output_dir)57 # Add dummy name and thumbnail58 image_list = [dict(thumbnail='image.png',59 name='name %s' % item['id'],60 **item) for item in image_list]...
ImageCompareTest.py
Source:ImageCompareTest.py
...54 im2 = Image.new('RGB', (256, 256), 'black')55 im1.save(self.imfile1)56 im2.save(self.imfile2)57 img_comp = ImageTools()58 img_diff = img_comp.compare_image_files(self.imfile1, self.imfile2)59 self.assertEqual(img_diff, 100.0)60 61 def test_imagefiles_calling_histogram_algo(self):62 im1 = Image.new('RGB', (256, 256), 'white')63 im2 = Image.new('RGB', (256, 256), 'black')64 im1.save(self.imfile1)65 im2.save(self.imfile2)66 img_comp = ImageTools()67 img_diff = img_comp.compare_image_files(self.imfile1, self.imfile2, algorithm='histogram')68 self.assertGreater(img_diff, 100.0)69 70 def test_imagefiles_calling_unknown_algo_returns_error(self):71 im1 = Image.new('RGB', (256, 256), 'white')72 im2 = Image.new('RGB', (256, 256), 'black')73 im1.save(self.imfile1)74 im2.save(self.imfile2)75 img_comp = ImageTools()76 img_diff = img_comp.compare_image_files(self.imfile1, self.imfile2, algorithm='unknown')77 self.assertEqual(img_diff, 'Unsupported algorithm')78 def test_nonexist_imagefiles_return_error(self):79 im1 = Image.new('RGB', (256, 256), 'white')80 im1.save(self.imfile1)81 img_comp = ImageTools()82 img_diff = img_comp.compare_image_files(self.imfile1, 'notreal.jpg')83 self.assertEqual(img_diff, 'Error reading one or more files')84if __name__ == '__main__':...
compare_image.py
Source:compare_image.py
...14 def __init__(self):15 pass16 17 18 def compare_image_files(self, file1, file2):19 img1 = imread(file1)20 img2 = imread(file2)21 return self.compare_images(img1, img2)22 def compare_images(self, img1, img2):23 # normalize to compensate for exposure difference, this may be unnecessary24 # consider disabling it25 img1 = self.normalize(img1)26 img2 = self.normalize(img2)27 # calculate the difference and its norms28 diff = img1 - img2 # elementwise for scipy arrays29 m_norm = np.sum(abs(diff))/img1.size # Manhattan norm30 z_norm = norm(diff.ravel(), 0)/img1.size # Zero norm31 # print ("Manhattan norm:", m_norm, "/ per pixel:", m_norm/img1.size)32 # print ("Zero norm:", z_norm, "/ per pixel:", z_norm*1.0/img1.size)33 34 return (m_norm, z_norm)35 def to_grayscale(self, arr):36 "If arr is a color image (3D array), convert it to grayscale (2D array)."37 if len(arr.shape) == 3:38 return average(arr, -1) # average over the last axis (color channels)39 else:40 return arr41 42 def normalize(self, arr):43 rng = arr.max()-arr.min()44 amin = arr.min()45 return (arr-amin)*255/rng46def main():47 img1 = imread("live0.png")48 img2 = imread("live200.png")49 img = imagecompare()50 n_m, n_0 = img.compare_image_files("live0.png", "live200.png")51 print ("Manhattan norm:", n_m, "/ per pixel:", n_m/img1.size)52 print ("Zero norm:", n_0, "/ per pixel:", n_0*1.0/img1.size)53 54if __name__ == '__main__':...
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!!