File size: 8,426 Bytes
07423df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
105
106
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
from abc import abstractmethod
from dataclasses import dataclass
from typing import Any, Callable, List, Optional, Sequence, Set, Tuple, Union

from llm_studio.src.nesting import Dependency


def _scan_dirs(dirname) -> List[str]:
    """Scans a directory for subfolders

    Args:
        dirname: directory name

    Returns:
        List of subfolders

    """

    subfolders = [f.path for f in os.scandir(dirname) if f.is_dir()]
    for dirname in list(subfolders):
        subfolders.extend(_scan_dirs(dirname))
    subfolders = [x + "/" if x[-1] != "/" else x for x in subfolders]
    return subfolders


def _scan_files(
    dirname, extensions: Tuple[str, ...] = (".csv", ".pq", ".parquet", ".json")
) -> List[str]:
    """Scans a directory for files with given extension

    Args:
        dirname: directory name
        extensions: extensions to consider

    Returns:
        List of files

    """
    path_list = [
        os.path.join(dirpath, filename)
        for dirpath, _, filenames in os.walk(dirname)
        for filename in filenames
        if any(map(filename.__contains__, extensions))
        and not filename.startswith("__meta_info__")
    ]
    return sorted(path_list)


def strip_prefix(paths: Sequence[str], ignore_set: Set[str] = set()) -> Tuple[str, ...]:
    """
    Strips the common prefix of all the given paths.

    Args:
        paths: the paths to strip
        ignore_set: set of path names to ignore when computing the prefix.

    Returns:
        List with the same length as `paths` without common prefixes.
    """

    paths_to_check = [
        os.path.split(os.path.normpath(path))[0]
        for path in paths
        if path not in ignore_set
    ]

    if len(paths_to_check) == 0:
        return tuple(paths)

    prefix = os.path.commonpath(paths_to_check)
    stripped = tuple(
        [
            path if path in ignore_set else os.path.relpath(path, prefix)
            for path in paths
        ]
    )

    return stripped


class Value:
    pass


@dataclass
class Number:
    min: Optional[float] = None
    max: Optional[float] = None
    step: Union[str, float] = 1.0


@dataclass
class String:
    # Each element of the tuple can be either:
    # - a tuple of (value, name)
    # - a string. In that case the same value will be used for name and value
    values: Any = None
    allow_custom: bool = False
    placeholder: Optional[str] = None


class DatasetValue:
    pass

    @abstractmethod
    def get_value(
        self, dataset: Any, value: Any, type_annotation: type, mode: str
    ) -> Tuple[String, Any]:
        pass

    @staticmethod
    def _compute_current_values(
        current_values: List[str],
        possible_values: List[str],
        prefer_with: Optional[Callable[[str], bool]] = None,
    ) -> List[str]:
        """
        Compute current values.

        Args:
            current_values: The preliminary current values.
            possible_values: All possible values.
            prefer_with: Function determining which values to prefer as default.

        Returns:
            A list
        """
        if len(possible_values) == 0:
            return [""]

        # allow only values which are in the possible values
        current_values = list(
            filter(lambda value: value in possible_values, current_values)
        )

        if len(current_values) == 0:
            # if the values are empty, take all the values where `prefer_with` is true
            for c in possible_values:
                if prefer_with is not None and prefer_with(c):
                    current_values.append(c)

            # if they are still empty, just take the first possible value
            if len(current_values) == 0:
                current_values = [possible_values[0]]

        return current_values


@dataclass
class Directories(DatasetValue):
    add_none: Union[bool, Callable[[str], bool]] = False
    prefer_with: Optional[Callable[[str], bool]] = None
    prefer_none: bool = True

    def get_value(self, dataset, value, type_annotation, mode) -> Tuple[String, Any]:
        if dataset is None:
            return String(tuple()), value

        available_dirs = _scan_dirs(dataset["path"])

        if (isinstance(self.add_none, bool) and self.add_none) or (
            callable(self.add_none) and self.add_none(mode)
        ):
            if self.prefer_none:
                available_dirs.insert(0, "None")
            else:
                available_dirs.insert(len(available_dirs), "None")

        if isinstance(value, str):
            value = [value]

        value = DatasetValue._compute_current_values(
            value, available_dirs, self.prefer_with
        )

        return (
            String(
                tuple(
                    zip(
                        available_dirs,
                        strip_prefix(available_dirs, ignore_set={"None"}),
                    )
                )
            ),
            value if type_annotation == Tuple[str, ...] else value[0],
        )


@dataclass
class Files(DatasetValue):
    add_none: Union[bool, Callable[[str], bool]] = False
    prefer_with: Optional[Callable[[str], bool]] = None
    # For the case where no match found, whether to prioritize
    # selecting any file or selecting no file
    prefer_none: bool = True

    def get_value(self, dataset, value, type_annotation, mode) -> Tuple[String, Any]:
        if dataset is None:
            return String(tuple()), value

        available_files = _scan_files(dataset["path"])

        if (isinstance(self.add_none, bool) and self.add_none) or (
            callable(self.add_none) and self.add_none(mode)
        ):
            if self.prefer_none:
                available_files.insert(0, "None")
            else:
                available_files.insert(len(available_files), "None")

        if isinstance(value, str):
            value = [value]

        value = DatasetValue._compute_current_values(
            value, available_files, self.prefer_with
        )

        return (
            String(
                tuple(
                    zip(
                        available_files,
                        strip_prefix(available_files, ignore_set={"None"}),
                    )
                )
            ),
            value if type_annotation == Tuple[str, ...] else value[0],
        )


@dataclass
class Columns(DatasetValue):
    add_none: Union[bool, Callable[[str], bool]] = False
    prefer_with: Optional[Callable[[str], bool]] = None

    def get_value(self, dataset, value, type_annotation, mode) -> Tuple[String, Any]:
        if dataset is None:
            return String(tuple()), value

        try:
            columns = list(dataset["dataframe"].columns)
        except KeyError:
            columns = []

        if (isinstance(self.add_none, bool) and self.add_none) or (
            callable(self.add_none) and self.add_none(mode)
        ):
            columns.insert(0, "None")

        if isinstance(value, str):
            value = [value]
        if value is None:
            value = [columns[0]]

        value = DatasetValue._compute_current_values(value, columns, self.prefer_with)

        return (
            String(tuple(columns)),
            value if type_annotation == Tuple[str, ...] else value[0],
        )


@dataclass
class ColumnValue(DatasetValue):
    column: str
    default: List[str]
    prefer_with: Optional[Callable[[str], bool]] = None
    dependency: Optional[Dependency] = None

    def get_value(self, dataset, value, type_annotation, mode) -> Tuple[String, Any]:
        if dataset is None:
            return String(tuple()), value

        try:
            df = dataset["dataframe"]
        except KeyError:
            df = None

        if df is not None:
            if self.dependency is not None and not self.dependency.check(
                [dataset[self.dependency.key]]
            ):
                values = self.default
            elif self.column in df:
                values = [str(v) for v in sorted(list(df[self.column].unique()))]
            else:
                values = self.default
        else:
            values = self.default

        value = DatasetValue._compute_current_values(value, values, self.prefer_with)

        return (
            String(tuple(values)),
            value if type_annotation == Tuple[str, ...] else value[0],
        )