Skip to content

Module dcm2bids.utils.io⚓︎

View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-

import json

from pathlib import Path

from collections import OrderedDict

def load_json(filename):

    """ Load a JSON file

    Args:

        filename (str): Path of a JSON file

    Return:

        Dictionary of the JSON file

    """

    with open(filename, "r") as f:

        data = json.load(f, object_pairs_hook=OrderedDict)

    return data

def save_json(filename, data):

    with open(filename, "w") as f:

        json.dump(data, f, indent=4)

def write_txt(filename, lines):

    with open(filename, "w") as f:

        f.write(f"{lines}\n")

def valid_path(in_path, type="folder"):

    """Assert that file exists.

    Parameters

    ----------

    required_file: Path

        Path to be checked.

    """

    if isinstance(in_path, str):

        in_path = Path(in_path)

    if type == 'folder':

        if in_path.is_dir() or in_path.parent.is_dir():

            return in_path

        else:

            raise NotADirectoryError(in_path)

    elif type == "file":

        if in_path.is_file():

            return in_path

        else:

            raise FileNotFoundError(in_path)

    raise TypeError(type)

Functions⚓︎

load_json⚓︎

1
2
3
def load_json(
    filename
)

Load a JSON file

Parameters:

Name Type Description Default
filename str Path of a JSON file None
View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def load_json(filename):

    """ Load a JSON file

    Args:

        filename (str): Path of a JSON file

    Return:

        Dictionary of the JSON file

    """

    with open(filename, "r") as f:

        data = json.load(f, object_pairs_hook=OrderedDict)

    return data

save_json⚓︎

1
2
3
4
def save_json(
    filename,
    data
)
View Source
1
2
3
4
5
def save_json(filename, data):

    with open(filename, "w") as f:

        json.dump(data, f, indent=4)

valid_path⚓︎

1
2
3
4
def valid_path(
    in_path,
    type='folder'
)

Assert that file exists.

Parameters:

Name Type Description Default
required_file Path Path to be checked. None
View Source
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def valid_path(in_path, type="folder"):

    """Assert that file exists.

    Parameters

    ----------

    required_file: Path

        Path to be checked.

    """

    if isinstance(in_path, str):

        in_path = Path(in_path)

    if type == 'folder':

        if in_path.is_dir() or in_path.parent.is_dir():

            return in_path

        else:

            raise NotADirectoryError(in_path)

    elif type == "file":

        if in_path.is_file():

            return in_path

        else:

            raise FileNotFoundError(in_path)

    raise TypeError(type)

write_txt⚓︎

1
2
3
4
def write_txt(
    filename,
    lines
)
View Source
1
2
3
4
5
def write_txt(filename, lines):

    with open(filename, "w") as f:

        f.write(f"{lines}\n")

Last update: 2023-08-31
Created: 2023-08-31