Python shutil.copy() Examples

The following are 30 code examples of shutil.copy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module shutil , or try the search function .
Example #1
Source File: install.py    From multibootusb with GNU General Public License v2.0 7 votes vote down vote up
def copy_iso(src, dst):
    """
    A simple wrapper for copying larger files. This is necessary as
    shutil copy files is much slower under Windows platform
    :param src: Path to source file
    :param dst: Destination directory
    :return:
    """
    if platform.system() == "Windows":
        # Note that xcopy asks if the target is a file or a directory when
        # source filename (or dest filename) contains space(s) and the target
        # does not exist.
        assert os.path.exists(dst)
        subprocess.call(['xcopy', '/Y', src, dst], shell=True)
    elif platform.system() == "Linux":
        shutil.copy(src, dst) 
Example #2
Source File: log_copy.py    From glazier with Apache License 2.0 6 votes vote down vote up
def _ShareUpload(self, source_log: Text, share: Text):
    """Copy the log file to a network file share.

    Args:
      source_log: Path to the source log file to be copied.
      share: The destination share to copy the file to.

    Raises:
      LogCopyError: Failure to mount share and copy log.
    """
    creds = LogCopyCredentials()
    username = creds.GetUsername()
    password = creds.GetPassword()

    mapper = drive_map.DriveMap()
    result = mapper.MapDrive('l:', share, username, password)
    if result:
      destination = self._GetLogFileName()
      try:
        shutil.copy(source_log, destination)
      except shutil.Error:
        raise LogCopyError('Log copy failed.')
      mapper.UnmapDrive('l:')
    else:
      raise LogCopyError('Drive mapping failed.') 
Example #3
Source File: settings.py    From bioservices with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, name=None, default_params={}):
        """name is going to be the generic name of the config folder

        e.g., /home/user/.config/<name>/<name>.cfg

        """
        if name is None:
            raise Exception("Name parameter must be provided")
        else:
            # use input parameters
            self.name = name
            self._default_params = copy.deepcopy(default_params)
            self.params = copy.deepcopy(default_params)

            # useful tool to handle XDG config file, path and parameters
            self.appdirs = appdirs.AppDirs(self.name)

            # useful tool to handle the config ini file
            self.config_parser = DynamicConfigParser()

            # Now, create the missing directories if needed
            self.init() # and read the user config file updating params if needed 
Example #4
Source File: test_mmbot.py    From MaliciousMacroBot with MIT License 6 votes vote down vote up
def test_init_files_in_directories_retain_contents():
    """
    Test ensures the mmb_init function can rebuild a model leveraging saved results
    without reprocessing all samples every time
    """
    # Create model with a few samples

    resetTest()
    mmb = MaliciousMacroBot(benign_path, 
                            malicious_path, 
                            model_path, retain_sample_contents=True)
    result = mmb.mmb_init_model(modelRebuild=True)

    shutil.copy(origsample_path, os.path.join(malicious_path, sample))

    # Add a file and rebuild
    mmb = MaliciousMacroBot(benign_path, 
                            malicious_path, 
                            model_path, retain_sample_contents=True)
    result = mmb.mmb_init_model(modelRebuild=True)
    assert result 
Example #5
Source File: can_haz_image.py    From macops with Apache License 2.0 6 votes vote down vote up
def GetBuildPackages(self):
    """Downloads the packages to be installed."""
    package_path = os.path.join(self.cwd, BUILD, 'Packages/')
    try:
      os.mkdir(package_path)
    except OSError:
      pass
    catalogs = [os.path.join(self.cwd, 'base%s_new.catalog' % self.os_version),
                os.path.join(self.cwd,
                             'thirdparty%s_new.catalog' % self.os_version)]

    for catalog in catalogs:
      f = open(catalog, 'r')
      packages = f.readlines()
      for line in packages:
        shutil.copy(os.path.join(TMPDIR, line.split()[0]),
                    os.path.join(package_path, line.split()[0])) 
