|
1 | | -"""This package includes all the modules related to data loading and preprocessing |
2 | | -
|
3 | | - To add a custom dataset class called 'dummy', you need to add a file called 'dummy_dataset.py' and define a subclass 'DummyDataset' inherited from BaseDataset. |
4 | | - You need to implement four functions: |
5 | | - -- <__init__>: initialize the class, first call BaseDataset.__init__(self, opt). |
6 | | - -- <__len__>: return the size of dataset. |
7 | | - -- <__getitem__>: get a data point from data loader. |
8 | | - -- <modify_commandline_options>: (optionally) add dataset-specific options and set default options. |
9 | | -
|
10 | | -Now you can use the dataset class by specifying flag '--dataset_mode dummy'. |
11 | | -See our template dataset class 'template_dataset.py' for more details. |
12 | | -""" |
13 | | -import os |
14 | | -import sys |
15 | | -project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")) |
16 | | -if project_root not in sys.path: |
17 | | - sys.path.append(project_root) |
18 | | - |
19 | | -import importlib |
20 | | -import torch.utils.data |
21 | | -from portpy.ai.data.base_dataset import BaseDataset |
22 | | - |
23 | | - |
24 | | -def find_dataset_using_name(dataset_name): |
25 | | - """Import the module "data/[dataset_name]_dataset.py". |
26 | | -
|
27 | | - In the file, the class called DatasetNameDataset() will |
28 | | - be instantiated. It has to be a subclass of BaseDataset, |
29 | | - and it is case-insensitive. |
30 | | - """ |
31 | | - dataset_filename = "portpy.ai.data." + dataset_name + "_dataset" |
32 | | - datasetlib = importlib.import_module(dataset_filename) |
33 | | - |
34 | | - dataset = None |
35 | | - target_dataset_name = dataset_name.replace('_', '') + 'dataset' |
36 | | - for name, cls in datasetlib.__dict__.items(): |
37 | | - if name.lower() == target_dataset_name.lower() \ |
38 | | - and issubclass(cls, BaseDataset): |
39 | | - dataset = cls |
40 | | - |
41 | | - if dataset is None: |
42 | | - raise NotImplementedError("In %s.py, there should be a subclass of BaseDataset with class name that matches %s in lowercase." % (dataset_filename, target_dataset_name)) |
43 | | - |
44 | | - return dataset |
45 | | - |
46 | | - |
47 | | -def get_option_setter(dataset_name): |
48 | | - """Return the static method <modify_commandline_options> of the dataset class.""" |
49 | | - dataset_class = find_dataset_using_name(dataset_name) |
50 | | - return dataset_class.modify_commandline_options |
51 | | - |
52 | | - |
53 | | -def create_dataset(opt): |
54 | | - """Create a dataset given the option. |
55 | | -
|
56 | | - This function wraps the class CustomDatasetDataLoader. |
57 | | - This is the main interface between this package and 'train.py'/'test.py' |
58 | | -
|
59 | | - Example: |
60 | | - from data import create_dataset |
61 | | - dataset = create_dataset(opt) |
62 | | - """ |
63 | | - data_loader = CustomDatasetDataLoader(opt) |
64 | | - dataset = data_loader.load_data() |
65 | | - return dataset |
66 | | - |
67 | | - |
68 | | -class CustomDatasetDataLoader(): |
69 | | - """Wrapper class of Dataset class that performs multi-threaded data loading""" |
| 1 | +# Copyright 2025, the PortPy Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 with the Commons Clause restriction. |
| 4 | +# You may obtain a copy of the Apache 2 License at: |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# ---------------------------------------------------------------------- |
| 9 | +# Commons Clause Restriction Notice: |
| 10 | +# PortPy is licensed under Apache 2.0 with the Commons Clause. |
| 11 | +# You may use, modify, and share the code for non-commercial |
| 12 | +# academic and research purposes only. |
| 13 | +# Commercial use — including offering PortPy as a service, |
| 14 | +# or incorporating it into a commercial product — requires |
| 15 | +# a separate commercial license. |
| 16 | +# ---------------------------------------------------------------------- |
70 | 17 |
|
71 | 18 | def __init__(self, opt): |
72 | 19 | """Initialize this class |
|
0 commit comments