How to use filtered_by method in Selene

Best Python code snippet using selene_python

test_scores.py

Source:test_scores.py Github

copy

Full Screen

...24 assert scores.length() == 1249025@pytest.mark.unit26@pytest.mark.parametrize("scores", scores_approaches_test)27def test_should_load_scores_filter_sex(scores):28 assert len(scores.filtered_by(Filter(sex=Sex.MALE))) == 1710 # 559129 assert len(scores.filtered_by(Filter(sex=Sex.FEMALE))) == 669 # 193830@pytest.mark.unit31@pytest.mark.parametrize("scores", scores_approaches_test)32def test_should_load_scores_filter_age(scores):33 assert len(scores.filtered_by(Filter(age=Age.YOUNG))) == 221434 assert len(scores.filtered_by(Filter(age=Age.ADULT))) == 5291 # 515335 assert len(scores.filtered_by(Filter(age=Age.SENIOR))) == 6336@pytest.mark.unit37@pytest.mark.parametrize("scores", scores_approaches_test)38def test_should_load_scores_filter_skin_tone(scores):39 assert len(scores.filtered_by(Filter(skin_tone=SkinTone.LIGHT_PINK))) == 25640 assert len(scores.filtered_by(Filter(skin_tone=SkinTone.MEDIUM_PINK_BROWN))) == 212141 assert len(scores.filtered_by(Filter(skin_tone=SkinTone.LIGHT_YELLOW))) == 78642 assert (43 len(scores.filtered_by(Filter(skin_tone=SkinTone.MEDIUM_YELLOW_BROWN))) == 378244 )45 assert len(scores.filtered_by(Filter(skin_tone=SkinTone.MEDIUM_DARK_BROWN))) == 40246 assert len(scores.filtered_by(Filter(skin_tone=SkinTone.DARK_BROWN))) == 22247@pytest.mark.unit48@pytest.mark.parametrize("scores", scores_approaches_test)49def test_should_load_quality_rbf_scores_filter_dataset(scores):50 assert len(scores.filtered_by(Filter(dataset=Dataset.CASIA_FASD))) == 36051 assert len(scores.filtered_by(Filter(dataset=Dataset.THREEDMAD))) == 7352 assert len(scores.filtered_by(Filter(dataset=Dataset.UVAD))) == 485653 assert len(scores.filtered_by(Filter(dataset=Dataset.SIW_M))) == 45654 assert len(scores.filtered_by(Filter(dataset=Dataset.SIW))) == 204255 assert len(scores.filtered_by(Filter(dataset=Dataset.ROSE_YOUTU))) == 174056 assert len(scores.filtered_by(Filter(dataset=Dataset.REPLAY_MOBILE))) == 30157 assert len(scores.filtered_by(Filter(dataset=Dataset.REPLAY_ATTACK))) == 47558 assert len(scores.filtered_by(Filter(dataset=Dataset.OULU_NPU))) == 179859 assert len(scores.filtered_by(Filter(dataset=Dataset.MSU_MFSD))) == 11860 assert len(scores.filtered_by(Filter(dataset=Dataset.HKBU_V2))) == 16861 assert len(scores.filtered_by(Filter(dataset=Dataset.HKBU))) == 3062 assert len(scores.filtered_by(Filter(dataset=Dataset.CSMAD))) == 7363@pytest.mark.unit64@pytest.mark.parametrize("scores", scores_approaches_test)65def test_should_load_scores_filter_dataset_and_sex(scores):66 assert (67 len(scores.filtered_by(Filter(sex=Sex.MALE, dataset=Dataset.CASIA_FASD))) == 30068 )69 assert (70 len(scores.filtered_by(Filter(sex=Sex.FEMALE, dataset=Dataset.CASIA_FASD)))71 == 6072 )73@pytest.mark.unit74@pytest.mark.parametrize("scores", scores_approaches_test)75def test_should_load_scores_with_fair_sex_subset(scores):76 fair_sex_subset = scores.get_fair_sex_subset()77 for gender in Sex.options():78 assert len(fair_sex_subset.get(gender.name)) == 137 # 581 # 188779@pytest.mark.unit80@pytest.mark.parametrize("scores", scores_approaches_test)81def test_should_load_scores_with_fair_age_subset(scores):82 fair_age_subset = scores.get_fair_age_subset()83 for age in Age.options():84 assert len(fair_age_subset.get(age.name)) == 42 # 6385@pytest.mark.unit86@pytest.mark.parametrize("scores", scores_approaches_test)87def test_should_load_scores_with_fair_skin_tone_subset(scores):88 fair_skin_tone_subset = scores.get_fair_skin_tone_subset()89 for skin_tone in SkinTone.options():90 assert len(fair_skin_tone_subset.get(skin_tone.name)) == 34 # 11391@pytest.mark.unit92@pytest.mark.parametrize("scores", scores_approaches_test)93def test_should_load_scores_filter_with_random_values(scores):94 assert len(scores.filtered_by(Filter(random_values=10))) == 1095@pytest.mark.unit96@pytest.mark.parametrize("scores", scores_approaches_test)97def test_should_load_scores_filter_with_pseudo_random_values(scores):98 assert len(scores.filtered_by(Filter(pseudo_random_values=10))) == 1099@pytest.mark.unit100@pytest.mark.parametrize("scores", scores_approaches_test)101def test_should_load_scores_get_genuine(scores):102 assert len(scores.get_genuine()) == 2281103@pytest.mark.unit104@pytest.mark.parametrize("scores", scores_approaches_test)105def test_should_load_scores_get_attacks(scores):106 assert len(scores.get_attacks()) == 10209107@pytest.mark.unit108@pytest.mark.parametrize("scores", scores_approaches_test)109def test_should_load_scores_get_numpy_scores_and_labels_filtered_by_labels(scores):110 scores, labels = scores.get_numpy_scores_and_labels_filtered_by_labels()111 assert len(scores) == 12490112 assert len(labels) == 12490

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