Example #6
Source File: datasets.py    From pruning_yolov3 with GNU General Public License v3.0 6 votes vote down vote up
def __next__(self):
        self.count += 1
        img0 = self.imgs.copy()
        if cv2.waitKey(1) == ord('q'):  # q to quit
            cv2.destroyAllWindows()
            raise StopIteration

        # Letterbox
        img = [letterbox(x, new_shape=self.img_size, interp=cv2.INTER_LINEAR)[0] for x in img0]

        # Stack
        img = np.stack(img, 0)

        # Normalize RGB
        img = img[:, :, :, ::-1].transpose(0, 3, 1, 2)  # BGR to RGB
        img = np.ascontiguousarray(img, dtype=np.float16 if self.half else np.float32)  # uint8 to fp16/fp32
        img /= 255.0  # 0 - 255 to 0.0 - 1.0

        return self.sources, img, img0, None 
Example #7
Source File: create_delex_data.py    From ConvLab with MIT License 6 votes vote down vote up
def loadData():
    data_url = "data/multi-woz/data.json"
    dataset_url = "https://www.repository.cam.ac.uk/bitstream/handle/1810/280608/MULTIWOZ2.zip?sequence=3&isAllowed=y"
    if not os.path.exists("data"):
        os.makedirs("data")
        os.makedirs("data/multi-woz")

    if not os.path.exists(data_url):
        print("Downloading and unzipping the MultiWOZ dataset")
        resp = urllib.urlopen(dataset_url)
        zip_ref = ZipFile(BytesIO(resp.read()))
        zip_ref.extractall("data/multi-woz")
        zip_ref.close()
        shutil.copy('data/multi-woz/MULTIWOZ2 2/data.json', 'data/multi-woz/')
        shutil.copy('data/multi-woz/MULTIWOZ2 2/valListFile.json', 'data/multi-woz/')
        shutil.copy('data/multi-woz/MULTIWOZ2 2/testListFile.json', 'data/multi-woz/')
        shutil.copy('data/multi-woz/MULTIWOZ2 2/dialogue_acts.json', 'data/multi-woz/') 
Example #8
Source File: prepare_dataset.py    From ICDAR-2019-SROIE with MIT License 6 votes vote down vote up
def get_data():
    filenames = [os.path.splitext(f)[0] for f in glob.glob("original/*.jpg")]
    jpg_files = [s + ".jpg" for s in filenames]
    txt_files = [s + ".txt" for s in filenames]

    for file in txt_files:
        boxes = []
        with open(file, "r", encoding="utf-8", newline="") as lines:
            for line in csv.reader(lines):
                boxes.append([line[0], line[1], line[6], line[7]])
        with open('mlt/label/' + file.split('/')[1], "w+") as labelFile:
            wr = csv.writer(labelFile)
            wr.writerows(boxes)

    for jpg in jpg_files:
        shutil.copy(jpg, 'mlt/image/') 
Example #9
Source File: strainsimulationwrapper.py    From CAMISIM with Apache License 2.0 6 votes vote down vote up
def _prepare_simulation_subfolder(self, directory_strains):
		"""
		Create strain directory and copy templates and parameter file into it.

		@param directory_strains: Directory for the simulated strains
		@type directory_strains: str | unicode

		@return: Nothing
		@rtype: None
		"""
		if not os.path.exists(directory_strains):
			os.mkdir(directory_strains)
		for filename in self._directory_template_filenames:
			src = os.path.join(self._directory_template, filename)
			dst = os.path.join(directory_strains, filename)
			shutil.copy(src, dst) 
Example #10
Source File: postinstall_simnibs.py    From simnibs with GNU General Public License v3.0 6 votes vote down vote up
def setup_gmsh_options(force=False, silent=False):
    ''' Copies the gmsh_options file to the appropriate place '''
    if sys.platform in ['linux', 'darwin']:
        target = os.path.expanduser('~/.gmsh-options')
    else:
        target = os.path.join(os.getenv('APPDATA'), 'gmsh-options')

    silent = silent and GUI
    copy = True
    if os.path.isfile(target):
        if force:
            copy = True
        else:
            copy = _get_input(
                'Found a gmsh configuration file, do you whish to overwite it?',
                silent)

    if copy:
        gmsh_options_path = os.path.join(
            SIMNIBSDIR, 'resources', 'gmsh-options_simnibsdefault')
        _copy_and_log(gmsh_options_path, target) 
