<commit_before>from opendr.renderer import DepthRenderer
import numpy as np
import unittest


visualize = False

class TestDepthRenderer(unittest.TestCase):

    def test_depth_image(self):
        # Create renderer
        import chumpy as ch
        from opendr.renderer import DepthRenderer
        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,0]), rotation=ch.zeros(3))
        m.v = m.v * .01
        m.v[:,2] += 4
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, vc=m.vc*0+1, bgcolor=ch.zeros(3))
        
        # import time
        # tm = time.time()
        # rn.r
        # print 'took %es' % (time.time() - tm)

        # Show it
        # if visualize:
        #     import matplotlib.pyplot as plt
        #     plt.ion()
        #     plt.imshow(rn.r)
        #     plt.show()

        # print np.min(rn.r.ravel())
        # print np.max(rn.r.ravel())
        self.assertTrue(np.abs(np.min(rn.r.ravel()) - 3.98) < 1e-5)
        self.assertTrue(np.abs(np.min(m.v[:,2]) - np.min(rn.r.ravel())) < 1e-5)
        self.assertTrue(np.abs(rn.r[h/2,w/2] - 3.98) < 1e-5)

    def test_derivatives(self):
        import chumpy as ch
        from chumpy.utils import row
        import numpy as np
        from opendr.renderer import DepthRenderer

        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3))
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, bgcolor=ch.zeros(3))

        if visualize:
            import matplotlib.pyplot as plt
            plt.figure()
        for which in range(3):
            r1 = rn.r

            adder = np.zeros(3)
            adder[which] = .01
            change = rn.v.r * 0 + row(adder)
            dr_pred = rn.dr_wrt(rn.v).dot(change.ravel()).reshape(rn.shape)
            rn.v = rn.v.r + change

            r2 = rn.r
            dr_emp = r2 - r1

            # print np.mean(np.abs(dr_pred-dr_emp))

            self.assertTrue(np.mean(np.abs(dr_pred-dr_emp)) < .025)

            if visualize:
                plt.subplot(2,3,which+1)
                plt.imshow(dr_pred)
                plt.clim(-.01,.01)
                plt.title('emp')
                plt.subplot(2,3,which+4)
                plt.imshow(dr_emp)
                plt.clim(-.01,.01)
                plt.title('pred')
                # cv2.imshow('pred%d' % (which,), dr_pred / np.max(dr_emp.ravel())*200.)
                # cv2.imshow('emp%d' % (which,), dr_emp / np.max(dr_emp.ravel())*200.)

    def test_derivatives2(self):
        import chumpy as ch
        from chumpy.utils import row
        import numpy as np
        from opendr.renderer import DepthRenderer

        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3))
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, bgcolor=ch.zeros(3))

        if visualize:
            import matplotlib.pyplot as plt
            plt.ion()
            plt.figure()

        for which in range(3):
            r1 = rn.r

            adder = np.random.rand(rn.v.r.size).reshape(rn.v.r.shape)*.01
            change = rn.v.r * 0 + adder
            dr_pred = rn.dr_wrt(rn.v).dot(change.ravel()).reshape(rn.shape)
            rn.v = rn.v.r + change

            r2 = rn.r
            dr_emp = r2 - r1

            print np.mean(np.abs(dr_pred-dr_emp))

            self.assertTrue(np.mean(np.abs(dr_pred-dr_emp)) < .019)

            if visualize:
                plt.subplot(2,3,which+1)
                plt.imshow(dr_pred)
                plt.clim(-.01,.01)
                plt.title('emp')
                plt.subplot(2,3,which+4)
                plt.imshow(dr_emp)
                plt.clim(-.01,.01)
                plt.title('pred')
                plt.draw()
                plt.show()
                # cv2.imshow('pred%d' % (which,), dr_pred / np.max(dr_emp.ravel())*200.)
                # cv2.imshow('emp%d' % (which,), dr_emp / np.max(dr_emp.ravel())*200.)
            # if True: # visualize:
            #     import cv2
            #     cv2.imshow('pred%d' % (which,), dr_pred / np.max(dr_emp.ravel())*200.)
            #     cv2.imshow('emp%d' % (which,), dr_emp / np.max(dr_emp.ravel())*200.)

if __name__ == '__main__':
    visualize = True
    #unittest.main()
    suite = unittest.TestLoader().loadTestsFromTestCase(TestDepthRenderer)
    unittest.TextTestRunner(verbosity=2).run(suite)
    import pdb; pdb.set_trace()
