0todd0000 / power1d

GNU General Public License v3.0
3 stars 1 forks source link

Error when running the sample size code #5

Closed zzzYYYYZ closed 8 months ago

zzzYYYYZ commented 8 months ago

Dr. Pataky, Nice to meet you. When I ran the sample size calculation code, an error happened when aiming at some signal location (q). I have no idea how to solve it. I would appreciate it if you could help me.

############################################################ The code is

(0) Create geometry and noise models:

JA, JB, Q = 55, 55, 101 # J(X) = No of subjects; Q= time points;

sample I

xata01 = pd.read_csv("D:\Python\learnsample\data1.csv", sep=',', header=None) m_data1 = np.array(xata01.values)[0] signal1 = power1d.geom.Continuum1D(m_data1)

Sample II

xata02 = pd.read_csv("D:\Python\learnsample\data2.csv", sep=',', header=None) m_data2 = np.array(xata02.values)[0] signal2 = power1d.geom.Continuum1D(m_data2)

Baseline

baseline = power1d.geom.Continuum1D(m_data1)

noise

data03 = pd.read_csv("D:\Python\learnsample\allstd.csv", sep=',', header=None) std003 = np.array(data03.values)[0] noise3 = power1d.noise.SmoothGaussian(JB, Q, mu=0, sigma=std003[0], fwhm=28)

Create data sample models:

modelA0 = power1d.models.DataSample(baseline, signal1, noise3, J=JA) # null A modelB0 = power1d.models.DataSample(baseline, signal1, noise3, J=JB) # null B modelA1 = power1d.models.DataSample(baseline, signal1, noise3, J=JA) # alternative A modelB1 = power1d.models.DataSample(baseline, signal2, noise3, J=JB) # alternative B

iteratively simulate for a range of sample sizes:

np.random.seed(0) #seed the random number generator JJ = [3, 4, 5, 6, 7, 8, 9, 10] #sample sizes teststat = power1d.stats.t_2sample_fn(JA, JB) expmodel0 = power1d.models.Experiment([modelA0, modelB0], teststat) #null expmodel1 = power1d.models.Experiment([modelA1, modelB1], teststat) #alternative sim = power1d.ExperimentSimulator(expmodel0, expmodel1) q = 80 # signal location results = sim.sample_size(power=0.8, alpha=0.05, niter0=200, niter=2000, coi=dict(q=q, r=3))

retrieve estimated sample size:

n = results['nstar'] print(f'Estimate sample size = {n}')

plot:

plt.figure() ax = plt.axes() ax.plot( results['n'], results['p'], 'o-') ax.axhline( results['target_power'] , color='k', linestyle='--') ax.set_xlabel('Sample size', size=14) ax.set_ylabel('Power', size=14) plt.show()

################################################### the error showed D:\Python\Python312\python.exe "D:\Python\project\pythonProject\samplesize-two sample.py" Traceback (most recent call last): File "D:\Python\project\pythonProject\samplesize-two sample.py", line 48, in results = sim.sample_size(power=0.8, alpha=0.05, niter0=200, niter=2000, coi=dict(q=q, r=3)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python\Python312\Lib\site-packages\power1d\models.py", line 711, in sample_size ind = np.argwhere( ps > power ).ravel()[0]


IndexError: index 0 is out of bounds for axis 0 with size 0

Process finished with exit code 1

######################################################
0todd0000 commented 8 months ago

Hello! And Happy New Year!

Without access to the CSV files it is difficult to find the source of the problem.

Can you please tell me: the shapes of the variables: m_data1, m_data2 and std003?

zzzYYYYZ commented 8 months ago

Dr. Pataky, thank you for your reminding. After checking the shapes, i find there is a problem with the shape of std003. The problem is solved.