Example #11
Source File: db.py    From StructEngPy with MIT License 6 votes vote down vote up
def open(self,database):
    """
    params:
        database: str. Database to be opered. The path should be included
    """
    assert(database[-4:]=='.mdo')
    if not os.path.exists(database):
        self._create(database)
    operate_db=database[:-4]+'.op'
    shutil.copy(database,operate_db)
#        engine=create_engine('sqlite:///:memory:')
    engine=create_engine('sqlite:///'+operate_db) #should be run in memory in the future
    Session=o.sessionmaker(bind=engine)
    self.session=Session()
    self.__operate_db=operate_db
    self.__storage_db=database 
Example #12
Source File: extractor.py    From firmanal with MIT License 6 votes vote down vote up
def _check_kernel(self):
        """
        If this file contains a kernel version string, assume it is a kernel.
        Only Linux kernels are currently extracted.
        """
        if not self.get_kernel_status():
            for module in binwalk.scan(self.item, "-y", "kernel",
                                       signature=True, quiet=True):
                for entry in module.results:
                    if "kernel version" in entry.description:
                        self.update_database("kernel_version",
                                             entry.description)
                        if "Linux" in entry.description:
                            if self.get_kernel_path():
                                shutil.copy(self.item, self.get_kernel_path())
                            else:
                                self.extractor.do_kernel = False
                            self.printf(">>>> %s" % entry.description)
                            return True
                        # VxWorks, etc
                        else:
                            self.printf(">>>> Ignoring: %s" % entry.description)
                            return False
                return False
        return False 
Example #13
Source File: import_train_images.py    From L3C-PyTorch with GNU General Public License v3.0 6 votes vote down vote up
def process(self, p_in):
        fn, ext = os.path.splitext(os.path.basename(p_in))
        if fn in self.images_cleaned:
            return 1
        if fn in self.images_discarded:
            return 0
        try:
            im = Image.open(p_in)
        except OSError as e:
            print(f'\n*** Error while opening {p_in}: {e}')
            return 0
        im_out = random_resize_or_discard(im, self.min_res)
        if im_out is not None:
            p_out = join(self.out_dir_clean, fn + '.png')  # Make sure to use .png!
            im_out.save(p_out)
            return 1
        else:
            p_out = join(self.out_dir_discard, os.path.basename(p_in))
            shutil.copy(p_in, p_out)
            return 0 
Example #14
Source File: __init__.py    From aegea with Apache License 2.0 6 votes vote down vote up
def initialize():
    global config, parser
    from .util.printing import BOLD, RED, ENDC
    config = AegeaConfig(__name__, use_yaml=True, save_on_exit=False)
    if not os.path.exists(config.config_files[2]):
        config_dir = os.path.dirname(os.path.abspath(config.config_files[2]))
        try:
            os.makedirs(config_dir)
        except OSError as e:
            if not (e.errno == errno.EEXIST and os.path.isdir(config_dir)):
                raise
        shutil.copy(os.path.join(os.path.dirname(__file__), "user_config.yml"), config.config_files[2])
        logger.info("Wrote new config file %s with default values", config.config_files[2])
        config = AegeaConfig(__name__, use_yaml=True, save_on_exit=False)

    parser = argparse.ArgumentParser(
        description="{}: {}".format(BOLD() + RED() + __name__.capitalize() + ENDC(), fill(__doc__.strip())),
        formatter_class=AegeaHelpFormatter
    )
    parser.add_argument("--version", action="version", version="%(prog)s {}\n{} {}\n{}".format(
        __version__,
        platform.python_implementation(),
        platform.python_version(),
        platform.platform()
    ))

    def help(args):
        parser.print_help()
    register_parser(help) 
