Organised matrix test stimulus generation
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules")
|
||||
|
||||
import argparse
|
||||
import os
|
||||
@@ -193,12 +195,12 @@ def gen_noise(OutDir, b, fs, s_rms):
|
||||
x = np.random.randn(int(fs*60.*10.))
|
||||
x /= x.max()
|
||||
noiseDir = os.path.join(OutDir, 'wav')
|
||||
noiseDir = os.path.join(OutDir, 'rms')
|
||||
noiseRMSDir = os.path.join(OutDir, 'rms')
|
||||
dir_must_exist(noiseDir)
|
||||
noiseDir = os.path.join(noiseDir, 'noise')
|
||||
dir_must_exist(noiseDir)
|
||||
y = block_lfilter_wav(b, [1.0], x, os.path.join(noiseDir, 'noise.wav'), 65538, 44100)
|
||||
noise_rms_path = os.path.join(noiseDir, 'noise_rms.npy')
|
||||
noise_rms_path = os.path.join(noiseRMSDir, 'noise_rms.npy')
|
||||
rms = np.sqrt(np.mean(y**2))
|
||||
np.save(noise_rms_path, rms)
|
||||
return y
|
||||
@@ -233,7 +235,7 @@ if __name__ == "__main__":
|
||||
'training TRF decoder by concatenating '
|
||||
'matrix test materials')
|
||||
parser.add_argument('--MatrixDir', type=PathType(exists=True, type='dir'),
|
||||
default='./speech_components',
|
||||
default='../speech_components',
|
||||
help='Matrix test speech data location')
|
||||
parser.add_argument('--OutDir', type=PathType(exists=None, type='dir'),
|
||||
default='./stimulus', help='Output directory')
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
|
||||
import argparse
|
||||
import os
|
||||
@@ -128,12 +130,12 @@ if __name__ == "__main__":
|
||||
'training TRF decoder by concatenating '
|
||||
'matrix test materials')
|
||||
parser.add_argument('--MatrixDir', type=PathType(exists=True, type='dir'),
|
||||
default='./speech_components',
|
||||
default='../speech_components',
|
||||
help='Matrix test speech data location')
|
||||
parser.add_argument('--OutDir', type=PathType(exists=None, type='dir'),
|
||||
default='./stimulus', help='Output directory')
|
||||
parser.add_argument('--ListDir', type=PathType(exists=None, type='dir'),
|
||||
default='./lists', help='Output directory')
|
||||
default='../lists', help='Output directory')
|
||||
|
||||
args = {k:v for k,v in vars(parser.parse_args()).items() if v is not None}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
./gen_noise.py --CalcRMS
|
||||
./gen_stim.py
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
cd ./behavioural_stim
|
||||
./generate_behavioural_stimulus.sh
|
||||
cd ../short_concat_stim
|
||||
./short_concat_stim.py
|
||||
cd ../long_concat_stim
|
||||
./generate_long_deconder_stimulus.sh
|
||||
+24
-10
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
|
||||
import argparse
|
||||
import os
|
||||
@@ -51,34 +53,44 @@ def globDir(directory, pattern):
|
||||
filepaths.append(item)
|
||||
return filepaths
|
||||
|
||||
def concatenateStimuli(MatrixDir, OutDir, Length):
|
||||
def concatenateStimuli(MatrixDir, OutDir, Length, n):
|
||||
# Get matrix wav file paths
|
||||
wavFiles = globDir(MatrixDir, '*.wav')
|
||||
wavFiles = natsorted(wavFiles)
|
||||
totalSize = 0
|
||||
y = []
|
||||
i = 0
|
||||
for wav in wavFiles:
|
||||
if i == n:
|
||||
break
|
||||
wavObj = PySndfile(wav)
|
||||
fs = wavObj.samplerate()
|
||||
size = wavObj.frames()
|
||||
totalSize += size
|
||||
totalSize += int(0.1*fs)
|
||||
if (totalSize/fs) > Length:
|
||||
break
|
||||
y = np.zeros(totalSize)
|
||||
y.append(np.zeros(totalSize))
|
||||
i += 1
|
||||
totalSize = 0
|
||||
|
||||
writePtr = 0
|
||||
i = 0
|
||||
for wav in wavFiles:
|
||||
if writePtr >= y.size:
|
||||
if writePtr >= y[i].size:
|
||||
i += 1
|
||||
writePtr = 0
|
||||
if i == n:
|
||||
break
|
||||
x, fs, encStr, fmtStr = sndio.read(wav, return_format=True)
|
||||
threeMs = int(0.1*fs)
|
||||
silence = np.zeros(threeMs)
|
||||
chunk = np.append(x, silence)
|
||||
y[writePtr:writePtr + chunk.size] = chunk
|
||||
y[i][writePtr:writePtr + chunk.size] = chunk
|
||||
|
||||
writePtr += chunk.size
|
||||
|
||||
pysndfile.sndio.write(os.path.join(OutDir, 'decoder_stim.wav'), y, format=fmtStr, enc=encStr)
|
||||
for ind, data in enumerate(y):
|
||||
pysndfile.sndio.write(os.path.join(OutDir, 'stim_{}.wav'.format(ind)), data, format=fmtStr, enc=encStr)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Create commandline interface
|
||||
@@ -86,12 +98,14 @@ if __name__ == "__main__":
|
||||
'training TRF decoder by concatenating '
|
||||
'matrix test materials')
|
||||
parser.add_argument('--MatrixDir', type=PathType(exists=True, type='dir'),
|
||||
default='./speech_components',
|
||||
default='./out/parts',
|
||||
help='Matrix test speech data location')
|
||||
parser.add_argument('--OutDir', type=PathType(exists=None, type='dir'),
|
||||
default='./out_concat', help='Output directory')
|
||||
parser.add_argument('--Length', type=int, default=60,
|
||||
help='Concatenated length of trials in seconds')
|
||||
default='./out/stim', help='Output directory')
|
||||
parser.add_argument('--Length', type=int, default=900,
|
||||
help='Length of each concatenated trial in seconds')
|
||||
parser.add_argument('-n', type=int, default=4,
|
||||
help='Number of trials to generate')
|
||||
args = {k:v for k,v in vars(parser.parse_args()).items() if v is not None}
|
||||
|
||||
# Generate output directory if it doesn't exist
|
||||
+26
-21
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
|
||||
import argparse
|
||||
import os
|
||||
@@ -70,7 +72,6 @@ def generateTrialInds(n=1):
|
||||
Indexes are generated randomly without replacement, ensuring no duplicate
|
||||
identical samples are generated
|
||||
'''
|
||||
pdb.set_trace()
|
||||
choice = np.random.choice(100000, n, replace=False)
|
||||
indexes = np.zeros((n, 5), dtype=int)
|
||||
for ind, c in enumerate(choice):
|
||||
@@ -86,8 +87,6 @@ def gen2(MatrixDir, OutDir, indexes):
|
||||
for ind in sentenceList:
|
||||
y, wavInfo, partnames = synthesizeTrial(wavFileMatrix, ind)
|
||||
|
||||
files.append(fileName)
|
||||
pdb.set_trace()
|
||||
|
||||
|
||||
|
||||
@@ -279,13 +278,14 @@ if __name__ == "__main__":
|
||||
'training TRF decoder by concatenating '
|
||||
'matrix test materials')
|
||||
parser.add_argument('--MatrixDir', type=PathType(exists=True, type='dir'),
|
||||
default='./speech_components',
|
||||
default='../speech_components',
|
||||
help='Matrix test speech data location')
|
||||
parser.add_argument('--OutDir', type=PathType(exists=None, type='dir'),
|
||||
default='./out_trials', help='Output directory')
|
||||
parser.add_argument('--Length', type=int, default=60,
|
||||
default='./out', help='Output directory')
|
||||
parser.add_argument('--Length', type=int, default=3600,
|
||||
help='Concatenated length of trials in seconds')
|
||||
args = {k:v for k,v in vars(parser.parse_args()).items() if v is not None}
|
||||
dir_must_exist(args['OutDir'])
|
||||
|
||||
# Check directory for storing generated noise exists
|
||||
#noiseDir = os.path.join(args['OutDir'], 'noise')
|
||||
@@ -297,32 +297,37 @@ if __name__ == "__main__":
|
||||
#if os.path.exists(args['OutDir']):
|
||||
# shutil.rmtree(args['OutDir'])
|
||||
# os.makedirs(args['OutDir'])
|
||||
## Generate output directory if it doesn't exist
|
||||
#prepareOutDir(args['OutDir'])
|
||||
|
||||
# Generate output directory if it doesn't exist
|
||||
outDir = args.pop("OutDir")
|
||||
prepareOutDir(outDir)
|
||||
partsDir = os.path.join(outDir, 'parts')
|
||||
dir_must_exist(partsDir)
|
||||
|
||||
|
||||
# Generate audio stimulus from arguments provided on command line
|
||||
|
||||
# Randomly generate word choices for each trial
|
||||
# indexes = generateTrialInds(100000)
|
||||
|
||||
x = np.repeat(np.arange(10), 5)
|
||||
x = x.reshape(10, 5)
|
||||
|
||||
y = np.zeros((50, 10, 5), dtype=int)
|
||||
|
||||
# 50 lists
|
||||
for i in range(50):
|
||||
x[:, 1] = np.roll(x[:, 1], 1)
|
||||
x[:, 2] = np.roll(x[:, 2], 2)
|
||||
x[:, 3] = np.roll(x[:, 3], 3)
|
||||
x[:, 4] = np.roll(x[:, 4], 4)
|
||||
y[i] = x.copy()
|
||||
#indexes =
|
||||
gen2(args['MatrixDir'], args['OutDir'], y)
|
||||
generateAudioStimulus(**args)
|
||||
# # 50 lists
|
||||
# for i in range(50):
|
||||
# x[:, 1] = np.roll(x[:, 1], 1)
|
||||
# x[:, 2] = np.roll(x[:, 2], 2)
|
||||
# x[:, 3] = np.roll(x[:, 3], 3)
|
||||
# x[:, 4] = np.roll(x[:, 4], 4)
|
||||
# y[i] = x.copy()
|
||||
# #indexes =
|
||||
# #gen2(args['MatrixDir'], args['OutDir'], y)
|
||||
# y = y.reshape(500, 5)
|
||||
# y = np.vstack((y, y, y, y))
|
||||
indexes = generateTrialInds(100000)
|
||||
generateAudioStimulus(**args, OutDir=partsDir, indexes=indexes)
|
||||
|
||||
generateNoiseFromSentences(args['OutDir'], noiseDir)
|
||||
#generateNoiseFromSentences(args['OutDir'], noiseDir)
|
||||
#generateDecoderAudio(args['OutDir'], noiseDir, decoderDir)
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
./gen_long_stim.py
|
||||
./concatenate_stimuli.py
|
||||
+15
-3
@@ -1,5 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
|
||||
import os
|
||||
from filesystem import globDir
|
||||
import argparse
|
||||
@@ -43,9 +46,18 @@ def gen_trigger(idx, freq, length, fs):
|
||||
return trigger
|
||||
|
||||
def main():
|
||||
base_dir = "./stimulus/wav/sentence-lists/"
|
||||
out_dir = "./short_concat_stim/"
|
||||
noise_filepath = "./stimulus/wav/noise/noise.wav"
|
||||
stim_dir = "../behavioural_stim/stimulus"
|
||||
wav_dir = "../behavioural_stim/stimulus/wav"
|
||||
base_dir = "../behavioural_stim/stimulus/wav/sentence-lists/"
|
||||
noise_dir = "../behavioural_stim/stimulus/wav/noise/"
|
||||
out_dir = "./out"
|
||||
dir_must_exist(base_dir)
|
||||
dir_must_exist(out_dir)
|
||||
dir_must_exist(wav_dir)
|
||||
dir_must_exist(noise_dir)
|
||||
|
||||
noise_filepath = "../behavioural_stim/stimulus/wav/noise/noise.wav"
|
||||
|
||||
folders = os.listdir(base_dir)
|
||||
folders = natsorted(folders)[1:15]
|
||||
folders = list(zip(folders[::2], folders[1::2]))
|
||||
@@ -0,0 +1,19 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
from filesystem import globDir
|
||||
from pysndfile import sndio
|
||||
import pdb
|
||||
import os
|
||||
|
||||
def main():
|
||||
wavs = globDir('./', 'stim.wav')
|
||||
for wav in wavs:
|
||||
x, fs, enc, fmt = sndio.read(wav, return_format=True)
|
||||
y = x[:, :2]
|
||||
head, tail = os.path.splitext(wav)
|
||||
out_filepath = "{0}_old{1}".format(head, tail)
|
||||
os.rename(wav, out_filepath)
|
||||
sndio.write(wav, y, rate=fs, format=fmt, enc=enc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,17 @@
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules/")
|
||||
from filesystem import globDir
|
||||
import pdb
|
||||
import os
|
||||
|
||||
def main():
|
||||
wavs = globDir('./', 'stim.wav')
|
||||
for wav in wavs:
|
||||
out_filepath = "{0}_old{1}".format(head, tail)
|
||||
out_temppath = "{0}_temp{1}".format(head, tail)
|
||||
os.rename(wav, out_temppath)
|
||||
os.rename(out_filepath, wav)
|
||||
os.rename(out_temppath, out_filepath)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user