Skip to content

read_file

phyllotaxis_analysis.read_file Link

This module contains a class and some functions to read and extract data from a csv file. We suppose that the data for each plant is stored in a column.

read_csv_file Link

read_csv_file(filename)

Read and extract divergence angles from a tab-separated CSV file.

This function reads a CSV file where each plant's data is stored in a column, converts valid integer values to integers, and returns them as nested lists.

Parameters:

Name Type Description Default
filename str

Path to the CSV file to be read. The file must use tab characters as delimiters.

required

Returns:

Type Description
list of list of int

A nested list where each sublist contains integer values extracted from the CSV file. Returns an empty list if the file is not found or contains non-integer values.

Raises:

Type Description
FileNotFoundError

If the specified file does not exist.

ValueError

If the file contains non-integer values that cannot be converted.

Source code in src/phyllotaxis_analysis/read_file.py
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
def read_csv_file(filename):
    """Read and extract divergence angles from a tab-separated CSV file.

    This function reads a CSV file where each plant's data is stored in a column,
    converts valid integer values to integers, and returns them as nested lists.

    Parameters
    ----------
    filename : str
        Path to the CSV file to be read. The file must use tab characters as delimiters.

    Returns
    -------
    list of list of int
        A nested list where each sublist contains integer values extracted from the CSV file.
        Returns an empty list if the file is not found or contains non-integer values.

    Raises
    ------
    FileNotFoundError
        If the specified file does not exist.
    ValueError
        If the file contains non-integer values that cannot be converted.
    """
    try:
        with open(filename, 'r') as fobj:
            readerObj = csv.reader(fobj, delimiter='\t')
            mainList = [list(row) for row in readerObj]
            angles = []
            for row in mainList:
                current_row = []
                for angle in row:
                    if angle != "":
                        try:
                            current_row.append(int(angle))
                        except ValueError:
                            # Skip non-integer values or handle as needed
                            pass
                angles.append(current_row)
        return angles
    except FileNotFoundError:
        print(f"Error: File '{filename}' not found.")
        return []
    except Exception as e:
        print(f"Error reading file: {e}")
        return []

read_seq_file Link

read_seq_file(filename)

Read and extract data from a .seq file

Parameters: filename (str): file name to read

Returns: list: list of sequences of divergence angles

Raises: FileNotFoundError: If the file does not exist IOError: If there's an error reading the file

Source code in src/phyllotaxis_analysis/read_file.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def read_seq_file(filename):
    """
    Read and extract data from a .seq file

    Parameters:
        filename (str): file name to read

    Returns:
        list: list of sequences of divergence angles

    Raises:
        FileNotFoundError: If the file does not exist
        IOError: If there's an error reading the file
    """
    anglesList = []
    seq = []

    try:
        with open(filename, 'r') as fobj:
            # Skip header (first 12 lines)
            for _ in range(12):
                next(fobj, None)

            # Process data lines
            for line in fobj:
                if len(line) > 3:
                    if '\\' in line[-4:]:
                        # Line continues in next line
                        splittedLine = line[:-3].split()
                        seq.extend([int(i) for i in splittedLine])
                    elif ')' in line[-4:]:
                        # End of sequence
                        splittedLine = line[:-8].split()
                        seq.extend([int(i) for i in splittedLine])
                        anglesList.append(seq)
                        seq = []

        return anglesList

    except (FileNotFoundError, IOError) as e:
        # Re-raise with more context
        raise type(e)(f"Error reading file {filename}: {str(e)}")

read_txt_data_file Link

read_txt_data_file(filename)

Reads a text data file containing angles represented by integer values and returns a list of lists with those values.

Parameters:

Name Type Description Default
filename str

The path to the text file that will be read, including its name and file extension (e.g., 'path/to/file.txt').

required

Returns:

Name Type Description
angles_list list

A list of lists where each inner list contains three integer values representing angles in a line of the input text file. If there are any issues reading or parsing the file, an empty list will be returned.

Raises:

Type Description
FileNotFoundError

If the specified file does not exist.

ValueError

If the file contains non-integer values that cannot be converted.

Source code in src/phyllotaxis_analysis/read_file.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def read_txt_data_file(filename):
    """Reads a text data file containing angles represented by integer values and returns
    a list of lists with those values.

    Parameters
    ----------
    filename : str
        The path to the text file that will be read, including its name and file
        extension (e.g., 'path/to/file.txt').

    Returns
    -------
    angles_list : list
        A list of lists where each inner list contains three integer values
        representing angles in a line of the input text file. If there are any issues
        reading or parsing the file, an empty list will be returned.

    Raises
    ------
    FileNotFoundError
        If the specified file does not exist.
    ValueError
        If the file contains non-integer values that cannot be converted.
    """
    anglesList = []
    try:
        with open(filename, 'r') as fobj:
            for line in fobj:
                # Strip whitespace from both ends instead of just removing last char
                line = line.strip()
                if not line:  # Skip empty lines
                    continue

                lineList = line.split(' ')
                try:
                    l = [int(i) for i in lineList if i]  # Skip empty strings
                    anglesList.append(l)
                except ValueError:
                    print(f"Warning: Skipping line with non-integer values: {line}")

        return anglesList
    except FileNotFoundError:
        print(f"Error: File '{filename}' not found")
        return []
    except Exception as e:
        print(f"Error reading file '{filename}': {e}")
        return []