Example #15
Source File: toolchain.py    From calmjs with GNU General Public License v2.0 6 votes vote down vote up
def compile_bundle_entry(self, spec, entry):
        """
        Handler for each entry for the bundle method of the compile
        process.  This copies the source file or directory into the
        build directory.
        """

        modname, source, target, modpath = entry
        bundled_modpath = {modname: modpath}
        bundled_target = {modname: target}
        export_module_name = []
        if isfile(source):
            export_module_name.append(modname)
            copy_target = join(spec[BUILD_DIR], target)
            if not exists(dirname(copy_target)):
                makedirs(dirname(copy_target))
            shutil.copy(source, copy_target)
        elif isdir(source):
            copy_target = join(spec[BUILD_DIR], modname)
            shutil.copytree(source, copy_target)

        return bundled_modpath, bundled_target, export_module_name 
Example #16
Source File: testset.py    From L3C-PyTorch with GNU General Public License v3.0 6 votes vote down vote up
def main():
    p = argparse.ArgumentParser('Copy deterministic subset of images to another directory.')
    p.add_argument('root_dir')
    p.add_argument('max_imgs', type=int)
    p.add_argument('out_dir')
    p.add_argument('--dry')
    p.add_argument('--verbose', '-v', action='store_true')
    flags = p.parse_args()
    os.makedirs(flags.out_dir, exist_ok=True)

    t = Testset(flags.root_dir, flags.max_imgs)

    def cp(p1, p2):
        if os.path.isfile(p2):
            print('Exists, skipping: {}'.format(p2))
            return
        if flags.verbose:
            print('cp {} -> {}'.format(p1, p2))
        if not flags.dry:
            shutil.copy(p1, p2)

    for p in t.iter_orig_paths():
        cp(p, os.path.join(flags.out_dir, os.path.basename(p))) 
Example #17
Source File: other_codecs.py    From imgcomp-cvpr with GNU General Public License v3.0 6 votes vote down vote up
def gen_bpg(in_images, out_dir, qs, first_n):
    if '*' not in in_images:
        in_images = os.path.join(in_images, '*.png')
    images = sorted(glob.glob(in_images))[:first_n]
    assert len(images) > 0, 'No matches for {}'.format(in_images)
    for img in images:
        if 'tmp' in img:
            print('Skipping {}'.format(img))
            continue
        shutil.copy(img, os.path.join(out_dir, os.path.basename(img).replace('.png', '_base.png')))
        print(os.path.basename(img))
        for q in qs:
            with remove_file_after(bpg_compress(img, q=q, tmp_dir=out_dir, chroma_fmt='422')) as p:
                bpp = bpp_of_bpg_image(p)
                out_png = decode_bpg_to_png(p)
                out_name = os.path.basename(img).replace('.png', '_{:.4f}.png'.format(bpp))
                out_p = os.path.join(out_dir, out_name)
                print('-> {:.3f}: {}'.format(bpp, out_name))
                os.rename(out_png, out_p) 
Example #18
Source File: net.py    From exposure with MIT License 6 votes vote down vote up
def draw_value_reward_score(self, img, value, reward, score):
    img = img.copy()
    # Average with 0.5 for semi-transparent background
    img[:14] = img[:14] * 0.5 + 0.25
    img[50:] = img[50:] * 0.5 + 0.25
    if self.cfg.gan == 'ls':
      red = -np.tanh(float(score) / 1) * 0.5 + 0.5
    else:
      red = -np.tanh(float(score) / 10.0) * 0.5 + 0.5
    top = '%+.2f %+.2f' % (value, reward)
    cv2.putText(img, top, (3, 7), cv2.FONT_HERSHEY_SIMPLEX, 0.25,
                (1.0, 1.0 - red, 1.0 - red))
    score = '%+.3f' % score
    cv2.putText(img, score, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
                (1.0, 1.0 - red, 1.0 - red))
    return img 
Example #19
Source File: datasetGenerator.py    From Traffic_sign_detection_YOLO with MIT License 6 votes vote down vote up
def generateDatasetFiles(self):
        with open(os.path.join(self.datasetPath,'frameAnnotations.csv')) as csvfile:
            anotations_list = csvfile.readlines()
            # print(anotations_list)
            anotations_list.pop(0)
            for sample in anotations_list:
                sample = sample.split(';')
                # print(sample)
                self.labels.add(sample[1])
                self.generateXML(file=sample[0],
                    label=sample[1],
                    _bndbox={
                        "xmin": sample[2],
                        "ymin": sample[3],
                        "xmax": sample[4],
                        "ymax": sample[5]})
                shutil.copy(
                    os.path.join(self.datasetPath,sample[0]),
                    self.imgPath)
                self.FileSequence.append(sample[0])
        
                # break 
Example #20
Source File: pickle_to_tex.py    From exposure with MIT License 6 votes vote down vote up
def process(filename, id, src):
  pkl_fn = os.path.join(src, filename)
  debug_info_list = pickle.load(open(pkl_fn, 'rb'))
  filename = filename[:-10]
  target_dir = 'export/{}'.format(id)
  os.makedirs(target_dir, exist_ok=True)
  for i in range(NUM_STEPS - 1):
    shutil.copy(os.path.join(src, filename + '.intermediate%02d.png' % i),
                os.path.join(target_dir, 'step%d.png' % (i + 1)))

  shutil.copy(os.path.join(src, filename + '.retouched.png'), os.path.join(target_dir, 'final.png'))
  shutil.copy(os.path.join(src, filename + '.linear.png'), os.path.join(target_dir, 'input.png'))

  with open(target_dir + '/steps.tex', 'w') as f:
    for i in range(NUM_STEPS):
      debug_info = debug_info_list[i]
      print(
          visualize_step(debug_info, 'agent%d' % (i + 1), (4, i * -3)),
          end=' ',
          file=f) 
Example #21
Source File: evaluate.py    From exposure with MIT License 6 votes vote down vote up
def evaluate():
  if len(sys.argv) < 4:
    print(
        "Usage: python3 evaluate.py [config suffix] [model name] [image files name1] [image files name2] ..."
    )
    exit(-1)
  if len(sys.argv) == 4:
    print(
        " Note: Process a single image at a time may be inefficient - try multiple inputs)"
    )
  print("(TODO: batch processing when images have the same resolution)")
  print()
  print("Initializing...")
  config_name = sys.argv[1]
  import shutil
  shutil.copy('models/%s/%s/scripts/config_%s.py' %
              (config_name, sys.argv[2], config_name), 'config_tmp.py')
  cfg = load_config('tmp')
  cfg.name = sys.argv[1] + '/' + sys.argv[2]
  net = GAN(cfg, restore=True)
  net.restore(20000)
  spec_files = sys.argv[3:]
  print('processing files {}', spec_files)
  net.eval(spec_files=spec_files, step_by_step=True) 
Example #22
Source File: partial.py    From MySQL-AutoXtraBackup with MIT License 5 votes vote down vote up
def copy_ibd_file_back(path_of_ibd_file, path_to_mysql_database_dir):
        # Copy .ibd file back
        try:
            logger.info("OK: Copying .ibd file back")
            shutil.copy(path_of_ibd_file, path_to_mysql_database_dir)
            return True
        except Exception as err:
            logger.error("FAILED: copy .ibd file back")
            logger.error(err)
            raise RuntimeError("FAILED: copy .ibd file back") 
Example #23
Source File: docs.py    From Telethon with MIT License 5 votes vote down vote up
def _copy_resources(res_dir):
    for dirname, files in [('css', ['docs.light.css', 'docs.dark.css']),
                           ('img', ['arrow.svg'])]:
        dirpath = pathlib.Path(dirname)
        dirpath.mkdir(parents=True, exist_ok=True)
        for file in files:
            shutil.copy(str(res_dir / dirname / file), str(dirpath)) 
