Пример #1
0
 def end_log_game_action(self, game_name):
     import json
     if game_name not in self._game_info: return
     game = self._game_info.get(game_name)
     if 'actions' in game:
         file_path = os.path.join(self.logdir, 'game_info')
         misc.ensure_dir(file_path)
         file_path = os.path.join(file_path, game_name + '.actions.json')
         misc.save_file(file_path, json.dumps(game['actions']))
     del self._game_info[game_name]
Пример #2
0
def save_structure_to_files(regime):
    '''Parse the metadata to compute a mapping between structures and their corresponding datapoints.'''
    print('Saving structure metadata')
    data_dir = '../data/' + regime + '/'
    data_files = os.listdir(data_dir)
    data_files = [data_dir + data_file for data_file in data_files]
    structure_to_files = {}
    for count, data_file in enumerate(data_files):
        if count % 10000 == 0:
            print(count, '/', len(data_files))
        data = np.load(data_file)
        # TODO Serialize structure instead of using string representation
        structure = parse_structure(data['relation_structure']).to_str()
        if structure not in structure_to_files:
            structure_to_files[structure] = [data_file]
        else:
            structure_to_files[structure].append(data_file)
    if os.path.exists('save_state/' + regime):
        os.mkdir('save_state/' + regime)
    misc.save_file(structure_to_files,
                   'save_state/' + regime + '/structure_to_files.pkl')
    return structure_to_files
Пример #3
0
def save_normalization_stats(regime, batch_size=100):
    '''Compute the mean and standard deviation jointly across all channels.'''
    print('Saving normalization stats')
    data_dir = '../data/' + regime + '/'
    data_files = os.listdir(data_dir)
    data_files = [data_dir + data_file for data_file in data_files]
    train_files = [
        data_file for data_file in data_files if 'train' in data_file
    ]
    loader = torch.utils.data.DataLoader(PreprocessDataset(
        train_files, 'image'),
                                         batch_size=batch_size)
    print('Computing x_mean')
    sum = 0
    n = 0
    count = 0
    for x in loader:
        sum += x.sum()
        n += x.numel()
        count += batch_size
        if count % 100000 == 0:
            print(count, '/', len(train_files))
    x_mean = float(sum / n)
    print('Computing x_sd')
    sum = 0
    n = 0
    count = 0
    for x in loader:
        sum += ((x - x_mean)**2).sum()
        n += x.numel()
        count += batch_size
        if count % 100000 == 0:
            print(count, '/', len(train_files))
    x_sd = float(np.sqrt(sum / n))
    misc.save_file((x_mean, x_sd),
                   'save_state/' + regime + '/normalization_stats.pkl')
    return x_mean, x_sd
Пример #4
0
    def write_wav(self, full_out_file=None):
        """

        Write sound data to a WAV-file.

        Parameters
        ----------
        fullOutFile : string
            Path- and file-name of the outfile. If none is given,
            the user is asked interactively to choose a folder/name
            for the outfile.

        Returns
        -------
        None :
            

        Examples
        --------
        >>> mySound = Sound()
        >>> mySound.read_sound('test.wav')
        >>> mySound.write_wav()

        """

        if full_out_file is None:

            (out_file,
             out_dir) = misc.save_file(FilterSpec='*.wav',
                                       DialogTitle='Write sound to ...',
                                       DefaultName='')
            full_out_file = os.path.join(out_dir, out_file)
            if full_out_file is None:
                print('Output discarded.')
                return 0
        else:
            (out_file, out_dir) = os.path.split(full_out_file)

        write(str(full_out_file), int(self.rate), self.data)
        print('Sounddata written to ' + out_file + ', with a sample rate of ' +
              str(self.rate))
        print('OutDir: ' + out_dir)

        return full_out_file
Пример #5
0
 def save_train_info(self):
     import json
     train_info_path = os.path.join(self.logdir, 'train_info.txt')
     misc.save_file(train_info_path, json.dumps(self.train_info))