|
import os |
|
from tasks.utils.event_codec import Event, EventRange |
|
from tasks.utils import event_codec |
|
|
|
ec = event_codec.Codec( |
|
max_shift_steps=1000, |
|
steps_per_second=100, |
|
event_ranges=[ |
|
EventRange('pitch', min_value=0, max_value=127), |
|
EventRange('velocity', min_value=0, max_value=1), |
|
EventRange('tie', min_value=0, max_value=0), |
|
EventRange('program', min_value=0, max_value=127), |
|
EventRange('drum', min_value=0, max_value=127), |
|
], |
|
) |
|
|
|
events = [ |
|
Event(type='shift', value=0), |
|
Event(type='shift', value=1), |
|
Event(type='shift', value=1000), |
|
Event(type='pitch', value=0), |
|
Event(type='pitch', value=60), |
|
Event(type='pitch', value=127), |
|
Event(type='velocity', value=0), |
|
Event(type='velocity', value=1), |
|
Event(type='tie', value=0), |
|
Event(type='program', value=0), |
|
Event(type='program', value=127), |
|
Event(type='drum', value=0), |
|
Event(type='drum', value=127), |
|
] |
|
|
|
events = events * 100 |
|
tokens = [ec.encode_event(e) for e in events] |
|
tokens = np.array(tokens, dtype=np.int16) |
|
|
|
import csv |
|
|
|
with open('events.csv', 'w', newline='') as file: |
|
writer = csv.writer(file) |
|
for event in events: |
|
writer.writerow([event.type, event.value]) |
|
|
|
|
|
with open('events.csv', 'r') as file: |
|
reader = csv.reader(file) |
|
events2 = [Event(row[0], int(row[1])) for row in reader] |
|
|
|
|
|
import json |
|
|
|
with open('events.json', 'w') as file: |
|
json.dump([event.__dict__ for event in events], file) |
|
|
|
|
|
with open('events.json', 'r') as file: |
|
events = [Event(**event_dict) for event_dict in json.load(file)] |
|
|
|
|
|
|
|
|
|
"""----------------------------""" |
|
|
|
import numpy as np |
|
np.save('tokens.npy', tokens) |
|
|
|
def t_npy(): |
|
t = np.load('tokens.npy', allow_pickle=True) |
|
|
|
os.makedirs('temp', exist_ok=True) |
|
for i in range(2400): |
|
np.save(f'temp/tokens{i}.npy', tokens) |
|
|
|
def t_npy2400(): |
|
for i in range(2400): |
|
t = np.load(f'temp/tokens{i}.npy') |
|
def t_npy2400_take200(): |
|
for i in range(200): |
|
t = np.load(f'temp/tokens{i}.npy') |
|
|
|
import shutil |
|
shutil.rmtree('temp', ignore_errors=True) |
|
|
|
|
|
data = dict() |
|
for i in range(2400): |
|
data[f'arr{i}'] = tokens.copy() |
|
np.save(f'tokens_2400x.npy', data) |
|
def t_npy2400single(): |
|
t = np.load('tokens_2400x.npy', allow_pickle=True).item() |
|
|
|
def t_mmap2400single(): |
|
t = np.load('tokens_2400x.npy', mmap_mode='r') |
|
|
|
|
|
np.savez('tokens.npz', arr0=tokens) |
|
def t_npz(): |
|
npz_file = np.load('tokens.npz') |
|
tt = npz_file['arr0'] |
|
|
|
data = dict() |
|
for i in range(2400): |
|
data[f'arr{i}'] = tokens |
|
np.savez('tokens.npz', **data ) |
|
def t_npz2400(): |
|
npz_file = np.load('tokens.npz') |
|
for i in range(2400): |
|
tt = npz_file[f'arr{i}'] |
|
|
|
def t_npz2400_take200(): |
|
npz_file = np.load('tokens.npz') |
|
|
|
for i in range(200): |
|
tt = npz_file[f'arr{i}'] |
|
|
|
|
|
|
|
with open('tokens.txt', 'w') as file: |
|
file.write(' '.join(map(str, tokens))) |
|
|
|
def t_txt(): |
|
|
|
with open('tokens.txt', 'r') as file: |
|
t = list(map(int, file.read().split())) |
|
t = np.array(t) |
|
|
|
|
|
|
|
with open('tokens.csv', 'w', newline='') as file: |
|
writer = csv.writer(file) |
|
writer.writerow(tokens) |
|
|
|
def t_csv(): |
|
|
|
with open('tokens.csv', 'r') as file: |
|
reader = csv.reader(file) |
|
t = list(map(int, next(reader))) |
|
t = np.array(t) |
|
|
|
|
|
|
|
with open('tokens.json', 'w') as file: |
|
json.dump(tokens, file) |
|
|
|
def t_json(): |
|
|
|
with open('tokens.json', 'r') as file: |
|
t = json.load(file) |
|
t = np.array(t) |
|
|
|
with open('tokens_2400x.json', 'w') as file: |
|
json.dump(data, file) |
|
|
|
def t_json2400single(): |
|
|
|
with open('tokens_2400x.json', 'r') as file: |
|
t = json.load(file) |
|
|
|
def t_mmap(): |
|
t = np.load('tokens.npy', mmap_mode='r') |
|
|
|
|
|
|
|
|
|
|
|
|
|
np.savetxt('tokens.ntxt', tokens) |
|
def t_ntxt(): |
|
t = np.loadtxt('tokens.ntxt').astype(np.int32) |
|
|
|
%timeit t_npz() |
|
%timeit t_mmap() |
|
%timeit t_npy() |
|
%timeit t_txt() |
|
%timeit t_csv() |
|
%timeit t_json() |
|
%timeit t_ntxt() |
|
|
|
%timeit t_npy2400() |
|
%timeit t_npz2400() |
|
|
|
%timeit t_npy2400_take200() |
|
%timeit t_npz2400_take200() |
|
%timeit t_npy2400single() |
|
%timeit t_mmap2400single() |
|
%timeit t_json2400single() |
|
|