Example #24
Source File: setup.py    From simnibs with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        develop.run(self)
        if sys.platform == 'win32':
            for f in glob.glob(os.path.join('simnibs', 'lib', 'win', '*')):
                shutil.copy(
                    f,
                    os.path.join('simnibs', 'cython_code', os.path.basename(f)))
        if sys.platform == 'darwin':
            for f in glob.glob(os.path.join('simnibs', 'lib', 'osx', '*')):
                shutil.copy(
                    f,
                    os.path.join('simnibs', 'cython_code', os.path.basename(f))) 
Example #25
Source File: create_delex_data.py    From ConvLab with MIT License 5 votes vote down vote up
def analyze_dialogue(dialogue, maxlen):
    """Cleaning procedure for all kinds of errors in text and annotation."""
    d = dialogue
    # do all the necessary postprocessing
    if len(d['log']) % 2 != 0:
        #print path
        print('odd # of turns')
        return None  # odd number of turns, wrong dialogue
    d_pp = {}
    d_pp['goal'] = d['goal']  # for now we just copy the goal
    usr_turns = []
    sys_turns = []
    for i in range(len(d['log'])):
        if len(d['log'][i]['text'].split()) > maxlen:
            print('too long')
            return None  # too long sentence, wrong dialogue
        if i % 2 == 0:  # usr turn
            if 'db_pointer' not in d['log'][i]:
                print('no db')
                return None  # no db_pointer, probably 2 usr turns in a row, wrong dialogue
            text = d['log'][i]['text']
            if not is_ascii(text):
                print('not ascii')
                return None
            #d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=True)
            usr_turns.append(d['log'][i])
        else:  # sys turn
            text = d['log'][i]['text']
            if not is_ascii(text):
                print('not ascii')
                return None
            #d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=False)
            belief_summary = get_summary_bstate(d['log'][i]['metadata'])
            d['log'][i]['belief_summary'] = belief_summary
            sys_turns.append(d['log'][i])
    d_pp['usr_log'] = usr_turns
    d_pp['sys_log'] = sys_turns

    return d_pp 
Example #26
Source File: dwdownloader.py    From daily-wallpaper with MIT License 5 votes vote down vote up
def download(url):
    if url.startswith('file://'):
        filename = url[7:]
        if os.path.exists(filename):
            if os.path.exists(comun.POTD):
                md5_old = md5(comun.POTD)
            md5_new = md5(filename)
            if md5_old == md5_new:
                return False
            shutil.copy(filename, comun.POTD)
            return True
        return False
    try:
        r = requests.get(url, stream=True)
        if r.status_code == 200:
            if os.path.exists(comun.POTD):
                md5_old = md5(comun.POTD)
                tempfilename = tempfile.NamedTemporaryFile().name
                with open(tempfilename, 'wb') as f:
                    for chunk in r.iter_content(1024):
                        f.write(chunk)
                md5_new = md5(tempfilename)
                if md5_old == md5_new:
                    os.remove(tempfilename)
                else:
                    os.remove(comun.POTD)
                    shutil.move(tempfilename, comun.POTD)
                    return True
            else:
                with open(comun.POTD, 'wb') as f:
                    for chunk in r.iter_content(1024):
                        f.write(chunk)
                    return True
    except Exception as e:
        print(e)
    return False 
Example #27
Source File: logutil.py    From Depth-Map-Prediction with GNU General Public License v3.0 5 votes vote down vote up
def copy(src, dst=None):
    '''
    Copy a file to the output directory.

    If dst is None, uses basename(src).  Otherwise, dst is the name of the
    file within the current output directory.
    '''
    if dst is None:
        dst = os.path.basename(src)
    dst = filename(dst)
    if os.path.realpath(src) != os.path.realpath(dst):
        shutil.copy(src, dst) 
Example #28
Source File: test_mmbot.py    From MaliciousMacroBot with MIT License 5 votes vote down vote up
def resetTest():
    """
    Resets test filesystem structure back to initial state
    """
    # remove artifacts from past tests, if they exist
    shutil.rmtree(benign_path, ignore_errors=True)
    shutil.rmtree(malicious_path, ignore_errors=True)
    shutil.rmtree(model_path, ignore_errors=True)
    shutil.rmtree(empty_path, ignore_errors=True)

    # make test directories
    try:
        os.mkdir(testdir) # test directory
    except:
        print("{} already exists".format(testdir))
    try:
        os.mkdir(benign_path)  # benign training set
    except:
        print("{} already exists".format(benign_path))
    try:
        os.mkdir(malicious_path)  # malicious training set
    except:
        print("{} already exists".format(benign_path))
    try:
        os.mkdir(model_path)  # where model data is kept
    except:
        print("{} already exists".format(model_path))
    try:
        os.mkdir(empty_path)  # empty directory
    except:
        print("{} already exists".format(empty_path))

    # setup malicious file for training set
    shutil.copy(os.path.join(testdir, sample1), os.path.join(malicious_path, sample1))     
    shutil.copy(os.path.join(testdir, sample2), os.path.join(benign_path, sample2))     
    shutil.copy(os.path.join(testdir, sample3), os.path.join(benign_path, sample3))     
    shutil.copy(os.path.join(testdir, vocab), os.path.join(model_path, vocab)) 
Example #29
Source File: train.py    From monoses with GNU General Public License v3.0 5 votes vote down vote up
def iterative_backtranslation(args):
    root = args.working + '/step8'
    os.mkdir(root)
    config = {('src', 'trg'): args.working + '/step7/src2trg.moses.ini',
              ('trg', 'src'): args.working + '/step7/trg2src.moses.ini'}
    for part in 'src', 'trg':
        bash('head -' + str(args.backtranslation_sentences) +
             ' ' + quote(args.working + '/step1/train.true.' + part) +
             ' > ' + quote(args.tmp + '/train.' + part))
    for it in range(1, args.backtranslation_iter + 1):
        for src, trg in ('src', 'trg'), ('trg', 'src'):
            # TODO Use cube pruning?
            bash(quote(MOSES + '/bin/moses2') +
                 ' -f ' + quote(config[(trg, src)]) + 
                 ' --threads ' + str(args.threads) +
                 ' < ' + quote(args.tmp + '/train.' + trg) +
                 ' > ' + quote(args.tmp + '/train.bt') +
                 ' 2> /dev/null')
            bash(quote(MOSES + '/bin/moses2') +
                 ' -f ' + quote(config[(trg, src)]) + 
                 ' --threads ' + str(args.threads) +
                 ' < ' + quote(args.working + '/step1/dev.true.' + trg) +
                 ' > ' + quote(args.tmp + '/dev.bt') +
                 ' 2> /dev/null')
            train_supervised(args,
                             args.tmp + '/train.bt',
                             args.tmp + '/train.' + trg,
                             args.tmp + '/dev.bt',
                             args.working + '/step1/dev.true.' + trg,
                             args.working + '/step2/' + trg + '.blm',
                             args.lm_order,
                             root + '/' + src + '2' + trg + '-it' + str(it))
            os.remove(args.tmp + '/train.bt')
            os.remove(args.tmp + '/dev.bt')
            config[(src, trg)] = root + '/' + src + '2' + trg + '-it' + str(it) + '/moses.ini'

    shutil.copy(config[('src', 'trg')], args.working + '/src2trg.moses.ini')
    shutil.copy(config[('trg', 'src')], args.working + '/trg2src.moses.ini')
    os.remove(args.tmp + '/train.src')
    os.remove(args.tmp + '/train.trg') 
Example #30
Source File: extractor.py    From firmanal with MIT License 5 votes vote down vote up
def __getstate__(self):
        """
        Eliminate attributes that should not be pickled.
        """
        self_dict = self.__dict__.copy()
        del self_dict["_pool"]
        del self_dict["_list"]
        return self_dict