1from __future__ import division2import pickle3import io4import os5from collections import Counter, defaultdict6from magpie.base.document import Document7def save_to_disk(path_to_disk, obj, overwrite=False):8 """ Pickle an object to disk """9 dirname = os.path.dirname(path_to_disk)10 if not os.path.exists(dirname):11 raise ValueError("Path " + str(dirname) + " does not exist")12 if not overwrite and os.path.exists(path_to_disk):13 raise ValueError("File " + str(path_to_disk) + "already exists")14 pickle.dump(obj, open(path_to_disk, 'wb'))15def load_from_disk(path_to_disk):16 """ Load a pickle from disk to memory """17 if not os.path.exists(path_to_disk):18 raise ValueError("File " + path_to_disk + " does not exist")19 return pickle.load(open(path_to_disk, 'rb'))20def get_all_answers(data_dir, filtered_by=None):21 """22 Extract ground truth answers from *.lab files in a given directory23 :param data_dir: path to the directory with .lab files24 :param filtered_by: whether to filter the answers.25 :return: dictionary of the form e.g. {'101231': set('lab1', 'lab2') etc.}26 """27 answers = dict()28 files = {filename[:-4] for filename in os.listdir(data_dir)}29 for f in files:30 answers[f] = get_answers_for_doc(f + '.txt',31 data_dir,32 filtered_by=filtered_by)33 return answers34def get_answers_for_doc(doc_name, data_dir, filtered_by=None):35 """36 Read ground_truth answers from a .lab file corresponding to the doc_name37 :param doc_name: the name of the document, should end with .txt38 :param data_dir: directory in which the documents and answer files are39 :param filtered_by: whether to filter the answers.40 :return: set of unicodes containing answers for this particular document41 """42 filename = os.path.join(data_dir, doc_name[:-4] + '.lab')43 if not os.path.exists(filename):44 raise ValueError("Answer file " + filename + " does not exist")45 with io.open(filename, 'r') as f:46 answers = {line.rstrip('\n') for line in f}47 if filtered_by:48 answers = {kw for kw in answers if kw in filtered_by}49 return answers50def calculate_label_distribution(data_dir, filtered_by=None):51 """52 Calculate the distribution of labels in a directory. Function can be used53 to find the most frequent and not used labels, so that the target54 vocabulary can be trimmed accordingly.55 :param data_dir: directory path with the .lab files56 :param filtered_by: a set of labels that defines the vocabulary57 :return: list of KV pairs of the form (14, ['lab1', 'lab2']), which means58 that both lab1 and lab2 were labels in 14 documents59 """60 answers = [61 kw for v in get_all_answers(62 data_dir,63 filtered_by=filtered_by).values() for kw in v]64 counts = Counter(answers)65 histogram = defaultdict(list)66 for kw, cnt in counts.items():67 histogram[cnt].append(kw)68 return histogram69def calculate_number_of_labels_distribution(data_dir, filtered_by=None):70 """ Look how many papers are there with 3 labels, 4 labels etc.71 Return a histogram. """72 answers = get_all_answers(data_dir, filtered_by=filtered_by).values()73 lengths = [len(ans_set) for ans_set in answers]...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful