Best Python code snippet using green
divide_and_compress_test.py
Source:divide_and_compress_test.py  
1#!/usr/bin/python2.42#3# Copyright (C) 2008 Google Inc.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9#      http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""Tests for divide_and_compress.py.18TODO(jmatt): Add tests for module methods.19"""20__author__ = 'jmatt@google.com (Justin Mattson)'21import os22import stat23import unittest24import zipfile25import divide_and_compress26import mox27class BagOfParts(object):28  """Just a generic class that I can use to assign random attributes to."""29  def NoOp(self):30    x = 131    32class ValidAndRemoveTests(unittest.TestCase):33  """Test the ArchiveIsValid and RemoveLastFile methods."""34  35  def setUp(self):36    """Prepare the test.37    Construct some mock objects for use with the tests.38    """39    self.my_mox = mox.Mox()40    file1 = BagOfParts()41    file1.filename = 'file1.txt'42    file1.contents = 'This is a test file'43    file2 = BagOfParts()44    file2.filename = 'file2.txt'45    file2.contents = ('akdjfk;djsf;kljdslkfjslkdfjlsfjkdvn;kn;2389rtu4i'46                      'tn;ghf8:89H*hp748FJw80fu9WJFpwf39pujens;fihkhjfk'47                      'sdjfljkgsc n;iself')48    self.files = {'file1': file1, 'file2': file2}49  def tearDown(self):50    """Remove any stubs we've created."""51    self.my_mox.UnsetStubs()52  def testArchiveIsValid(self):53    """Test the DirectoryZipper.ArchiveIsValid method.54    Run two tests, one that we expect to pass and one that we expect to fail55    """56    test_file_size = 105673057    self.my_mox.StubOutWithMock(os, 'stat')58    os.stat('/foo/0.zip').AndReturn([test_file_size])59    self.my_mox.StubOutWithMock(stat, 'ST_SIZE')60    stat.ST_SIZE = 061    os.stat('/baz/0.zip').AndReturn([test_file_size])62    mox.Replay(os.stat)63    test_target = divide_and_compress.DirectoryZipper('/foo/', 'bar', 64                                                      test_file_size - 1, True)65    self.assertEqual(False, test_target.ArchiveIsValid(),66                     msg=('ERROR: Test failed, ArchiveIsValid should have '67                          'returned false, but returned true'))68    test_target = divide_and_compress.DirectoryZipper('/baz/', 'bar',69                                                      test_file_size + 1, True)70    self.assertEqual(True, test_target.ArchiveIsValid(),71                     msg=('ERROR: Test failed, ArchiveIsValid should have'72                          ' returned true, but returned false'))73  def testRemoveLastFile(self):74    """Test DirectoryZipper.RemoveLastFile method.75    Construct a ZipInfo mock object with two records, verify that write is76    only called once on the new ZipFile object.77    """78    source = self.CreateZipSource()79    dest = self.CreateZipDestination()80    source_path = ''.join([os.getcwd(), '/0-old.zip'])81    dest_path = ''.join([os.getcwd(), '/0.zip'])82    test_target = divide_and_compress.DirectoryZipper(83        ''.join([os.getcwd(), '/']), 'dummy', 1024*1024, True)84    self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath')85    test_target.OpenZipFileAtPath(source_path, mode='r').AndReturn(source)86    test_target.OpenZipFileAtPath(dest_path,87                                  compress=zipfile.ZIP_DEFLATED,88                                  mode='w').AndReturn(dest)89    self.my_mox.StubOutWithMock(os, 'rename')90    os.rename(dest_path, source_path)91    self.my_mox.StubOutWithMock(os, 'unlink')92    os.unlink(source_path)93    94    self.my_mox.ReplayAll()95    test_target.RemoveLastFile()96    self.my_mox.VerifyAll()    97  def CreateZipSource(self):98    """Create a mock zip sourec object.99    Read should only be called once, because the second file is the one100    being removed.101    Returns:102      A configured mocked103    """104    105    source_zip = self.my_mox.CreateMock(zipfile.ZipFile)106    source_zip.infolist().AndReturn([self.files['file1'], self.files['file1']])107    source_zip.infolist().AndReturn([self.files['file1'], self.files['file1']])108    source_zip.read(self.files['file1'].filename).AndReturn(109        self.files['file1'].contents)110    source_zip.close()111    return source_zip112  def CreateZipDestination(self):113    """Create mock destination zip.114    Write should only be called once, because there are two files in the115    source zip and we expect the second to be removed.116    Returns:117      A configured mocked118    """119    120    dest_zip = mox.MockObject(zipfile.ZipFile)121    dest_zip.writestr(self.files['file1'].filename,122                      self.files['file1'].contents)123    dest_zip.close()124    return dest_zip125class FixArchiveTests(unittest.TestCase):126  """Tests for the DirectoryZipper.FixArchive method."""127  128  def setUp(self):129    """Create a mock file object."""130    self.my_mox = mox.Mox()131    self.file1 = BagOfParts()132    self.file1.filename = 'file1.txt'133    self.file1.contents = 'This is a test file'134  def tearDown(self):135    """Unset any mocks that we've created."""136    self.my_mox.UnsetStubs()137  def _InitMultiFileData(self):138    """Create an array of mock file objects.139    Create three mock file objects that we can use for testing.140    """141    self.multi_file_dir = []142    143    file1 = BagOfParts()144    file1.filename = 'file1.txt'145    file1.contents = 'kjaskl;jkdjfkja;kjsnbvjnvnbuewklriujalvjsd'146    self.multi_file_dir.append(file1)147    file2 = BagOfParts()148    file2.filename = 'file2.txt'149    file2.contents = ('He entered the room and there in the center, it was.'150                      ' Looking upon the thing, suddenly he could not remember'151                      ' whether he had actually seen it before or whether'152                      ' his memory of it was merely the effect of something'153                      ' so often being imagined that it had long since become '154                      ' manifest in his mind.')155    self.multi_file_dir.append(file2)156    file3 = BagOfParts()157    file3.filename = 'file3.txt'158    file3.contents = 'Whoa, what is \'file2.txt\' all about?'159    self.multi_file_dir.append(file3)160    161  def testSingleFileArchive(self):162    """Test behavior of FixArchive when the archive has a single member.163    We expect that when this method is called with an archive that has a164    single member that it will return False and unlink the archive.165    """166    test_target = divide_and_compress.DirectoryZipper(167        ''.join([os.getcwd(), '/']), 'dummy', 1024*1024, True)168    self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath')169    test_target.OpenZipFileAtPath(170        ''.join([os.getcwd(), '/0.zip']), mode='r').AndReturn(171            self.CreateSingleFileMock())172    self.my_mox.StubOutWithMock(os, 'unlink')173    os.unlink(''.join([os.getcwd(), '/0.zip']))174    self.my_mox.ReplayAll()175    self.assertEqual(False, test_target.FixArchive('SIZE'))176    self.my_mox.VerifyAll()177  def CreateSingleFileMock(self):178    """Create a mock ZipFile object for testSingleFileArchive.179    We just need it to return a single member infolist twice180    Returns:181      A configured mock object182    """183    mock_zip = self.my_mox.CreateMock(zipfile.ZipFile)184    mock_zip.infolist().AndReturn([self.file1])185    mock_zip.infolist().AndReturn([self.file1])186    mock_zip.close()187    return mock_zip188  def testMultiFileArchive(self):189    """Test behavior of DirectoryZipper.FixArchive with a multi-file archive.190    We expect that FixArchive will rename the old archive, adding '-old' before191    '.zip', read all the members except the last one of '-old' into a new192    archive with the same name as the original, and then unlink the '-old' copy193    """194    test_target = divide_and_compress.DirectoryZipper(195        ''.join([os.getcwd(), '/']), 'dummy', 1024*1024, True)196    self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath')197    test_target.OpenZipFileAtPath(198        ''.join([os.getcwd(), '/0.zip']), mode='r').AndReturn(199            self.CreateMultiFileMock())200    self.my_mox.StubOutWithMock(test_target, 'RemoveLastFile')201    test_target.RemoveLastFile(''.join([os.getcwd(), '/0.zip']))202    self.my_mox.StubOutWithMock(os, 'stat')203    os.stat(''.join([os.getcwd(), '/0.zip'])).AndReturn([49302])204    self.my_mox.StubOutWithMock(stat, 'ST_SIZE')205    stat.ST_SIZE = 0206    self.my_mox.ReplayAll()207    self.assertEqual(True, test_target.FixArchive('SIZE'))208    self.my_mox.VerifyAll()209  def CreateMultiFileMock(self):210    """Create mock ZipFile object for use with testMultiFileArchive.211    The mock just needs to return the infolist mock that is prepared in212    InitMultiFileData()213    Returns:214      A configured mock object215    """216    self._InitMultiFileData()217    mock_zip = self.my_mox.CreateMock(zipfile.ZipFile)218    mock_zip.infolist().AndReturn(self.multi_file_dir)219    mock_zip.close()220    return mock_zip221class AddFileToArchiveTest(unittest.TestCase):222  """Test behavior of method to add a file to an archive."""223  def setUp(self):224    """Setup the arguments for the DirectoryZipper object."""225    self.my_mox = mox.Mox()226    self.output_dir = '%s/' % os.getcwd()227    self.file_to_add = 'file.txt'228    self.input_dir = '/foo/bar/baz/'229  def tearDown(self):230    self.my_mox.UnsetStubs()231  def testAddFileToArchive(self):232    """Test the DirectoryZipper.AddFileToArchive method.233    We are testing a pretty trivial method, we just expect it to look at the234    file its adding, so that it possible can through out a warning.235    """236    test_target = divide_and_compress.DirectoryZipper(self.output_dir,237                                                      self.input_dir,238                                                      1024*1024, True)239    self.my_mox.StubOutWithMock(test_target, 'OpenZipFileAtPath')240    archive_mock = self.CreateArchiveMock()241    test_target.OpenZipFileAtPath(242        ''.join([self.output_dir, '0.zip']),243        compress=zipfile.ZIP_DEFLATED).AndReturn(archive_mock)244    self.StubOutOsModule()245    self.my_mox.ReplayAll()246    test_target.AddFileToArchive(''.join([self.input_dir, self.file_to_add]),247                                 zipfile.ZIP_DEFLATED)248    self.my_mox.VerifyAll()249  def StubOutOsModule(self):250    """Create a mock for the os.path and os.stat objects.251    Create a stub that will return the type (file or directory) and size of the252    object that is to be added.253    """254    self.my_mox.StubOutWithMock(os.path, 'isfile')255    os.path.isfile(''.join([self.input_dir, self.file_to_add])).AndReturn(True)256    self.my_mox.StubOutWithMock(os, 'stat')257    os.stat(''.join([self.input_dir, self.file_to_add])).AndReturn([39480])258    self.my_mox.StubOutWithMock(stat, 'ST_SIZE')259    stat.ST_SIZE = 0260    261  def CreateArchiveMock(self):262    """Create a mock ZipFile for use with testAddFileToArchive.263    Just verify that write is called with the file we expect and that the264    archive is closed after the file addition265    Returns:266      A configured mock object267    """268    archive_mock = self.my_mox.CreateMock(zipfile.ZipFile)269    archive_mock.write(''.join([self.input_dir, self.file_to_add]),270                       self.file_to_add)271    archive_mock.close()272    return archive_mock273class CompressDirectoryTest(unittest.TestCase):274  """Test the master method of the class.275  Testing with the following directory structure.276  /dir1/277  /dir1/file1.txt278  /dir1/file2.txt279  /dir1/dir2/280  /dir1/dir2/dir3/281  /dir1/dir2/dir4/282  /dir1/dir2/dir4/file3.txt283  /dir1/dir5/284  /dir1/dir5/file4.txt285  /dir1/dir5/file5.txt286  /dir1/dir5/file6.txt287  /dir1/dir5/file7.txt288  /dir1/dir6/289  /dir1/dir6/file8.txt290  file1.txt., file2.txt, file3.txt should be in 0.zip291  file4.txt should be in 1.zip292  file5.txt, file6.txt should be in 2.zip293  file7.txt will not be stored since it will be too large compressed294  file8.txt should b in 3.zip295  """296  def setUp(self):297    """Setup all the mocks for this test."""298    self.my_mox = mox.Mox()299    self.base_dir = '/dir1'300    self.output_path = '/out_dir/'301    self.test_target = divide_and_compress.DirectoryZipper(302        self.output_path, self.base_dir, 1024*1024, True)303    304    self.InitArgLists()305    self.InitOsDotPath()306    self.InitArchiveIsValid()307    self.InitWriteIndexRecord()308    self.InitAddFileToArchive()309  def tearDown(self):310    self.my_mox.UnsetStubs()311  def testCompressDirectory(self):312    """Test the DirectoryZipper.CompressDirectory method."""313    self.my_mox.ReplayAll()314    for arguments in self.argument_lists:315      self.test_target.CompressDirectory(None, arguments[0], arguments[1])316    self.my_mox.VerifyAll()317  def InitAddFileToArchive(self):318    """Setup mock for DirectoryZipper.AddFileToArchive.319    Make sure that the files are added in the order we expect.320    """321    self.my_mox.StubOutWithMock(self.test_target, 'AddFileToArchive')322    self.test_target.AddFileToArchive('/dir1/file1.txt', zipfile.ZIP_DEFLATED)323    self.test_target.AddFileToArchive('/dir1/file2.txt', zipfile.ZIP_DEFLATED)324    self.test_target.AddFileToArchive('/dir1/dir2/dir4/file3.txt',325                                      zipfile.ZIP_DEFLATED)326    self.test_target.AddFileToArchive('/dir1/dir5/file4.txt',327                                      zipfile.ZIP_DEFLATED)328    self.test_target.AddFileToArchive('/dir1/dir5/file4.txt',329                                      zipfile.ZIP_DEFLATED)330    self.test_target.AddFileToArchive('/dir1/dir5/file5.txt',331                                      zipfile.ZIP_DEFLATED)332    self.test_target.AddFileToArchive('/dir1/dir5/file5.txt',333                                      zipfile.ZIP_DEFLATED)334    self.test_target.AddFileToArchive('/dir1/dir5/file6.txt',335                                      zipfile.ZIP_DEFLATED)336    self.test_target.AddFileToArchive('/dir1/dir5/file7.txt',337                                      zipfile.ZIP_DEFLATED)338    self.test_target.AddFileToArchive('/dir1/dir5/file7.txt',339                                      zipfile.ZIP_DEFLATED)340    self.test_target.AddFileToArchive('/dir1/dir6/file8.txt',341                                      zipfile.ZIP_DEFLATED)342  343  def InitWriteIndexRecord(self):344    """Setup mock for DirectoryZipper.WriteIndexRecord."""345    self.my_mox.StubOutWithMock(self.test_target, 'WriteIndexRecord')346    # we are trying to compress 8 files, but we should only attempt to347    # write an index record 7 times, because one file is too large to be stored348    self.test_target.WriteIndexRecord().AndReturn(True)349    self.test_target.WriteIndexRecord().AndReturn(False)350    self.test_target.WriteIndexRecord().AndReturn(False)351    self.test_target.WriteIndexRecord().AndReturn(True)352    self.test_target.WriteIndexRecord().AndReturn(True)353    self.test_target.WriteIndexRecord().AndReturn(False)354    self.test_target.WriteIndexRecord().AndReturn(True)355  def InitArchiveIsValid(self):356    """Mock out DirectoryZipper.ArchiveIsValid and DirectoryZipper.FixArchive.357    Mock these methods out such that file1, file2, and file3 go into one358    archive. file4 then goes into the next archive, file5 and file6 in the359    next, file 7 should appear too large to compress into an archive, and360    file8 goes into the final archive361    """362    self.my_mox.StubOutWithMock(self.test_target, 'ArchiveIsValid')363    self.my_mox.StubOutWithMock(self.test_target, 'FixArchive')364    self.test_target.ArchiveIsValid().AndReturn(True)365    self.test_target.ArchiveIsValid().AndReturn(True)366    self.test_target.ArchiveIsValid().AndReturn(True)367    # should be file4.txt368    self.test_target.ArchiveIsValid().AndReturn(False)369    self.test_target.FixArchive('SIZE').AndReturn(True)370    self.test_target.ArchiveIsValid().AndReturn(True)371    # should be file5.txt372    self.test_target.ArchiveIsValid().AndReturn(False)373    self.test_target.FixArchive('SIZE').AndReturn(True)374    self.test_target.ArchiveIsValid().AndReturn(True)375    self.test_target.ArchiveIsValid().AndReturn(True)376    # should be file7.txt377    self.test_target.ArchiveIsValid().AndReturn(False)378    self.test_target.FixArchive('SIZE').AndReturn(True)379    self.test_target.ArchiveIsValid().AndReturn(False)380    self.test_target.FixArchive('SIZE').AndReturn(False)381    self.test_target.ArchiveIsValid().AndReturn(True)382    383  def InitOsDotPath(self):384    """Mock out os.path.isfile.385    Mock this out so the things we want to appear as files appear as files and386    the things we want to appear as directories appear as directories. Also387    make sure that the order of file visits is as we expect (which is why388    InAnyOrder isn't used here).389    """390    self.my_mox.StubOutWithMock(os.path, 'isfile')391    os.path.isfile('/dir1/dir2').AndReturn(False)392    os.path.isfile('/dir1/dir5').AndReturn(False)393    os.path.isfile('/dir1/dir6').AndReturn(False)394    os.path.isfile('/dir1/file1.txt').AndReturn(True)395    os.path.isfile('/dir1/file2.txt').AndReturn(True)396    os.path.isfile('/dir1/dir2/dir3').AndReturn(False)397    os.path.isfile('/dir1/dir2/dir4').AndReturn(False)398    os.path.isfile('/dir1/dir2/dir4/file3.txt').AndReturn(True)399    os.path.isfile('/dir1/dir5/file4.txt').AndReturn(True)400    os.path.isfile('/dir1/dir5/file4.txt').AndReturn(True)401    os.path.isfile('/dir1/dir5/file5.txt').AndReturn(True)402    os.path.isfile('/dir1/dir5/file5.txt').AndReturn(True)403    os.path.isfile('/dir1/dir5/file6.txt').AndReturn(True)404    os.path.isfile('/dir1/dir5/file7.txt').AndReturn(True)405    os.path.isfile('/dir1/dir5/file7.txt').AndReturn(True)406    os.path.isfile('/dir1/dir6/file8.txt').AndReturn(True)407  def InitArgLists(self):408    """Create the directory path => directory contents mappings."""409    self.argument_lists = []410    self.argument_lists.append(['/dir1',411                                ['file1.txt', 'file2.txt', 'dir2', 'dir5',412                                 'dir6']])413    self.argument_lists.append(['/dir1/dir2', ['dir3', 'dir4']])414    self.argument_lists.append(['/dir1/dir2/dir3', []])415    self.argument_lists.append(['/dir1/dir2/dir4', ['file3.txt']])416    self.argument_lists.append(['/dir1/dir5',417                                ['file4.txt', 'file5.txt', 'file6.txt',418                                 'file7.txt']])419    self.argument_lists.append(['/dir1/dir6', ['file8.txt']])420      421if __name__ == '__main__':...Evaluate.py
Source:Evaluate.py  
1r"""2 Multi-Label Leaning颿µç»æçè¯æµå½æ°ï¼åªéè¦è°ç¨æåçmll_evaluate()彿°ï¼ä¼ å
¥ç¸åºåæ°å³å¯ï¼å
¶ä»å½æ°ä¸ºè¾
å©çå·¥å
·å½æ°3"""4import csv5import numpy6import numpy as np7from sklearn.metrics import f1_score, roc_auc_score8from sklearn.metrics import hamming_loss9import scipy.io as sci10def find(instance, label1, label2):11    index1 = []12    index2 = []13    for i in range(instance.shape[0]):14        if instance[i] == label1:15            index1.append(i)16        if instance[i] == label2:17            index2.append(i)18    return index1, index219def findmax(outputs):20    Max = -float("inf")21    index = 022    for i in range(outputs.shape[0]):23        if outputs[i] > Max:24            Max = outputs[i]25            index = i26    return Max, index27def sort(x):28    temp = np.array(x)29    length = temp.shape[0]30    index = []31    sortX = []32    for i in range(length):33        Min = float("inf")34        Min_j = i35        for j in range(length):36            if temp[j] < Min:37                Min = temp[j]38                Min_j = j39        sortX.append(Min)40        index.append(Min_j)41        temp[Min_j] = 999999942        # temp[Min_j] = float("inf")43    return temp, index44def findIndex(a, b):45    for i in range(len(b)):46        if a == b[i]:47            return i48def avgprec(outputs, test_target):49    test_data_num = outputs.shape[0]50    class_num = outputs.shape[1]51    temp_outputs = []52    temp_test_target = []53    instance_num = 054    labels_index = []55    not_labels_index = []56    labels_size = []57    for i in range(test_data_num):58        if sum(test_target[i]) != class_num and sum(test_target[i]) != 0:59            instance_num = instance_num + 160            temp_outputs.append(outputs[i])61            temp_test_target.append(test_target[i])62            labels_size.append(sum(test_target[i] == 1))63            index1, index2 = find(test_target[i], 1, 0)64            labels_index.append(index1)65            not_labels_index.append(index2)66    aveprec = 067    for i in range(instance_num):68        tempvalue, index = sort(temp_outputs[i])69        indicator = np.zeros((class_num,))70        for j in range(labels_size[i]):71            loc = findIndex(labels_index[i][j], index)72            indicator[loc] = 173        summary = 074        for j in range(labels_size[i]):75            loc = findIndex(labels_index[i][j], index)76            # print(loc)77            summary = summary + sum(indicator[loc:class_num]) / (class_num - loc);78        aveprec = aveprec + summary / labels_size[i]79    return aveprec / test_data_num80def Coverage(outputs, test_target):81    test_data_num = outputs.shape[0]82    class_num = outputs.shape[1]83    labels_index = []84    not_labels_index = []85    labels_size = []86    for i in range(test_data_num):87        labels_size.append(sum(test_target[i] == 1))88        index1, index2 = find(test_target[i], 1, 0)89        labels_index.append(index1)90        not_labels_index.append(index2)91    cover = 092    for i in range(test_data_num):93        tempvalue, index = sort(outputs[i])94        temp_min = class_num + 195        for j in range(labels_size[i]):96            loc = findIndex(labels_index[i][j], index)97            if loc < temp_min:98                temp_min = loc99        cover = cover + (class_num - temp_min)100    return (cover / test_data_num - 1) / class_num101def HammingLoss(predict_labels, test_target):102    labels_num = predict_labels.shape[1]103    test_data_num = predict_labels.shape[0]104    hammingLoss = 0105    for i in range(test_data_num):106        notEqualNum = 0107        for j in range(labels_num):108            if predict_labels[i][j] != test_target[i][j]:109                notEqualNum = notEqualNum + 1110        hammingLoss = hammingLoss + notEqualNum / labels_num111    hammingLoss = hammingLoss / test_data_num112    return hammingLoss113def OneError(outputs, test_target):114    test_data_num = outputs.shape[0]115    class_num = outputs.shape[1]116    num = 0117    one_error = 0118    for i in range(test_data_num):119        if sum(test_target[i]) != class_num and sum(test_target[i]) != 0:120            Max, index = findmax(outputs[i])121            num = num + 1122            if test_target[i][index] != 1:123                one_error = one_error + 1124    return one_error / num125def rloss(outputs, test_target):126    test_data_num = outputs.shape[0]127    class_num = outputs.shape[1]128    temp_outputs = []129    temp_test_target = []130    instance_num = 0131    labels_index = []132    not_labels_index = []133    labels_size = []134    for i in range(test_data_num):135        if sum(test_target[i]) != class_num and sum(test_target[i]) != 0:136            instance_num = instance_num + 1137            temp_outputs.append(outputs[i])138            temp_test_target.append(test_target[i])139            labels_size.append(sum(test_target[i] == 1))140            index1, index2 = find(test_target[i], 1, 0)141            labels_index.append(index1)142            not_labels_index.append(index2)143    rankloss = 0144    for i in range(instance_num):145        m = labels_size[i]146        n = class_num - m147        temp = 0148        for j in range(m):149            for k in range(n):150                if temp_outputs[i][labels_index[i][j]] < temp_outputs[i][not_labels_index[i][k]]:151                    temp = temp + 1152        rankloss = rankloss + temp / (m * n)153    rankloss = rankloss / instance_num154    return rankloss155def SubsetAccuracy(predict_labels, test_target):156    test_data_num = predict_labels.shape[0]157    class_num = predict_labels.shape[1]158    correct_num = 0159    for i in range(test_data_num):160        for j in range(class_num):161            if predict_labels[i][j] != test_target[i][j]:162                break163        if j == class_num - 1:164            correct_num = correct_num + 1165    return correct_num / test_data_num166def MacroAveragingAUC(outputs, test_target):167    test_data_num = outputs.shape[0]168    class_num = outputs.shape[1]169    P = []170    N = []171    labels_size = []172    not_labels_size = []173    AUC = 0174    for i in range(class_num):175        P.append([])176        N.append([])177    for i in range(test_data_num):  # å¾å°PkåNk178        for j in range(class_num):179            if test_target[i][j] == 1:180                P[j].append(i)181            else:182                N[j].append(i)183    for i in range(class_num):184        labels_size.append(len(P[i]))185        not_labels_size.append(len(N[i]))186    for i in range(class_num):187        auc = 0188        for j in range(labels_size[i]):189            for k in range(not_labels_size[i]):190                pos = outputs[P[i][j]][i]191                neg = outputs[N[i][k]][i]192                if pos > neg:193                    auc = auc + 1194        print(AUC, auc, labels_size[i], not_labels_size[i])195        AUC = AUC + auc / (labels_size[i] * not_labels_size[i])196    return AUC / class_num197def Performance(predict_labels, test_target):198    data_num = predict_labels.shape[0]199    tempPre = np.transpose(np.copy(predict_labels))200    tempTar = np.transpose(np.copy(test_target))201    tempTar[tempTar == 0] = -1202    com = sum(tempPre == tempTar)203    tempTar[tempTar == -1] = 0204    PreLab = sum(tempPre)205    TarLab = sum(tempTar)206    I = 0207    for i in range(data_num):208        if TarLab[i] == 0:209            I += 1210        else:211            if PreLab[i] == 0:212                I += 0213            else:214                I += com[i] / PreLab[i]215    return I / data_num216# y_pred为MLLç颿µç»æï¼y_real为MLLçå®é
ç»æï¼äºè
åºè¯¥é½ä¸ºç©éµï¼éé¢åªæè®°å·ä¸º1æè
0217def mll_evaluate(y_pred, y_real):218    hm = round(hamming_loss(y_true=y_real, y_pred=y_pred), ndigits=3) # 宿¹HammingLossç计ç®ç¨åº219    f1_score_micro = round(f1_score(y_true=y_real, y_pred=y_pred, average='micro'), ndigits=3) # 宿¹F1-Micro220    # f1_score_macro = round(f1_score(y_true=y_real, y_pred=y_pred, average='macro'), ndigits=5) # 宿¹F1-Macro221    # auc = round(roc_auc_score(y_true=y_real, y_score=y_pred), ndigits=5) #roc222    # f1_score_macro_2 = round(MacroAveragingAUC(outputs=y_pred, test_target=y_real), ndigits=5) # èªå®ä¹F1-Macro223    average_precision = round(avgprec(outputs=y_pred, test_target=y_real), ndigits=3)224    coverage = round(Coverage(outputs=y_pred, test_target=y_real), ndigits=3)225    one_error = round(OneError(outputs=y_pred, test_target=y_real), ndigits=3)226    rank_loss = round(rloss(outputs=y_pred, test_target=y_real), ndigits=3)227    subset_accuracy = round(SubsetAccuracy(predict_labels=y_pred, test_target=y_real), ndigits=3)228    print('The bigger the value, the better the performance.', '\n',229          'Subset accuracy'.ljust(20, "-"), subset_accuracy, '\n',230          'Average precision'.ljust(20, "-"), average_precision)231    print('The smaller the value, the better the performance.', '\n',232          'Hamming loss'.ljust(20, "-"), hm, '\n',233          'One-error'.ljust(20, "-"), one_error, '\n',234          'Coverage'.ljust(20, "-"), coverage, '\n',235          'Ranking'.ljust(20, "-"), rank_loss)236    return subset_accuracy, average_precision, \237           hm, one_error, coverage, rank_loss238# è¿åk-fold交åéªè¯åçmean+std239def mean_std(source):240    mean = np.mean()241    std = np.std()...test_annotated_regions.py
Source:test_annotated_regions.py  
1# Licensed to the Apache Software Foundation (ASF) under one2# or more contributor license agreements.  See the NOTICE file3# distributed with this work for additional information4# regarding copyright ownership.  The ASF licenses this file5# to you under the Apache License, Version 2.0 (the6# "License"); you may not use this file except in compliance7# with the License.  You may obtain a copy of the License at8#9#   http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing,12# software distributed under the License is distributed on an13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14# KIND, either express or implied.  See the License for the15# specific language governing permissions and limitations16# under the License.17# pylint: disable=no-else-return, unidiomatic-typecheck, invalid-name18import tvm19from tvm import relay20from tvm.relay.op.annotation import compiler_begin, compiler_end21def check_region(region_set, target, args, nodes, rets):22    region = region_set.get_region(args[0])23    assert region24    assert target == region.target25    assert set(args) == set(region.args)26    assert set(nodes) == set(region.nodes)27    assert set(rets) == set(region.rets)28def test_region_set_creator_diamond():29    data = relay.var("data", shape=(10, 10))30    cb_1 = compiler_begin(data, "test_target")31    O_1 = relay.abs(cb_1)32    ce_1 = compiler_end(O_1, "test_target")33    ce_2 = compiler_end(O_1, "test_target")34    cb_2 = compiler_begin(ce_1, "test_target")35    O_2 = relay.nn.relu(cb_2)36    ce_3 = compiler_end(O_2, "test_target")37    cb_d = compiler_begin(ce_2, "default")38    X = relay.tanh(cb_d)39    ce_d = compiler_end(X, "default")40    cb_3 = compiler_begin(ce_3, "test_target")41    cb_4 = compiler_begin(ce_d, "test_target")42    O_3 = relay.add(cb_3, cb_4)43    ce_4 = compiler_end(O_3, "test_target")44    diamond = relay.Function([data], ce_4)45    region_set = relay.analysis.AnnotatedRegionSet(46        diamond, relay.op.get("annotation.compiler_begin"), relay.op.get("annotation.compiler_end")47    )48    assert len(region_set) == 449    check_region(50        region_set,51        "test_target",52        [cb_1],53        [cb_1, O_1, ce_1, ce_2],54        [ce_1, ce_2],55    )56    check_region(57        region_set,58        "test_target",59        [cb_2],60        [cb_2, O_2, ce_3],61        [ce_3],62    )63    check_region(64        region_set,65        "default",66        [cb_d],67        [cb_d, X, ce_d],68        [ce_d],69    )70    check_region(71        region_set,72        "test_target",73        [cb_3, cb_4],74        [cb_3, cb_4, O_3, ce_4],75        [ce_4],76    )77def test_region_set_creator_merged():78    data = relay.var("data", shape=(10, 10))79    cb_1 = compiler_begin(data, "test_target")80    O_1 = relay.abs(cb_1)81    ce_2 = compiler_end(O_1, "test_target")82    O_2 = relay.nn.relu(O_1)83    ce_3 = compiler_end(O_2, "test_target")84    cb_d = compiler_begin(ce_2, "default")85    X = relay.tanh(cb_d)86    ce_d = compiler_end(X, "default")87    cb_3 = compiler_begin(ce_3, "test_target")88    cb_4 = compiler_begin(ce_d, "test_target")89    O_3 = relay.add(cb_3, cb_4)90    O_4 = relay.add(cb_3, cb_4)91    O_5 = relay.Tuple([O_3, O_4])92    ce_4 = compiler_end(O_5, "test_target")93    merged = relay.Function([data], ce_4)94    region_set = relay.analysis.AnnotatedRegionSet(95        merged, relay.op.get("annotation.compiler_begin"), relay.op.get("annotation.compiler_end")96    )97    assert len(region_set) == 398    check_region(99        region_set,100        "test_target",101        [cb_1],102        [cb_1, O_1, O_2, ce_2, ce_3],103        [ce_2, ce_3],104    )105    check_region(106        region_set,107        "default",108        [cb_d],109        [cb_d, X, ce_d],110        [ce_d],111    )112    check_region(113        region_set,114        "test_target",115        [cb_3, cb_4],116        [cb_3, cb_4, O_3, O_4, O_5, ce_4],117        [ce_4],118    )119if __name__ == "__main__":120    test_region_set_creator_diamond()...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!!
