Skip to content

Commit

Permalink
further improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Nov 23, 2024
1 parent 7f37c11 commit ce89898
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 206 deletions.
192 changes: 71 additions & 121 deletions docs/collaboration/contributors.md

Large diffs are not rendered by default.

61 changes: 37 additions & 24 deletions docs/getting_started/folders_and_files/metadata/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ JSON files are text files that take the following structure:
Note that they can be nested (curly brackets within curly brackets).
Here are some common ways to read / write these files.

- [Official documentation about JSON](https://www.json.org/json-en.html)

## Editing JSON file

### Online
Expand Down Expand Up @@ -57,24 +59,28 @@ to that of Python dictionaries (assuming you are only storing text / numbers in

### MATLAB / Octave

There are many toolboxes in MATLAB or [Octave](https://octave.org/) (an open-source drop-in compatible with many Matlab scripts)
for reading / writing JSON files.
Since MATLAB R2016b
and [Octave](https://octave.org/) >= 7.1 (an open-source drop-in compatible with many Matlab scripts),
you can use the built-in functions
[`jsonencode`](https://www.mathworks.com/help/matlab/ref/jsonencode.html)
and [`jsondecode`](https://www.mathworks.com/help/matlab/ref/jsondecode.html)
to help your read and write JSON files.

<!-- TODO check is more recent Octave have JSON support. -->
Since MATLAB R2016b, you can use the built-in functions `jsonencode` (to write) and `jsondecode` (to read) JSON files.
They should be available in Octave 6.1.
!!! note -- "bids-matlab and other libraries"

The [JSONio library](https://github.com/gllmflndn/JSONio) will allow you to read
and write JSON files with MATLAB and octave (see examples below to use `jsonwrite` and `jsonread`).
There are also several toolboxes in MATLAB or Octave
for reading / writing JSON files.

SPM12 uses the JSONio library by calling `spm_jsonwrite` and `spm_jsonread` and
it has [other interesting functions to help you with BIDS](https://en.wikibooks.org/wiki/SPM/BIDS).
[bids-matlab](https://github.com/bids-standard/bids-matlab) has functions
([`bids.util.jsonencode`](https://bids-matlab.readthedocs.io/en/main/utility_functions.html#bids.util.jsonencode)
and [`bids.util.jsondecode`](https://bids-matlab.readthedocs.io/en/main/utility_functions.html#bids.util.jsonencode))
that act as wrappers and will use whatever implementation (MATLAB, Octave...) is available.

!!! note -- "bids-matlab"
The [JSONio library](https://github.com/gllmflndn/JSONio) will allow you to read
and write JSON files with MATLAB and Octave with the `jsonread` and `jsonwrite` functions.

[bids-matlab](https://github.com/bids-standard/bids-matlab) has 2 functions
(`bids.util.jsonencode` and `bids.util.jsondecode`) that act as wrappers and
will use whatever implementation (SPM, JSONio, MATLAB) is available.
SPM12 uses the JSONio library by calling `spm_jsonwrite` and `spm_jsonread` and
it has [other interesting functions to help you with BIDS](https://en.wikibooks.org/wiki/SPM/BIDS).

### R

Expand All @@ -84,10 +90,6 @@ Remember to install and call a package before using it.

<!-- There is a new package to help intract with BIDS datasets: [bidser](https://github.com/bbuchsbaum/bidser) -->

```R
install.packages('jsonlite')
```

## Reading a `.json` file

=== "python"
Expand All @@ -103,7 +105,10 @@ install.packages('jsonlite')
For MATLAB >= R2016b

```matlab
metadata = jsonencode('myfile.json')
% Read JSON data from a file
jsonStr = fileread('myfile.json');
% Convert JSON string to MATLAB variables
jsonData = jsondecode(jsonStr);
```

=== "octave"
Expand All @@ -116,10 +121,13 @@ install.packages('jsonlite')

=== "R"

The example below uses the [jsonlite](https://github.com/jeroen/jsonlite) library.

```R
install.packages('jsonlite')
# install jsonlite in case you did not already do it
# install.packages('jsonlite')
library(jsonlite)
metadata = fromJSON('myfile.json', pretty=TRUE)
metadata = fromJSON('myfile.json')
```

## Writing a `.json` file
Expand All @@ -145,16 +153,21 @@ install.packages('jsonlite')
The example below uses the [JSONio library](https://github.com/gllmflndn/JSONio).

```matlab
metadata = struct('field1', 'value1', 'field2', 3, 'field3', 'field3')
jsonwrite('my_output_file.json', metadata)
```

=== "R"

The example below uses the [jsonlite](https://github.com/jeroen/jsonlite) library.

```R
install.packages('jsonlite')
# install jsonlite in case you did not already do it
# install.packages('jsonlite')
library(jsonlite)
metadata = '{"field1": "value1", "field2": 3, "field3": "field3"}'
writeLines(metadata, file="my_output_file.json")
metadata <- list(field1 = "value1", field2 = 3, field3 = "field3")
metadata = toJSON(metadata, pretty=TRUE)
write(metadata, "my_output_file.json")
```

## Interoperability issues
Expand Down Expand Up @@ -191,7 +204,7 @@ json_content =
key: 'value'
```

There are however some strict rules for what makes a valid fieldname in MATLAB and octave.
There are however some strict rules for what makes a valid fieldname in MATLAB and Octave.

Fieldnames must:

Expand Down
145 changes: 86 additions & 59 deletions docs/getting_started/folders_and_files/metadata/tsv.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@ A Tab-Separate Values (TSV) file is a text file where tab characters (`\t`) sepa
It is structured as a table, with each column representing a field of interest,
and each row representing a single datapoint.

## Python

In Python, the easiest way to work with TSV files is to use the [Pandas library](https://pandas.pydata.org/).
This provides a high-level structure to organize, manipulate, clean, and visualize tabular data.
You can install `pandas` with the following command:

```bash
pip install pandas
```

## MATLAB / Octave

Since MATLAB R2013b,
there is a [`readtable`](https://www.mathworks.com/help/matlab/ref/readtable.html) function that can load TSV files,
and a [`writetable`](https://www.mathworks.com/help/matlab/ref/writetable.html) function to write them.

For Octave, the `writetable` function is not implemented in older version of Octave
(e.g 4.2.2) and the `table` function differs from its MATLAB counterpart,
so it may be easier to rely on [bids-matlab](https://github.com/bids-standard/bids-matlab) functions
([`bids.util.tsvwrite`](https://bids-matlab.readthedocs.io/en/main/utility_functions.html#bids.util.tsvwrite) and
[`bids.util.tsvread`](https://bids-matlab.readthedocs.io/en/main/utility_functions.html#bids.util.tsvread))
to help you work with those files.

## R

Reading and writing tab separated files comes natively in R, no need for extra packages.

## Reading a `.tsv` file

=== "python"

In Python, the easiest way to work with TSV files is to use the Pandas library.
This provides a high-level structure to organize, manipulate, clean, and
visualize tabular data.
You can install `pandas` with the following command:

```bash
pip install pandas
```
In this example, we assume the .tsv includes column names (headers),
and explicitly set column separator (delimiter) to tab (`'\t'`).

```python
import pandas as pd
data = pd.pd.read_csv("file.tsv", sep="\t", headers=True)
data = pd.read_csv("file.tsv", sep="\t", headers=True)
```

=== "MATLAB"

Since MATLAB R2013b, there is a [`readtable`](https://www.mathworks.com/help/matlab/ref/readtable.html) function
that can load TSV files.

```matlab
table_content = readtable('file.tsv', ...
'FileType', 'text', ...
Expand All @@ -36,75 +54,84 @@ and each row representing a single datapoint.

=== "octave"

The example below uses the [bids-matlab](https://github.com/bids-standard/bids-matlab) library.

```matlab
table_content = bids.util.tsvread('file.tsv');
```

=== "R"

Reading and writing tab separated files comes natively in R, no need for extra packages.

In this example, we assume the .tsv includes column names (headers),
and explicitly set column separator (delimiter) to tab (`'\t'`)

```R
data = read.table('myFile.tsv', header=TRUE, sep='\t')
data = read.table('file.tsv', header=TRUE, sep='\t')
```

=== "Excel / [LibreOffice Calc](https://www.libreoffice.org/)"

## Writing a `.tsv` file
=== "Excel / LibreOffice Calc"

## Matlab
Excel, [LibreOffice Calc](https://www.libreoffice.org/) and similar software to work with tables
should have no problem opening TSV files.

```matlab
root_dir = pwd;
bidsProject = 'temp';
mkdir(fullfile(root_dir, bidsProject));
bids_participants_name = 'participants.tsv';
## Writing a `.tsv` file

participant_id = ['sub-01'; 'sub-02'];
age = [20 30]';
sex = ['m';'f'];
=== "python"

t = table(participant_id,age,sex);
writetable(t, fullfile(root_dir, bidsProject, bids_participants_name), ...
'FileType', 'text', ...
'Delimiter', '\t');
```
```python
import pandas as pd

## Octave
participants = pd.DataFrame(
{
"participant_id": ["sub-01", "sub-02"],
"age": [20, 30],
"sex": ["m", "f"],
}
)
participants.to_csv("participants.tsv")
```

The `writetable` function is not implemented in older version of Octave
(e.g 4.2.2) and the `table` function differs from its matlab counterpart.
These are still in development for future
[releases](https://github.com/apjanke/octave-tablicious) so some of the scripts
provided in the BIDS starter-kit repository in the matlab code folder
to create .tsv might not work with octave because of that reason.
=== "MATLAB"

## Excel
```matlab
participant_id = ['sub-01'; 'sub-02'];
age = [20; 30]';
sex = ['m'; 'f'];
participants = table(participant_id, age, sex);

writetable(participants, ...
'participants.tsv', ...
'FileType', 'text', ...
'Delimiter', '\t');
```

- Create a file with the following columns
(at least, for other values see paragraph the
[BIDS specification](https://bids-specification.readthedocs.io/en/latest/03-modality-agnostic-files.html#participants-file))
- participant_id
- age
- sex
=== "Octave"

- Save as tab separated `.txt` and change extension to `.tsv`
The example below uses the [bids-matlab](https://github.com/bids-standard/bids-matlab) library.

## R
```matlab
participants = struct(...
'participant_id', ['sub-01', 'sub-02'];
'age', [20, 30]';
'sex', ['m', 'f']
);

### Writing a `.tsv` file
bids.util.tsvwrite('participants.tsv', participants);
```

When writing files, column and row names are always saved, we remove row names,
and quotes from the outpur explicitly by setting them to FALSE.
=== "R"

```R
data = cbind.data.frame(
participant_id = c('sub-01', 'sub-02'),
age = c(20,30),
sex = c('m','f'))
When writing files, column and row names are always saved,
we remove row names and quotes from the output explicitly by setting them to `FALSE`.

write.table(data, file='myData.tsv',sep='\t',
row.names = FALSE, quote = FALSE)
```
```R
participant_id <- c('sub-01', 'sub-02');
age <- c(20, 30);
sex <- c('m', 'f');
participants <- data.frame(participant_id, age, sex)
write.table(participants,
file='participants.tsv',
sep='\t',
row.names = FALSE,
quote = FALSE)
```
2 changes: 0 additions & 2 deletions docs/getting_started/resources/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ It'll work for all the languages below!

[**_Octave Information_**](https://www.gnu.org/software/octave/)

Required JSON package [**JSONio**](https://github.com/gllmflndn/JSONio)

## Python

[**_Information_**](https://www.python.org/) (Both Python 2 and 3 will work for BIDS)
Expand Down
3 changes: 3 additions & 0 deletions temp/participants.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
participant_id age sex
sub-01 20 m
sub-02 30 f
10 changes: 10 additions & 0 deletions tmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pandas as pd

participants = pd.DataFrame(
{
"participant_id": ["sub-01", "sub-02"],
"age": [20, 30],
"sex": ["m", "f"],
}
)
participants.to_csv("participants.tsv")

0 comments on commit ce89898

Please sign in to comment.