%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22numpy%3E%3D2.0%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Code%20structure%20for%20data%20analysis%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20folder%20with%20twelve%20files%20is%20manageable%20by%20hand.%20A%20folder%20with%20twelve%20thousand%20is%20an%20invitation%20to%20lose%20a%20trial.%20Here%20we%20turn%20subject%2C%20condition%2C%20and%20repetition%20into%20explicit%20indices%2C%20then%20keep%20the%20same%20indices%20when%20a%20file%20is%20absent.%20You%20need%20Python%20loops%20and%20NumPy%20arrays.%0A%0A%20%20%20%20Read%20the%20examples%20in%20order%2C%20predict%20each%20result%2C%20and%20then%20run%20the%20cell.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20checkpoint%20questions%20and%20independent%20Problems%3B%20their%20panels%20contain%20final%20answers%20only.%0A%0A%20%20%20%20**Challenge%200.**%20Two%20subjects%20each%20complete%20two%20conditions%20and%20three%20trials.%20If%20one%20file%20is%20missing%2C%20should%20the%20result%20have%20eleven%20rows%20or%20twelve%3F%20What%20information%20would%20either%20choice%20preserve%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20NumPy%20creates%20small%20synthetic%20text%20files%20and%20summarizes%20their%20columns.%20All%20demonstration%20files%20are%20written%20to%20a%20temporary%20directory%2C%20not%20to%20the%20repository%20data%20folder.%20This%20lesson%20intentionally%20deletes%20one%20demonstration%20file%20to%20show%20missing-data%20handling.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Sometimes%20data%20from%20experiments%20are%20stored%20in%20different%20files%20where%20each%20file%20contains%20data%20for%20different%20subjects%2C%20trials%2C%20conditions%2C%20etc.%20This%20text%20presents%20a%20common%20and%20simple%20solution%20to%20write%20a%20code%20to%20analyze%20such%20data.%3Cbr%3E%0A%20%20%20%20The%20basic%20idea%20is%20that%20the%20name%20of%20the%20file%20is%20created%20in%20a%20structured%20way%20and%20you%20can%20use%20that%20to%20run%20a%20sequence%20of%20procedures%20inside%20one%20or%20more%20nested%20loops.%3Cbr%3E%0A%20%20%20%20For%20instance%2C%20consider%20that%20the%20two%20first%20letters%20of%20the%20filename%20encode%20the%20initials%20of%20the%20subject's%20name%2C%20the%20next%20two%20letters%20the%20different%20conditions%2C%20and%20the%20last%20two%20characters%20the%20trial%20number.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20subjects%20%20%20%3D%20%5B'AA'%2C%20'AB'%5D%0A%20%20%20%20conditions%20%3D%20%5B'c1'%2C%20'c2'%5D%0A%20%20%20%20trials%20%20%20%20%20%3D%20%5B'01'%2C%20'02'%2C%20'03'%5D%0A%20%20%20%20return%20conditions%2C%20subjects%2C%20trials%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20could%20open%20and%20process%20these%20files%20with%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20for%20_subject%20in%20subjects%3A%0A%20%20%20%20%20%20%20%20for%20_condition%20in%20conditions%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_trial%20in%20trials%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20_subject%20%2B%20_condition%20%2B%20_trial%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(_filename)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20problem%20with%20this%20code%20is%20that%20if%20one%20one%20more%20files%20are%20missing%20or%20corrupted%20(which%20is%20typical)%2C%20it%20will%20break.%20A%20solution%20is%20to%20read%20the%20file%20inside%20a%20%60try%60%20statement.%20The%20%60try...except%60%20handles%20exceptions%20such%20as%20a%20failure%20in%20reading%20a%20file%20and%20then%20we%20can%20use%20a%20%60continue%60%20statement%20to%20skip%20each%20failed%20iteration%20in%20the%20inner%20loop.%3Cbr%3E%0A%20%20%20%20Let's%20create%20some%20files%20and%20implement%20this%20idea.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20Decode%20%60ABc203.txt%60.%20How%20many%20filenames%20do%20the%20nested%20loops%20generate%2C%20and%20which%20index%20changes%20fastest%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20The%20subject%20is%20AB%2C%20condition%20c2%2C%20trial%2003.%20There%20are%20%242%5Ctimes2%5Ctimes3%3D12%24%20combinations.%20The%20innermost%20trial%20loop%20changes%20fastest%3B%20this%20order%20determines%20how%20we%20flatten%20results%20later.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Read%20and%20save%20files%0A%0A%20%20%20%20If%20the%20data%20is%20in%20text%20(%5BASCII%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FASCII))%20format%2C%20it's%20easier%20to%20read%20the%20file%20with%20the%20%5B%60Numpy%60%5D(http%3A%2F%2Fwww.numpy.org%2F)%20function%20%5B%60loadtxt%60%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Fgenerated%2Fnumpy.loadtxt.html)%20or%20with%20the%20%5B%60pandas%60%5D(http%3A%2F%2Fpandas.pydata.org%2F)%20function%20%5B%60read_csv%60%5D(http%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fstable%2Fgenerated%2Fpandas.io.parsers.read_csv.html).%20Both%20functions%20behave%20similarly%3B%20they%20can%20skip%20a%20certain%20number%20of%20first%20rows%2C%20can%20read%20files%20with%20different%20column%20separators%2C%20read%20numbers%20and%20letters%2C%20etc.%20%60read_csv%60%20tends%20to%20be%20faster%20but%20it%20returns%20a%20%60pandas%60%20%5B%60DataFrame%60%5D(http%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fdev%2Fgenerated%2Fpandas.DataFrame.html)%20object%2C%20which%20might%20not%20be%20useful%20if%20you%20are%20not%20into%20%60pandas%60%20(but%20you%20should%20be).%0A%0A%20%20%20%20To%20save%20data%20to%20a%20file%2C%20we%20can%20use%20the%20counterpart%20functions%20%60savetxt%60%20and%20%60to_csv%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20tempfile%20as%20_tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%20as%20_Path%0A%20%20%20%20path%20%3D%20_tempfile.mkdtemp(prefix%3D'BMC-CodeStructure-')%20%2B%20'%2F'%0A%20%20%20%20extension%20%3D%20'.txt'%0A%20%20%20%20for%20_subject%20in%20subjects%3A%0A%20%20%20%20%20%20%20%20for%20_condition%20in%20conditions%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_trial%20in%20trials%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20path%20%2B%20_subject%20%2B%20_condition%20%2B%20_trial%20%2B%20extension%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_data%20%3D%20np.random.randn(5%2C%203)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_header%20%3D%20'Col%20A%5CtCol%20B%5CtCol%20C'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.savetxt(_filename%2C%20_data%2C%20fmt%3D'%25g'%2C%20delimiter%3D'%5Ct'%2C%20header%3D_header%2C%20comments%3D'')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print('File'%2C%20_filename%2C%20'saved')%0A%20%20%20%20return%20extension%2C%20np%2C%20path%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20files%20are%20saved%20in%20a%20temporary%20folder%2C%20so%20that%20the%20notebook%20also%20runs%20where%20it%20cannot%20write%20next%20to%20itself%20(e.g.%2C%20on%20molab).%20To%20save%20them%20somewhere%20else%2C%20change%20%60path%60%3B%20for%20instance%2C%20'.%2F..%2Fdata%2F'%20moves%20up%20one%20directory%20relatively%20to%20the%20current%20directory%20(see%20the%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCd_(command)%22%3Ecd%20(command)%3C%2Fa%3E)%20and%20then%20into%20the%20folder%20data.%3Cbr%3E%0A%20%20%20%20Let's%20remove%20one%20of%20the%20files%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(path)%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%20%20%20%20missing_trial%20%3D%20Path(path)%20%2F%20'AAc202.txt'%0A%20%20%20%20missing_trial.unlink(missing_ok%3DTrue)%0A%20%20%20%20return%20(missing_trial%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Now%20let's%20read%20the%20data%20in%20these%20files%20and%20handle%20a%20possible%20missing%20or%20corrupted%20file%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20extension%2C%20missing_trial%2C%20np%2C%20path%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20assert%20not%20missing_trial.exists()%2C%20%22Run%20the%20missing-trial%20demonstration%20first%22%0A%20%20%20%20for%20_subject%20in%20subjects%3A%0A%20%20%20%20%20%20%20%20for%20_condition%20in%20conditions%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_trial%20in%20trials%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20path%20%2B%20_subject%20%2B%20_condition%20%2B%20_trial%20%2B%20extension%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_data%20%3D%20np.loadtxt(_filename%2C%20skiprows%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20except%20(OSError%2C%20ValueError)%20as%20err%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(_filename%2C%20err)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(_filename%2C%20'loaded')%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20The%20missing%20trial%20is%20AAc202.%20Why%20should%20its%20mean%20remain%20NaN%20instead%20of%20zero%3F%20Which%20errors%20should%20the%20file-reading%20block%20catch%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20Zero%20is%20a%20possible%20measured%20mean%3B%20NaN%20marks%20unavailable%20information.%20Catch%20%60OSError%60%20for%20missing%20or%20unreadable%20files%20and%20%60ValueError%60%20for%20malformed%20numeric%20text.%20Catching%20every%20exception%20would%20also%20hide%20programming%20defects.%20Record%20the%20filename%20and%20error%20so%20a%20missing%20result%20remains%20traceable.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Store%20results%0A%0A%20%20%20%20The%20results%20of%20the%20analysis%20for%20each%20file%20can%20be%20stored%20in%20a%20variable%20in%20different%20ways.%3Cbr%3E%0A%20%20%20%20We%20can%20store%20the%20results%20in%20a%20multidimensional%20variable%20where%20each%20dimension%20corresponds%20to%20the%20different%20indices%20in%20the%20loops.%20With%20the%20data%20above%20this%20would%20produce%20%60results(s%2C%20c%2C%20t)%60%2C%20a%202x2x3%20array.%20Or%20we%20can%20store%20everything%20in%20a%20two-dimensional%20array%20where%20for%20example%20each%20row%20corresponds%20to%20each%20combination%20of%20subject%2C%20condition%2C%20and%20trial.%3Cbr%3E%0A%20%20%20%20Let's%20try%20both%20ways%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20extension%2C%20missing_trial%2C%20np%2C%20path%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20assert%20not%20missing_trial.exists()%2C%20%22Run%20the%20missing-trial%20demonstration%20first%22%0A%20%20%20%20results%20%3D%20np.empty(shape%3D(2%2C%202%2C%203%2C%203))%20*%20np.nan%0A%20%20%20%20for%20(_s%2C%20_subject)%20in%20enumerate(subjects)%3A%0A%20%20%20%20%20%20%20%20for%20(_c%2C%20_condition)%20in%20enumerate(conditions)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(_t%2C%20_trial)%20in%20enumerate(trials)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20path%20%2B%20_subject%20%2B%20_condition%20%2B%20_trial%20%2B%20extension%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_data%20%3D%20np.loadtxt(_filename%2C%20skiprows%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20except%20(OSError%2C%20ValueError)%20as%20err%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pass%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20results%5B_s%2C%20_c%2C%20_t%2C%20%3A%5D%20%3D%20np.mean(_data%2C%20axis%3D0)%0A%20%20%20%20print(results.shape)%0A%20%20%20%20print(results)%0A%20%20%20%20return%20(results%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20One%20problem%20with%20this%20approach%20is%20that%20for%20many%20dimensions%20the%20data%20gets%20convoluted%20and%20it%20might%20be%20difficult%20to%20read%20it.%3Cbr%3E%0A%20%20%20%20The%20results%20for%20the%20first%20subject%2C%20condition%2C%20and%20trial%20are%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(results)%3A%0A%20%20%20%20results%5B0%2C%200%2C%200%2C%20%3A%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20can%20use%20the%20second%20approach%20and%20store%20the%20results%20in%20a%20two-dimensional%20array%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20extension%2C%20missing_trial%2C%20np%2C%20path%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20assert%20not%20missing_trial.exists()%2C%20%22Run%20the%20missing-trial%20demonstration%20first%22%0A%20%20%20%20results_1%20%3D%20np.empty(shape%3D(2%20*%202%20*%203%2C%203))%20*%20np.nan%0A%20%20%20%20results2%20%3D%20np.empty(shape%3D(2%20*%202%20*%203%2C%203))%20*%20np.nan%0A%20%20%20%20_ind%20%3D%200%0A%20%20%20%20for%20(_s%2C%20_subject)%20in%20enumerate(subjects)%3A%0A%20%20%20%20%20%20%20%20for%20(_c%2C%20_condition)%20in%20enumerate(conditions)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(_t%2C%20_trial)%20in%20enumerate(trials)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ind%20%3D%20_ind%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20path%20%2B%20_subject%20%2B%20_condition%20%2B%20_trial%20%2B%20extension%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_data%20%3D%20np.loadtxt(_filename%2C%20skiprows%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20except%20(OSError%2C%20ValueError)%20as%20err%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pass%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20results_1%5B_ind%20-%201%2C%20%3A%5D%20%3D%20np.mean(_data%2C%20axis%3D0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20results2%5Blen(conditions)%20*%20len(trials)%20*%20_s%20%2B%20len(trials)%20*%20_c%20%2B%20_t%2C%20%3A%5D%20%3D%20np.mean(_data%2C%20axis%3D0)%0A%20%20%20%20print(results_1.shape)%0A%20%20%20%20print(results_1)%0A%20%20%20%20print(results2.shape)%0A%20%20%20%20print(results2)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20can%20create%20columns%20identifying%20the%20subject%2C%20condition%2C%20and%20trial%2C%20which%20might%20be%20useful%20for%20running%20statistical%20analysis%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(conditions%2C%20extension%2C%20missing_trial%2C%20np%2C%20path%2C%20subjects%2C%20trials)%3A%0A%20%20%20%20assert%20not%20missing_trial.exists()%2C%20%22Run%20the%20missing-trial%20demonstration%20first%22%0A%20%20%20%20results_2%20%3D%20np.empty(shape%3D(2%20*%202%20*%203%2C%203))%20*%20np.nan%0A%20%20%20%20_ind%20%3D%200%0A%20%20%20%20indexes%20%3D%20%5B%5D%0A%20%20%20%20for%20(_s%2C%20_subject)%20in%20enumerate(subjects)%3A%0A%20%20%20%20%20%20%20%20for%20(_c%2C%20_condition)%20in%20enumerate(conditions)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(_t%2C%20_trial)%20in%20enumerate(trials)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ind%20%3D%20_ind%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20indexes.append(%5B_s%2C%20_c%2C%20_t%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_filename%20%3D%20path%20%2B%20_subject%20%2B%20_condition%20%2B%20_trial%20%2B%20extension%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_data%20%3D%20np.loadtxt(_filename%2C%20skiprows%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20except%20(OSError%2C%20ValueError)%20as%20err%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pass%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20results_2%5B_ind%20-%201%2C%20%3A%5D%20%3D%20np.mean(_data%2C%20axis%3D0)%0A%20%20%20%20results_2%20%3D%20np.hstack((np.array(indexes)%2C%20results_2))%0A%20%20%20%20print(results_2.shape)%0A%20%20%20%20print(results_2)%0A%20%20%20%20return%20(results_2%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203.**%20For%20zero-based%20indices%20subject%201%2C%20condition%200%2C%20trial%202%2C%20find%20the%20row%20in%20the%20flattened%2012-row%20result.%20Why%20keep%20identifier%20columns%20as%20well%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20The%20row%20is%20%242%5Ctimes3%5Ctimes1%2B3%5Ctimes0%2B2%3D8%24.%20Identifier%20columns%20preserve%20the%20experimental%20meaning%20if%20rows%20are%20subsequently%20sorted%2C%20filtered%2C%20or%20merged%3B%20an%20array%20position%20alone%20does%20not.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20These%20are%20just%20some%20possible%20generic%20approaches%20to%20analyze%20data%20in%20multiple%20files.%0A%0A%20%20%20%20And%20we%20can%20save%20the%20results%20in%20a%20file%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20path%2C%20results_2)%3A%0A%20%20%20%20_filename%20%3D%20path%20%2B%20'results.txt'%0A%20%20%20%20_header%20%3D%20'Subject%5CtCondition%5CtTrial%5CtCol%20A%5CtCol%20B%5CtCol%20C'%0A%20%20%20%20np.savetxt(_filename%2C%20results_2%2C%20fmt%3D'%25d%5Ct%25d%5Ct%25d%5Ct%25g%5Ct%25g%5Ct%25g'%2C%20delimiter%3D'%5Ct'%2C%20header%3D_header%2C%20comments%3D'')%0A%20%20%20%20print('File'%2C%20_filename%2C%20'saved')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20Which%20dimension%20of%20%60results%60%20represents%20measured%20columns%3F%0A%20%20%20%202.%20Why%20is%20%60continue%60%20inside%20the%20innermost%20loop%3F%0A%20%20%20%203.%20Does%20a%20successful%20file%20read%20prove%20the%20measurement%20is%20valid%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20The%20last%20dimension%2C%20of%20length%20three.%0A%20%20%20%202.%20It%20skips%20only%20the%20failed%20trial.%0A%20%20%20%203.%20No%3B%20check%20shape%2C%20units%2C%20range%2C%20and%20acquisition%20metadata%20too.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Four%20subjects%2C%20three%20conditions%2C%20five%20trials%2C%20and%20two%20measured%20columns%3A%20give%20both%20result%20shapes.%0A%20%20%20%202.%20One%20failed%20trial%20and%20one%20genuine%20zero%20mean%20occur%20in%20the%20same%20column.%20State%20how%20each%20should%20be%20represented.%0A%20%20%20%203.%20Give%20a%20reproducible%20record%20to%20accompany%20the%20exported%20result.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20%60(4%2C%203%2C%205%2C%202)%60%20and%20%60(60%2C%202)%60%3B%20with%20identifier%20columns%2C%20%60(60%2C%205)%60.%0A%20%20%20%202.%20NaN%20for%20unavailable%20data%3B%200%20for%20the%20measured%20zero.%0A%20%20%20%203.%20Subject%2Fcondition%2Ftrial%20identifiers%2C%20units%2C%20source%20filenames%2C%20exclusion%20reasons%2C%20processing%20settings%2C%20and%20code%20version.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BPython%20tutorial%3A%20errors%20and%20exceptions%5D(https%3A%2F%2Fdocs.python.org%2F3%2Ftutorial%2Ferrors.html)%20%E2%80%94%20distinguish%20expected%20input%20failures%20from%20bugs.%20Follow%20with%20%5BPython%20tutorial%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fpython_tutorial.py)%20for%20arrays%20and%20files.%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BMIT%3A%20Introduction%20to%20Computer%20Science%20and%20Programming%20in%20Python%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2F6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016%2Fvideo_galleries%2Flecture-videos%2F)%20%E2%80%94%20start%20with%20computation%2C%20branching%2C%20and%20iteration%3B%20the%20videos%20explain%20how%20to%20reason%20about%20code%20before%20running%20it.%0A%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20%5BPython%20tutorial%3A%20errors%20and%20exceptions%5D(https%3A%2F%2Fdocs.python.org%2F3%2Ftutorial%2Ferrors.html)%20%E2%80%94%20distinguish%20expected%20input%20failures%20from%20bugs.%20Follow%20with%20%5BPython%20tutorial%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fpython_tutorial.py)%20for%20arrays%20and%20files.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
5b4a13dd191b9be00e58625d8a12a992