⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mvctestcase.py

📁 JMVM MPEG MVC/3DAV 测试平台 国际通用标准
💻 PY
字号:

"""
This module provides the MVCTestCase class, which is useful for
defining a test encoding run that can be spawned on a Beowulf
cluster or just locally on a single machine.  The idea is that
you can write a script that creates an instance of the MVCTestCase
class to define your tests.

See the documentation for the MVCTestCase class for instructions.
"""

import CreateConfigFile, os, time, re

class MVCTestCase:
    """
    class MVCTestCase

    This class creates a test case for multiview video coding.  After
    creating the test case, calling the Run() method will spawn the
    job on a Beowulf cluster machine.  Alternatively, calling the
    RunOnLocalhost() method will start the job on the local machine.
    This is useful if you do not have a Beowulf cluster.
    
    See documentation for the __init__ method for what the initialization
    parameters mean.
    """

    def __init__(self, qp, paramsForMVC={}, paramsForJSVM={},
                 pythonExe='python', 
                 baseWorkingDirectory = os.path.join(
        os.sep,'opt','turing','scratch',os.getenv('USER'),'MVC'),
                 qsubExe='/opt/torque//bin/qsub',mainMVCDir=os.getcwd(),
                 comment='',adminEmail=None, switchedWorkingDir=None):
        """
        __init__(self, qp, paramsForMVC={}, paramsForJSVM={},
                 pythonExe='python', 
                 baseWorkingDirectory = os.path.join(
        os.sep,'opt','turing','scratch',os.getenv('USER'),'MVC')
                 qsubExe='/opt/torque/bin/qsub',mainMVCDir=os.getcwd(),
                 comment='',adminEmail=None, switchedWorkingDir=None):

                 qp:    Quantiation parameter to use.

                 paramsForMVC: A dictionary of parameter overrides to use
                               in the generated config file.

                 paramsForJSVM: A dictionary of parameter overrides to use
                                in the generated config file.

                 pythonExe:   Name of python executable.

                 baseWorkingDirectory: The directory to use for output.
                 
                 qsubExe:     Path to qsub executable to spawn a Beowulf job.

                 mainMVCDir:  Path to directory containing the MVCRunner.py
                              module to call to actually do multiview coding.
                              If you called python from the directory above
                              MVCRunner.py, this will automatically be set.

                 comment:     A simple string comment that will be used in
                              the automatically generated working directory.

                 adminEmail:  Optional email address of someone to send
                              problems to when running test case.

                 switchedWorkingDir:  Optional directory to switch to for
                                      .yuv, .264, and other files.
                              
        """
        for item in ['pythonExe', 'mainMVCDir', 'qsubExe', 'comment',
                     'baseWorkingDirectory','qp']:
            self.__dict__[item] = locals()[item]
        assert (re.match('^[-:.,_+=@a-zA-Z_0-9]*$',self.comment)), (
            'Invalid comment %s; Only simple characters (%s) allowed.' % (
            `self.comment`,'[-:.,_+=@a-zA-Z_0-9]'))

        self.workingDir = os.path.join(
            self.baseWorkingDirectory,
            paramsForMVC['SEQUENCE'].strip('_'),
            self.comment + paramsForMVC['SEQUENCE'] + 
            time.strftime('_%h_%d_%H_%M__qp') + `qp` )
        self.configFileName = os.path.join(self.workingDir,'configFile.py')
        self.paramsForMVC = dict(paramsForMVC)
        self.paramsForJSVM = dict(paramsForJSVM)
        self.adminEmail = adminEmail
        self.switchedWorkingDir = switchedWorkingDir
        self.namePrefix = paramsForMVC['SEQUENCE'].strip('_') + '_MVCScript'

    def Run(self):
        """
        Run(self):

        This is the main method for this class.  After you create an
        instance of the class, calling this spawns the job on turing
        and exits.
        """
        if (not os.path.exists(self.qsubExe)):
            print '\n\n'
            print 'WARNING:  No qsub executable found so jobs will be'
            print '          run on local machine not on parallel cluster.\n\n'
            self.RunOnLocalhost()
            
        self.PrepareDirectoryForJob()
        self.PrepareQsubScript()
        self.ExecuteQsubScript()

    def RunOnLocalhost(self):
        """
        RunOnLocalhost(self):

        This is an alternative to the Run method.  After you create an
        instance of the class, calling this starts the job on the
        local machine instead of on the Beowulf cluster.
        """
        self.PrepareDirectoryForJob()
        self.PrepareQsubScript()
        self.ExecuteQsubScriptOnLocalhost()


    def PrepareDirectoryForJob(self):
        for directory in [os.path.dirname(os.path.dirname(self.workingDir)),
                          os.path.dirname(self.workingDir),self.workingDir]:
            if (not os.path.exists(directory)):
                print '\nCreating directory %s\n' % `directory`
                os.mkdir(directory)   
            
        CreateConfigFile.WriteConfigFile(
            self.paramsForMVC,self.paramsForJSVM,self.configFileName,
            self.mainMVCDir,self.workingDir,self.qp,self.adminEmail,
            self.switchedWorkingDir)

        self.scriptFile = os.path.join(self.workingDir,self.namePrefix+'.csh')
        self.logFile = os.path.join(self.workingDir,self.namePrefix+'.log')
        self.errFile = os.path.join(self.workingDir,self.namePrefix+'.err')


    def PrepareQsubScript(self):

        print '--> Creating temporary execution script ' + `self.scriptFile`

        fd = open(self.scriptFile,'w')
        fd.write('#!/bin/csh\n')
        fd.write('cd ' + self.workingDir + '\n')
        fd.write('%s %s %s |& tee stdout.txt\n' % (
            self.pythonExe,
            os.path.join(self.mainMVCDir,'MVC','tools','MVCRunner.py'),
            self.configFileName))
        fd.close()
        os.chmod(self.scriptFile,0755)

    def ExecuteQsubScript(self):
        print '\n\n'
        print '\t***********************************'
        print '\t*                                 *'
        print '\t*  SPAWNING JOB VIA BEOWULF       *'
        print '\t*                                 *'
        print '\t***********************************'
        print '\n\n'
        os.spawnv(os.P_NOWAIT,self.qsubExe,
                  [self.qsubExe,'-lnodes=1','-m','ae','-o',
                   self.logFile,'-e',self.errFile,self.scriptFile])

    def ExecuteQsubScriptOnLocalhost(self):
        print '\n\n'
        print '\t***********************************'
        print '\t*                                 *'
        print '\t*  STARTING JOB ON LOCAL MACHINE  *'
        print '\t*                                 *'
        print '\t***********************************'
        print '\n\n'
        os.execv(self.scriptFile,[self.scriptFile])


def MyDefaultEmail():
    """
    MyDefaultEmail():

    Create an email address for me by looking at the USER and HOST
    environment variables.
    """
    (user, host) = map(os.getenv, ['USER','HOST'])
    if ('' == user.strip() or '' == user.strip()):
        print '\n'
        print 'WARNING:   Could not determine default email address.\n'
        print '           Setting default email to None.\n\n'
        return None
    else:
        splitHost = host.split('.')
        N = len(splitHost)
        if ('turing' == host.strip() or
            re.compile('.*merl.com').search(host.strip())):
            print '\n'
            print 'WARNING:  Using mail.merl.com for host on MERL system.'
            return '%s@mail.merl.com' % user
        elif (N >= 2):
            return '%s@%s.%s' % (user, splitHost[N-2], splitHost[N-1])
        else:
            print 'WARNING:  Could not figure out hostname from %s.' %host
            print '           Setting default email to None.\n\n'
            return None
    
            
            

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -