Skip to content

cli


parse_shape(value)

Parse a shape argument into a tuple of integers.

This function takes a string representing shape dimensions. If the input is a single value, it is duplicated to form a tuple of length two. If multiple values are provided, they are converted into a tuple.

Args: value: A string containing one or more integers separated by commas, representing shape dimensions.

Returns: A tuple of two integers derived from the input string.

Examples: parse_shape("5") returns (5, 5) parse_shape("5,6") returns (5, 6)

Source code in preprocess_toolbox/cli.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def parse_shape(value: str) -> tuple[int, int]:
    """
    Parse a shape argument into a tuple of integers.

    This function takes a string representing shape dimensions.
    If the input is a single value, it is duplicated to form a tuple of
    length two. If multiple values are provided, they are converted into
    a tuple.

    Args:
        value: A string containing one or more integers separated by commas,
            representing shape dimensions.

    Returns:
        A tuple of two integers derived from the input string.

    Examples:
        parse_shape("5") returns (5, 5)
        parse_shape("5,6") returns (5, 6)
    """
    if isinstance(value, int):
        return (value, value)
    else:
        values = value.split(",")

        # If only one value is provided, repeat it to create a tuple of length 2
        if len(values) == 1:
            values.append(values[0])
        values = map(int, values)

    return tuple(values)

process_split_args(args, frequency)

Parameters:

Name Type Description Default
args object
required
frequency Frequency
required

Returns:

Type Description
dict
Source code in preprocess_toolbox/cli.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def process_split_args(args: object,
                       frequency: Frequency) -> dict:
    """

    :param args:
    :param frequency:
    :return:
    """
    if not hasattr(args, "split_names") or args.split_names is None:
        logging.info("No split names in arguments to process...")
        return dict()

    splits = {_: list() for _ in args.split_names}

    for idx, split in enumerate(splits.keys()):
        split_dates = collections.deque()

        for period_start, period_end in zip(args.split_starts[idx], args.split_ends[idx]):
            split_dates += [
                pd.to_datetime(date).date()
                for date in pd.date_range(period_start, period_end, freq=frequency.freq)
            ]
        logging.info("Got {} dates for {}".format(len(split_dates), split))

        splits[split] = sorted(list(split_dates))
    return splits