<jupyter_start><jupyter_text>![Image](./img/dataset-diagram-logo.png)**objectif** : pandas like but for **ndim**<jupyter_code>import numpy as np
import pandas as pd
import xarray as xr
truc = xr.DataArray(np.random.randint(2, 3))
truc
data = xr.DataArray(np.random.randint(6, size=(2, 3)), coords={'x': ['a', 'b']}, dims=('x', 'y'))
data
xr.DataArray(pd.Series(range(3), index=list('abc'), name='foo'))<jupyter_output><empty_output><jupyter_text>Attribute of DataArray<jupyter_code>print('values : ', data.values)
print('dims : ', data.dims)
print('coords : ', data.coords)
print('attrs metadata', data.attrs)<jupyter_output>values :  [[0 5 0]
 [1 1 1]]
dims :  ('x', 'y')
coords :  Coordinates:
  * x        (x) <U1 'a' 'b'
attrs metadata {}<jupyter_text>Indexing  numpy like<jupyter_code>data[:, :2]<jupyter_output><empty_output><jupyter_text>pandas like with loc[]<jupyter_code>data.loc[['a', 'b'], :]<jupyter_output><empty_output><jupyter_text>with isel()<jupyter_code>data.isel(y=[0, 2])<jupyter_output><empty_output><jupyter_text>with sel()<jupyter_code>data.sel(x=['a'])<jupyter_output><empty_output><jupyter_text>Attributes (metadata) usefull for protting<jupyter_code>data.attrs['long_name'] = 'random unseless stuff'
data.attrs['units'] = 'mV'
data
data.sel(x='a').plot()<jupyter_output><empty_output><jupyter_text>Computation / aggregation<jupyter_code># mais on peut utiliser les noms des axes au lieu de leur numéro
data.mean(dim='y')<jupyter_output><empty_output><jupyter_text>GroupBy<jupyter_code>data
labels = xr.DataArray(['E', 'F', 'E'], coords = [data.coords['y']], name='labels')
labels
data1 = data.groupby(labels).sum('y')
data1
data2 = data.groupby(labels).apply(lambda x: x - x.min())
data2<jupyter_output><empty_output><jupyter_text>Pandas<jupyter_code>series = data.to_series()
series
series.to_xarray()<jupyter_output><empty_output><jupyter_text>Dataset = DataArray in the same place<jupyter_code>times = np.arange(0, 10, 0.01)
freqs = np.arange(10, 50, 2.)

data1 = xr.DataArray(np.random.randn(times.size),
                     coords={'times': times},
                     dims=('times'))

data2 = xr.DataArray(np.random.randn(times.size, freqs.size),
                     coords={'times': times, 'freqs': freqs},
                     dims=('times', 'freqs'))



ds = xr.Dataset({'raw_signal': data1,
                 'time_freq' : data2,
                 'one_scalar': np.pi})
ds
ds['raw_signal']
ds.isel(times=slice(0,100))<jupyter_output><empty_output><jupyter_text>NetCDF<jupyter_code>ds.to_netcdf('example.nc')
xr.open_dataset('example.nc')<jupyter_output><empty_output><jupyter_text>Exercice 1 * Explorer le dossier `data/data_folder` * Créer un DataSet/DataArray pour organiser les datas de manieres multi dimensionel  Chaque tableau `*.npy` a pour shape `(4, 3)` On peut considerer ces dimenssion comme `(trial, question)` avec trial de 0 à 4 and question 'a', 'b', 'c'<jupyter_code>import xarray as xr<jupyter_output><empty_output><jupyter_text>Solution 1<jupyter_code>!ls -r data/data_folder/*
from pathlib import Path
folder = Path('data/data_folder')

subjects = [f.stem for f in folder.iterdir()]
print(subjects)

sessions = [int(f.stem.replace('session_', '')) for f in (folder / subjects[0]).iterdir()]
sessions = sorted(sessions)
sessions
# mydata = {}
# for file_path in folder.glob('**/*.npy'):
#     subject = file_path.parents[0].stem
#     session = int(file_path.stem.replace('session_', ''))
#     if subject not in mydata:
#         mydata[subject] = {}
#     arr = np.load(file_path)
# mydata

coords = {
    'subjects' : subjects,
    'sessions' : sessions,
    'trial' : np.arange(4),
    'questions' : np.array(['a', 'b', 'c'])
}
data = xr.DataArray(coords=coords, dims=['subjects', 'sessions', 'trial','questions'])
data


for subject in subjects:
    for session in sessions:
        file_path = folder / subject / f'session_{session}.npy'
        np_arr = np.load(file_path)
        data.loc[subject, session, :, :] = np_arr

data
ds = xr.Dataset()
ds['data'] = data
ds.to_netcdf('mydata.nc')
ds = xr.open_dataset('mydata.nc')
ds['data']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17223837209302326, 'fraction_numerical': 0.011627906976744186, 'mean_word_length': 5.218373493975903, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '272608', 'license': 'MIT', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1586, 'n_words': 290, 'path': 'notebooks/xarray.ipynb', 'repo_name': 'crnl-lab/formation_python_crnl_2022', 'size': '4128'}