18 from typing
import Dict, List, Any
19 from typing_extensions
import Self, override
20 from pathlib
import Path
22 from .base
import BaseImageDataset
23 from ..network_dataset
import NetworkDataset
24 from ...sample
import ImageSample
25 from ...annotation
import ImageDatasetClass, ImageDatasetClasses
26 from ....codable
import KeyDescriptor, Codable
27 from ....networking
import networkManager, FileData, NetworkRequestError
30 class ClassDistribution(Codable):
37 class ImageDataset(BaseImageDataset[ImageSample], NetworkDataset[ImageSample]):
40 Represents the Image Dataset class \n
41 Includes functionality for working with Image Datasets
42 that are uploaded to Coretex.ai
45 classDistribution: List[ClassDistribution]
47 def __init__(self) -> None:
48 super().__init__(ImageSample)
51 def _keyDescriptors(cls) -> Dict[str, KeyDescriptor]:
52 descriptors = super()._keyDescriptors()
54 descriptors[
"samples"] =
KeyDescriptor(
"sessions", ImageSample, list)
55 descriptors[
"classes"] =
KeyDescriptor(
"classes", ImageDatasetClass, ImageDatasetClasses)
56 descriptors[
"classDistribution"] =
KeyDescriptor(
"class_distribution", ClassDistribution, list)
61 def fetchById(cls, objectId: int, **kwargs: Any) -> Self:
62 obj = super().fetchById(objectId, **kwargs)
64 response = networkManager.get(f
"annotation-class?dataset_id={obj.id}")
66 if not response.hasFailed():
67 obj.classes = cls._decodeValue(
"classes", response.getJson(list))
68 obj._writeClassesToFile()
72 def saveClasses(self, classes: ImageDatasetClasses) -> bool:
74 "dataset_id": self.id,
75 "classes": [clazz.encode()
for clazz
in classes]
78 response = networkManager.post(
"annotation-class", parameters)
79 if not response.hasFailed():
80 return super().saveClasses(classes)
82 return not response.hasFailed()
85 def _uploadSample(self, samplePath: Path, sampleName: str, **metadata: Any) -> ImageSample:
91 FileData.createFromPath(
"file", samplePath)
94 response = networkManager.formData(
"session/import", params, files)
95 if response.hasFailed():
96 raise NetworkRequestError(response, f
"Failed to create image Sample from \"{samplePath}\"")
98 return self.
_sampleType_sampleType.decode(response.getJson(dict))