<commit_msg>test_dr/test_depth_renderer.py: call np.random.seed(0), and remove unused code<commit_after>from opendr.renderer import DepthRenderer
import numpy as np
import unittest


visualize = False

class TestDepthRenderer(unittest.TestCase):

    def setUp(self):
        np.random.seed(0)

    def test_depth_image(self):
        # Create renderer
        import chumpy as ch
        from opendr.renderer import DepthRenderer
        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,0]), rotation=ch.zeros(3))
        m.v = m.v * .01
        m.v[:,2] += 4
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, vc=m.vc*0+1, bgcolor=ch.zeros(3))
        
        # import time
        # tm = time.time()
        # rn.r
        # print 'took %es' % (time.time() - tm)

        # print np.min(rn.r.ravel())
        # print np.max(rn.r.ravel())
        self.assertTrue(np.abs(np.min(rn.r.ravel()) - 3.98) < 1e-5)
        self.assertTrue(np.abs(np.min(m.v[:,2]) - np.min(rn.r.ravel())) < 1e-5)
        self.assertTrue(np.abs(rn.r[h/2,w/2] - 3.98) < 1e-5)

    def test_derivatives(self):
        import chumpy as ch
        from chumpy.utils import row
        import numpy as np
        from opendr.renderer import DepthRenderer

        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3))
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, bgcolor=ch.zeros(3))

        if visualize:
            import matplotlib.pyplot as plt
            plt.figure()
        for which in range(3):
            r1 = rn.r

            adder = np.zeros(3)
            adder[which] = .01
            change = rn.v.r * 0 + row(adder)
            dr_pred = rn.dr_wrt(rn.v).dot(change.ravel()).reshape(rn.shape)
            rn.v = rn.v.r + change

            r2 = rn.r
            dr_emp = r2 - r1

            # print np.mean(np.abs(dr_pred-dr_emp))

            self.assertTrue(np.mean(np.abs(dr_pred-dr_emp)) < .025)

            if visualize:
                plt.subplot(2,3,which+1)
                plt.imshow(dr_pred)
                plt.clim(-.01,.01)
                plt.title('emp')
                plt.subplot(2,3,which+4)
                plt.imshow(dr_emp)
                plt.clim(-.01,.01)
                plt.title('pred')

    def test_derivatives2(self):
        import chumpy as ch
        import numpy as np
        from opendr.renderer import DepthRenderer

        rn = DepthRenderer()

        # Assign attributes to renderer
        from opendr.test_dr.common import get_earthmesh
        m = get_earthmesh(trans=ch.array([0,0,4]), rotation=ch.zeros(3))
        w, h = (320, 240)
        from opendr.camera import ProjectPoints
        rn.camera = ProjectPoints(v=m.v, rt=ch.zeros(3), t=ch.zeros(3), f=ch.array([w,w])/2., c=ch.array([w,h])/2., k=ch.zeros(5))
        rn.frustum = {'near': 1., 'far': 10., 'width': w, 'height': h}
        rn.set(v=m.v, f=m.f, bgcolor=ch.zeros(3))

        if visualize:
            import matplotlib.pyplot as plt
            plt.ion()
            plt.figure()

        for which in range(3):
            r1 = rn.r

            adder = np.random.rand(rn.v.r.size).reshape(rn.v.r.shape)*.01
            change = rn.v.r * 0 + adder
            dr_pred = rn.dr_wrt(rn.v).dot(change.ravel()).reshape(rn.shape)
            rn.v = rn.v.r + change

            r2 = rn.r
            dr_emp = r2 - r1

            #print np.mean(np.abs(dr_pred-dr_emp))

            self.assertTrue(np.mean(np.abs(dr_pred-dr_emp)) < .019)

            if visualize:
                plt.subplot(2,3,which+1)
                plt.imshow(dr_pred)
                plt.clim(-.01,.01)
                plt.title('emp')
                plt.subplot(2,3,which+4)
                plt.imshow(dr_emp)
                plt.clim(-.01,.01)
                plt.title('pred')
                plt.draw()
                plt.show()

if __name__ == '__main__':
    visualize = True
    #unittest.main()
    suite = unittest.TestLoader().loadTestsFromTestCase(TestDepthRenderer)
    unittest.TextTestRunner(verbosity=2).run(suite)
    import pdb; pdb.set_trace()

{'fraction_non_alphanumeric': 0.1537114505302072, 'fraction_numerical': 0.028115575445082207, 'mean_word_length': 1.7957574109328256, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1858060', 'n_tokens_mistral': 3956, 'n_tokens_neox': 3693, 'n_words': 673}