justdata 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. justdata/__init__.py +3 -0
  2. justdata/acoustic/__init__.py +381 -0
  3. justdata/acoustic/adapters.py +329 -0
  4. justdata/acoustic/augment/__init__.py +119 -0
  5. justdata/acoustic/augment/batch.py +813 -0
  6. justdata/acoustic/augment/configs.py +204 -0
  7. justdata/acoustic/augment/patchout.py +331 -0
  8. justdata/acoustic/augment/spectrogram.py +806 -0
  9. justdata/acoustic/augment/waveform.py +879 -0
  10. justdata/acoustic/batching.py +27 -0
  11. justdata/acoustic/channel.py +53 -0
  12. justdata/acoustic/compat/__init__.py +95 -0
  13. justdata/acoustic/compat/ast.py +626 -0
  14. justdata/acoustic/compat/ced.py +152 -0
  15. justdata/acoustic/compat/efficientat.py +243 -0
  16. justdata/acoustic/compat/panns.py +115 -0
  17. justdata/acoustic/compat/passt.py +191 -0
  18. justdata/acoustic/configs.py +519 -0
  19. justdata/acoustic/corruptions/__init__.py +33 -0
  20. justdata/acoustic/corruptions/codec.py +92 -0
  21. justdata/acoustic/corruptions/datasets.py +155 -0
  22. justdata/acoustic/corruptions/device.py +39 -0
  23. justdata/acoustic/corruptions/dynamics.py +55 -0
  24. justdata/acoustic/corruptions/filters.py +105 -0
  25. justdata/acoustic/corruptions/noise.py +173 -0
  26. justdata/acoustic/corruptions/registry.py +230 -0
  27. justdata/acoustic/corruptions/reverb.py +49 -0
  28. justdata/acoustic/corruptions/timefreq.py +127 -0
  29. justdata/acoustic/datasets.py +52 -0
  30. justdata/acoustic/dcase2025.py +814 -0
  31. justdata/acoustic/decoding.py +73 -0
  32. justdata/acoustic/eval_views.py +54 -0
  33. justdata/acoustic/evals.py +71 -0
  34. justdata/acoustic/filters.py +18 -0
  35. justdata/acoustic/frontends/__init__.py +37 -0
  36. justdata/acoustic/frontends/ast.py +116 -0
  37. justdata/acoustic/frontends/kaldi.py +34 -0
  38. justdata/acoustic/frontends/log.py +94 -0
  39. justdata/acoustic/frontends/mel.py +173 -0
  40. justdata/acoustic/frontends/mfcc.py +26 -0
  41. justdata/acoustic/frontends/pcen.py +26 -0
  42. justdata/acoustic/frontends/stft.py +174 -0
  43. justdata/acoustic/jax.py +23 -0
  44. justdata/acoustic/labels.py +169 -0
  45. justdata/acoustic/layouts.py +86 -0
  46. justdata/acoustic/metadata.py +152 -0
  47. justdata/acoustic/normalization.py +196 -0
  48. justdata/acoustic/pipelines.py +112 -0
  49. justdata/acoustic/postprocessing.py +328 -0
  50. justdata/acoustic/preprocessing.py +175 -0
  51. justdata/acoustic/presets.py +257 -0
  52. justdata/acoustic/registry.py +381 -0
  53. justdata/acoustic/resampling.py +218 -0
  54. justdata/acoustic/schema.py +51 -0
  55. justdata/acoustic/segment.py +369 -0
  56. justdata/acoustic/sources.py +503 -0
  57. justdata/acoustic/stages.py +59 -0
  58. justdata/acoustic/stats.py +50 -0
  59. justdata/acoustic/tasks.py +375 -0
  60. justdata/audio/__init__.py +20 -0
  61. justdata/core/__init__.py +74 -0
  62. justdata/core/adapters.py +46 -0
  63. justdata/core/augmentations.py +34 -0
  64. justdata/core/filters.py +94 -0
  65. justdata/core/finalization.py +173 -0
  66. justdata/core/label_mixing.py +129 -0
  67. justdata/core/loader.py +384 -0
  68. justdata/core/metadata.py +250 -0
  69. justdata/core/presets.py +155 -0
  70. justdata/core/registry.py +229 -0
  71. justdata/core/sources.py +87 -0
  72. justdata/core/stats.py +227 -0
  73. justdata/corruptions.py +52 -0
  74. justdata/py.typed +0 -0
  75. justdata/vision/__init__.py +17 -0
  76. justdata/vision/augmentations/__init__.py +31 -0
  77. justdata/vision/augmentations/auto.py +480 -0
  78. justdata/vision/augmentations/color.py +163 -0
  79. justdata/vision/augmentations/composed.py +156 -0
  80. justdata/vision/augmentations/geometric.py +302 -0
  81. justdata/vision/augmentations/mixing.py +232 -0
  82. justdata/vision/augmentations/registry.py +142 -0
  83. justdata/vision/corruptions/__init__.py +16 -0
  84. justdata/vision/corruptions/blur.py +65 -0
  85. justdata/vision/corruptions/digital.py +26 -0
  86. justdata/vision/corruptions/noise.py +20 -0
  87. justdata/vision/corruptions/registry.py +113 -0
  88. justdata/vision/corruptions/weather.py +100 -0
  89. justdata/vision/datasets.py +34 -0
  90. justdata/vision/encodings/__init__.py +1 -0
  91. justdata/vision/encodings/heatmaps.py +196 -0
  92. justdata/vision/minic.py +69 -0
  93. justdata/vision/pipelines.py +62 -0
  94. justdata/vision/presets.py +509 -0
  95. justdata/vision/sources.py +1080 -0
  96. justdata/vision/stages.py +271 -0
  97. justdata/vision/tasks/__init__.py +1 -0
  98. justdata/vision/tasks/classification.py +321 -0
  99. justdata/vision/tasks/depth.py +25 -0
  100. justdata/vision/tasks/segmentation.py +194 -0
  101. justdata/vision/transforms.py +113 -0
  102. justdata/vision/utils.py +741 -0
  103. justdata-1.0.0.dist-info/METADATA +860 -0
  104. justdata-1.0.0.dist-info/RECORD +106 -0
  105. justdata-1.0.0.dist-info/WHEEL +4 -0
  106. justdata-1.0.0.dist-info/licenses/LICENSE +23 -0
@@ -0,0 +1,519 @@
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ import hashlib
5
+ import json
6
+ from collections.abc import Mapping
7
+ from dataclasses import dataclass, field
8
+ from typing import Any, Literal, TypeVar
9
+
10
+
11
+ ConfigT = TypeVar("ConfigT", bound="_SerializableConfig")
12
+
13
+
14
+ def _canonical_json(config: Any) -> str:
15
+ return json.dumps(dataclasses.asdict(config), sort_keys=True, separators=(",", ":"))
16
+
17
+
18
+ def _short_hash(config: Any) -> str:
19
+ return hashlib.sha256(_canonical_json(config).encode("utf-8")).hexdigest()[:16]
20
+
21
+
22
+ def _ensure_literal(field_name: str, value: Any, allowed: set[Any]) -> None:
23
+ if value not in allowed:
24
+ choices = ", ".join(repr(v) for v in sorted(allowed, key=str))
25
+ raise ValueError(f"{field_name} must be one of {choices}; got {value!r}")
26
+
27
+
28
+ def _ensure_positive(field_name: str, value: int | float | None) -> None:
29
+ if (
30
+ value is None
31
+ or isinstance(value, bool)
32
+ or not isinstance(value, (int, float))
33
+ or value <= 0
34
+ ):
35
+ raise ValueError(f"{field_name} must be positive; got {value!r}")
36
+
37
+
38
+ def _tuple_value(value: Any) -> Any:
39
+ if isinstance(value, list):
40
+ return tuple(value)
41
+ return value
42
+
43
+
44
+ def _tuple_or_scalar_value(value: Any) -> Any:
45
+ if isinstance(value, list):
46
+ return tuple(value)
47
+ return value
48
+
49
+
50
+ class _SerializableConfig:
51
+ def __post_init__(self) -> None:
52
+ self.validate()
53
+
54
+ def to_dict(self) -> dict[str, Any]:
55
+ return dataclasses.asdict(self)
56
+
57
+ def to_json(self) -> str:
58
+ return _canonical_json(self)
59
+
60
+ @classmethod
61
+ def from_dict(cls: type[ConfigT], data: Mapping[str, Any] | ConfigT) -> ConfigT:
62
+ if isinstance(data, cls):
63
+ return data
64
+ return cls(**dict(data))
65
+
66
+ @classmethod
67
+ def from_json(cls: type[ConfigT], data: str) -> ConfigT:
68
+ return cls.from_dict(json.loads(data))
69
+
70
+ def hash(self) -> str:
71
+ return _short_hash(self)
72
+
73
+ def validate(self) -> _SerializableConfig:
74
+ return self
75
+
76
+
77
+ @dataclass(frozen=True)
78
+ class AudioPreprocessConfig(_SerializableConfig):
79
+ target_sample_rate: int
80
+ dtype: Literal["float32"] = "float32"
81
+ waveform_range: Literal["[-1,1]"] = "[-1,1]"
82
+ channel_strategy: Literal["mono_mean", "mono_left", "mono_right", "keep"] = (
83
+ "mono_mean"
84
+ )
85
+ resampler: Literal["tensorflow", "soxr", "kaiser_best", "hf_audio", "identity"] = (
86
+ "tensorflow"
87
+ )
88
+ normalize_waveform: Literal["none", "peak", "rms", "lufs"] = "none"
89
+ remove_dc_offset: bool = False
90
+ clip_value: float | None = None
91
+ cache_after_resample: bool = True
92
+
93
+ def validate(self) -> AudioPreprocessConfig:
94
+ _ensure_positive("target_sample_rate", self.target_sample_rate)
95
+ _ensure_literal("dtype", self.dtype, {"float32"})
96
+ _ensure_literal("waveform_range", self.waveform_range, {"[-1,1]"})
97
+ _ensure_literal(
98
+ "channel_strategy",
99
+ self.channel_strategy,
100
+ {"mono_mean", "mono_left", "mono_right", "keep"},
101
+ )
102
+ _ensure_literal(
103
+ "resampler",
104
+ self.resampler,
105
+ {"tensorflow", "soxr", "kaiser_best", "hf_audio", "identity"},
106
+ )
107
+ _ensure_literal(
108
+ "normalize_waveform",
109
+ self.normalize_waveform,
110
+ {"none", "peak", "rms", "lufs"},
111
+ )
112
+ if self.clip_value is not None and self.clip_value <= 0:
113
+ raise ValueError("clip_value must be positive when set")
114
+ return self
115
+
116
+
117
+ @dataclass(frozen=True)
118
+ class SegmentStrategyConfig(_SerializableConfig):
119
+ clip_duration: float
120
+ train_mode: Literal["random_crop", "center_crop", "full", "sliding", "pad_or_crop"]
121
+ eval_mode: Literal["center_crop", "full", "sliding", "multi_crop"]
122
+ pad_mode: Literal["zero", "repeat", "reflect"]
123
+ pad_position: Literal["right", "center", "random"]
124
+ num_views: int = 1
125
+ sliding_hop_duration: float | None = None
126
+ allow_train_sliding: bool = False
127
+ drop_short: bool = False
128
+ min_duration: float | None = None
129
+ duration_policy: Literal[
130
+ "keep_1s",
131
+ "pad_to_model_duration",
132
+ "tile_to_model_duration",
133
+ "repeat_pad_to_model_duration",
134
+ "sliding_windows",
135
+ "none",
136
+ ] = "none"
137
+
138
+ def validate(self) -> SegmentStrategyConfig:
139
+ _ensure_positive("clip_duration", self.clip_duration)
140
+ _ensure_literal(
141
+ "train_mode",
142
+ self.train_mode,
143
+ {"random_crop", "center_crop", "full", "sliding", "pad_or_crop"},
144
+ )
145
+ _ensure_literal(
146
+ "eval_mode",
147
+ self.eval_mode,
148
+ {"center_crop", "full", "sliding", "multi_crop"},
149
+ )
150
+ _ensure_literal("pad_mode", self.pad_mode, {"zero", "repeat", "reflect"})
151
+ _ensure_literal(
152
+ "pad_position", self.pad_position, {"right", "center", "random"}
153
+ )
154
+ _ensure_positive("num_views", self.num_views)
155
+ if self.sliding_hop_duration is not None:
156
+ _ensure_positive("sliding_hop_duration", self.sliding_hop_duration)
157
+ _ensure_literal(
158
+ "duration_policy",
159
+ self.duration_policy,
160
+ {
161
+ "keep_1s",
162
+ "pad_to_model_duration",
163
+ "tile_to_model_duration",
164
+ "repeat_pad_to_model_duration",
165
+ "sliding_windows",
166
+ "none",
167
+ },
168
+ )
169
+ if self.drop_short:
170
+ raise ValueError(
171
+ "drop_short is not supported because dataset-level filtering "
172
+ "is not implemented"
173
+ )
174
+ if self.min_duration is not None:
175
+ raise ValueError(
176
+ "min_duration is not supported because dataset-level filtering "
177
+ "is not implemented"
178
+ )
179
+ return self
180
+
181
+
182
+ @dataclass(frozen=True)
183
+ class STFTConfig(_SerializableConfig):
184
+ sample_rate: int
185
+ n_fft: int
186
+ win_length: int
187
+ hop_length: int
188
+ window: Literal["hann", "hamming", "povey", "rectangular"] = "hann"
189
+ window_periodic: bool = True
190
+ periodic: bool | None = None
191
+ center: bool = True
192
+ pad_mode: Literal["reflect", "constant"] = "reflect"
193
+ power: float = 2.0
194
+ normalized: bool = False
195
+ onesided: bool = True
196
+ eps: float = 1e-10
197
+
198
+ def __post_init__(self) -> None:
199
+ if self.periodic is not None:
200
+ object.__setattr__(self, "window_periodic", bool(self.periodic))
201
+ object.__setattr__(self, "periodic", None)
202
+ self.validate()
203
+
204
+ def validate(self) -> STFTConfig:
205
+ _ensure_positive("sample_rate", self.sample_rate)
206
+ _ensure_positive("n_fft", self.n_fft)
207
+ _ensure_positive("win_length", self.win_length)
208
+ _ensure_positive("hop_length", self.hop_length)
209
+ if self.win_length > self.n_fft:
210
+ raise ValueError("win_length must be <= n_fft")
211
+ _ensure_literal(
212
+ "window", self.window, {"hann", "hamming", "povey", "rectangular"}
213
+ )
214
+ _ensure_literal("pad_mode", self.pad_mode, {"reflect", "constant"})
215
+ _ensure_positive("power", self.power)
216
+ _ensure_positive("eps", self.eps)
217
+ return self
218
+
219
+
220
+ @dataclass(frozen=True)
221
+ class MelConfig(_SerializableConfig):
222
+ n_mels: int
223
+ f_min: float = 0.0
224
+ f_max: float | None = None
225
+ mel_scale: Literal["htk", "slaney"] = "htk"
226
+ mel_norm: Literal["none", "slaney"] = "none"
227
+ filterbank_impl: Literal[
228
+ "tf",
229
+ "librosa",
230
+ "torchaudio",
231
+ "kaldi_compatible",
232
+ "torchaudio_or_kaldi_compatible",
233
+ "efficientat_kaldi",
234
+ ] = "tf"
235
+
236
+ def validate(self) -> MelConfig:
237
+ _ensure_positive("n_mels", self.n_mels)
238
+ if self.f_min < 0:
239
+ raise ValueError("f_min must be non-negative")
240
+ if self.f_max is not None and self.f_max <= self.f_min:
241
+ raise ValueError("f_max must be greater than f_min")
242
+ _ensure_literal("mel_scale", self.mel_scale, {"htk", "slaney"})
243
+ _ensure_literal("mel_norm", self.mel_norm, {"none", "slaney"})
244
+ _ensure_literal(
245
+ "filterbank_impl",
246
+ self.filterbank_impl,
247
+ {
248
+ "tf",
249
+ "librosa",
250
+ "torchaudio",
251
+ "kaldi_compatible",
252
+ "torchaudio_or_kaldi_compatible",
253
+ "efficientat_kaldi",
254
+ },
255
+ )
256
+ return self
257
+
258
+
259
+ @dataclass(frozen=True)
260
+ class LogCompressionConfig(_SerializableConfig):
261
+ kind: Literal["log", "log10", "db", "pcen"] = "log"
262
+ amin: float = 1e-10
263
+ ref: float | Literal["max"] = 1.0
264
+ top_db: float | None = None
265
+ log_offset: float = 0.0
266
+ alpha: float = 0.98
267
+ delta: float = 2.0
268
+ r: float = 0.5
269
+ smooth_coef: float = 0.025
270
+ eps: float = 1e-6
271
+
272
+ def validate(self) -> LogCompressionConfig:
273
+ _ensure_literal("kind", self.kind, {"log", "log10", "db", "pcen"})
274
+ _ensure_positive("amin", self.amin)
275
+ if self.ref != "max":
276
+ _ensure_positive("ref", self.ref)
277
+ if self.top_db is not None:
278
+ _ensure_positive("top_db", self.top_db)
279
+ if self.log_offset < 0:
280
+ raise ValueError("log_offset must be non-negative")
281
+ _ensure_positive("delta", self.delta)
282
+ _ensure_positive("eps", self.eps)
283
+ if not 0.0 <= self.alpha <= 1.0:
284
+ raise ValueError("alpha must be in [0, 1]")
285
+ if not 0.0 < self.r <= 1.0:
286
+ raise ValueError("r must be in (0, 1]")
287
+ if not 0.0 < self.smooth_coef <= 1.0:
288
+ raise ValueError("smooth_coef must be in (0, 1]")
289
+ return self
290
+
291
+
292
+ @dataclass(frozen=True)
293
+ class FeatureNormConfig(_SerializableConfig):
294
+ kind: Literal[
295
+ "none",
296
+ "dataset_mean_std",
297
+ "per_clip_mean_std",
298
+ "per_frequency_mean_std",
299
+ "kaldi_cmvn",
300
+ "checkpoint_mean_std",
301
+ "affine",
302
+ ] = "none"
303
+ mean: tuple[float, ...] | str | None = None
304
+ std: tuple[float, ...] | str | None = None
305
+ scale: tuple[float, ...] | float | None = None
306
+ bias: tuple[float, ...] | float | None = None
307
+ axes: tuple[str, ...] = ()
308
+ eps: float = 1e-6
309
+
310
+ def __post_init__(self) -> None:
311
+ object.__setattr__(self, "mean", _tuple_value(self.mean))
312
+ object.__setattr__(self, "std", _tuple_value(self.std))
313
+ object.__setattr__(self, "scale", _tuple_or_scalar_value(self.scale))
314
+ object.__setattr__(self, "bias", _tuple_or_scalar_value(self.bias))
315
+ object.__setattr__(self, "axes", _tuple_value(self.axes))
316
+ self.validate()
317
+
318
+ def validate(self) -> FeatureNormConfig:
319
+ _ensure_literal(
320
+ "kind",
321
+ self.kind,
322
+ {
323
+ "none",
324
+ "dataset_mean_std",
325
+ "per_clip_mean_std",
326
+ "per_frequency_mean_std",
327
+ "kaldi_cmvn",
328
+ "checkpoint_mean_std",
329
+ "affine",
330
+ },
331
+ )
332
+ for field_name, value in (("mean", self.mean), ("std", self.std)):
333
+ if value is not None and not isinstance(value, (tuple, str)):
334
+ raise ValueError(f"{field_name} must be a tuple, string, or None")
335
+ for field_name, value in (("scale", self.scale), ("bias", self.bias)):
336
+ if value is not None and not isinstance(value, (tuple, int, float)):
337
+ raise ValueError(f"{field_name} must be a tuple, number, or None")
338
+ if not isinstance(self.axes, tuple):
339
+ raise ValueError("axes must be a tuple")
340
+ _ensure_positive("eps", self.eps)
341
+ return self
342
+
343
+
344
+ @dataclass(frozen=True)
345
+ class FrontendConfig(_SerializableConfig):
346
+ name: Literal[
347
+ "raw_waveform",
348
+ "ast_kaldi_fbank",
349
+ "stft_magnitude",
350
+ "mel_power",
351
+ "logmel",
352
+ "efficientat_logmel",
353
+ "kaldi_fbank",
354
+ "mfcc",
355
+ "pcen_mel",
356
+ ]
357
+ stft: STFTConfig | None = None
358
+ mel: MelConfig | None = None
359
+ log: LogCompressionConfig | None = None
360
+ norm: FeatureNormConfig = FeatureNormConfig()
361
+ n_mfcc: int = 13
362
+
363
+ def __post_init__(self) -> None:
364
+ if isinstance(self.stft, Mapping):
365
+ object.__setattr__(self, "stft", STFTConfig.from_dict(self.stft))
366
+ if isinstance(self.mel, Mapping):
367
+ object.__setattr__(self, "mel", MelConfig.from_dict(self.mel))
368
+ if isinstance(self.log, Mapping):
369
+ object.__setattr__(self, "log", LogCompressionConfig.from_dict(self.log))
370
+ if isinstance(self.norm, Mapping):
371
+ object.__setattr__(self, "norm", FeatureNormConfig.from_dict(self.norm))
372
+ self.validate()
373
+
374
+ def validate(self) -> FrontendConfig:
375
+ _ensure_literal(
376
+ "name",
377
+ self.name,
378
+ {
379
+ "raw_waveform",
380
+ "ast_kaldi_fbank",
381
+ "stft_magnitude",
382
+ "mel_power",
383
+ "logmel",
384
+ "efficientat_logmel",
385
+ "kaldi_fbank",
386
+ "mfcc",
387
+ "pcen_mel",
388
+ },
389
+ )
390
+ if self.stft is not None and not isinstance(self.stft, STFTConfig):
391
+ raise ValueError("stft must be an STFTConfig or None")
392
+ if self.mel is not None and not isinstance(self.mel, MelConfig):
393
+ raise ValueError("mel must be a MelConfig or None")
394
+ if self.log is not None and not isinstance(self.log, LogCompressionConfig):
395
+ raise ValueError("log must be a LogCompressionConfig or None")
396
+ if not isinstance(self.norm, FeatureNormConfig):
397
+ raise ValueError("norm must be a FeatureNormConfig")
398
+ _ensure_positive("n_mfcc", self.n_mfcc)
399
+ return self
400
+
401
+
402
+ @dataclass(frozen=True)
403
+ class LabelTransformConfig(_SerializableConfig):
404
+ mode: Literal["index", "one_hot", "multi_hot", "text", "event_frames"]
405
+ num_classes: int | None = None
406
+ class_names: tuple[str, ...] | None = None
407
+ smoothing: float = 0.0
408
+ keep_hard_label_in_metadata: bool = True
409
+ num_frames: int | None = None
410
+ frame_hop_seconds: float | None = None
411
+ hop_length: int | None = None
412
+ sample_rate: int | None = None
413
+
414
+ def __post_init__(self) -> None:
415
+ object.__setattr__(self, "class_names", _tuple_value(self.class_names))
416
+ self.validate()
417
+
418
+ def validate(self) -> LabelTransformConfig:
419
+ _ensure_literal(
420
+ "mode", self.mode, {"index", "one_hot", "multi_hot", "text", "event_frames"}
421
+ )
422
+ if self.num_classes is not None:
423
+ _ensure_positive("num_classes", self.num_classes)
424
+ if self.class_names is not None and not isinstance(self.class_names, tuple):
425
+ raise ValueError("class_names must be a tuple or None")
426
+ if not 0.0 <= self.smoothing < 1.0:
427
+ raise ValueError("smoothing must be in [0, 1)")
428
+ if self.num_frames is not None:
429
+ _ensure_positive("num_frames", self.num_frames)
430
+ if self.frame_hop_seconds is not None:
431
+ _ensure_positive("frame_hop_seconds", self.frame_hop_seconds)
432
+ if self.hop_length is not None:
433
+ _ensure_positive("hop_length", self.hop_length)
434
+ if self.sample_rate is not None:
435
+ _ensure_positive("sample_rate", self.sample_rate)
436
+ return self
437
+
438
+
439
+ @dataclass(frozen=True)
440
+ class AudioPreset(_SerializableConfig):
441
+ name: str
442
+ input_duration: float | None
443
+ target_sample_rate: int
444
+ preprocess: AudioPreprocessConfig
445
+ segment: SegmentStrategyConfig
446
+ frontend: FrontendConfig
447
+ label_transform: LabelTransformConfig
448
+ layout: Literal["bt", "btc", "btf", "bft", "bcft", "btfc"]
449
+ output_key: str | None = None
450
+ static_shape: tuple[int | None, ...] | None = None
451
+ dtype: Literal["float32", "float16", "bfloat16"] = "float32"
452
+ train_augment: dict[str, Any] = field(default_factory=dict)
453
+ eval_views: dict[str, Any] = field(default_factory=dict)
454
+ metadata: dict[str, Any] = field(default_factory=dict)
455
+ metadata_mode: Literal["full", "numeric_only", "none"] = "numeric_only"
456
+
457
+ def __post_init__(self) -> None:
458
+ if isinstance(self.preprocess, Mapping):
459
+ object.__setattr__(
460
+ self, "preprocess", AudioPreprocessConfig.from_dict(self.preprocess)
461
+ )
462
+ if isinstance(self.segment, Mapping):
463
+ object.__setattr__(
464
+ self, "segment", SegmentStrategyConfig.from_dict(self.segment)
465
+ )
466
+ if isinstance(self.frontend, Mapping):
467
+ object.__setattr__(
468
+ self, "frontend", FrontendConfig.from_dict(self.frontend)
469
+ )
470
+ if isinstance(self.label_transform, Mapping):
471
+ object.__setattr__(
472
+ self,
473
+ "label_transform",
474
+ LabelTransformConfig.from_dict(self.label_transform),
475
+ )
476
+ object.__setattr__(self, "static_shape", _tuple_value(self.static_shape))
477
+ object.__setattr__(self, "train_augment", dict(self.train_augment))
478
+ object.__setattr__(self, "eval_views", dict(self.eval_views))
479
+ object.__setattr__(self, "metadata", dict(self.metadata))
480
+ self.validate()
481
+
482
+ @classmethod
483
+ def from_dict(cls, data: Mapping[str, Any] | AudioPreset) -> AudioPreset:
484
+ if isinstance(data, cls):
485
+ return data
486
+ if "target_sample_rate" not in data:
487
+ raise ValueError("AudioPreset requires target_sample_rate")
488
+ return cls(**dict(data))
489
+
490
+ def validate(self) -> AudioPreset:
491
+ if not self.name:
492
+ raise ValueError("name must be non-empty")
493
+ _ensure_positive("target_sample_rate", self.target_sample_rate)
494
+ if self.input_duration is not None:
495
+ _ensure_positive("input_duration", self.input_duration)
496
+ if not isinstance(self.preprocess, AudioPreprocessConfig):
497
+ raise ValueError("preprocess must be an AudioPreprocessConfig")
498
+ if not isinstance(self.segment, SegmentStrategyConfig):
499
+ raise ValueError("segment must be a SegmentStrategyConfig")
500
+ if not isinstance(self.frontend, FrontendConfig):
501
+ raise ValueError("frontend must be a FrontendConfig")
502
+ if not isinstance(self.label_transform, LabelTransformConfig):
503
+ raise ValueError("label_transform must be a LabelTransformConfig")
504
+ if self.preprocess.target_sample_rate != self.target_sample_rate:
505
+ raise ValueError(
506
+ "target_sample_rate must match preprocess.target_sample_rate"
507
+ )
508
+ _ensure_literal(
509
+ "layout", self.layout, {"bt", "btc", "btf", "bft", "bcft", "btfc"}
510
+ )
511
+ if self.output_key is not None and not self.output_key:
512
+ raise ValueError("output_key must be non-empty when set")
513
+ if self.static_shape is not None and not isinstance(self.static_shape, tuple):
514
+ raise ValueError("static_shape must be a tuple or None")
515
+ _ensure_literal("dtype", self.dtype, {"float32", "float16", "bfloat16"})
516
+ _ensure_literal(
517
+ "metadata_mode", self.metadata_mode, {"full", "numeric_only", "none"}
518
+ )
519
+ return self
@@ -0,0 +1,33 @@
1
+ from justdata.acoustic.corruptions import (
2
+ codec,
3
+ device,
4
+ dynamics,
5
+ filters,
6
+ noise,
7
+ reverb,
8
+ timefreq,
9
+ )
10
+ from justdata.acoustic.corruptions.datasets import create_audio_corruption_datasets
11
+ from justdata.acoustic.corruptions.registry import (
12
+ apply_audio_corruption,
13
+ get_audio_corruption,
14
+ has_audio_corruption,
15
+ list_audio_corruptions,
16
+ register_audio_corruption,
17
+ )
18
+
19
+ __all__ = [
20
+ "apply_audio_corruption",
21
+ "codec",
22
+ "create_audio_corruption_datasets",
23
+ "device",
24
+ "dynamics",
25
+ "filters",
26
+ "get_audio_corruption",
27
+ "has_audio_corruption",
28
+ "list_audio_corruptions",
29
+ "noise",
30
+ "register_audio_corruption",
31
+ "reverb",
32
+ "timefreq",
33
+ ]
@@ -0,0 +1,92 @@
1
+ from __future__ import annotations
2
+
3
+ import tensorflow as tf
4
+
5
+ from justdata.acoustic.corruptions.registry import (
6
+ _as_tc,
7
+ _fit_length,
8
+ _resize_time,
9
+ _restore_rank,
10
+ _sample_rate,
11
+ _severity_value,
12
+ register_audio_corruption,
13
+ )
14
+
15
+
16
+ CODEC_BITRATE_KBPS = {1: 128, 2: 96, 3: 64, 4: 32, 5: 16}
17
+ CODEC_MULAW_LEVELS = {1: 256, 2: 128, 3: 64, 4: 32, 5: 16}
18
+ RESAMPLING_INTERMEDIATE_HZ_32K = {1: 24000, 2: 16000, 3: 12000, 4: 8000, 5: 4000}
19
+
20
+
21
+ def _quantize_unit(x: tf.Tensor, levels: tf.Tensor) -> tf.Tensor:
22
+ levels = tf.cast(tf.maximum(levels, 2), tf.float32)
23
+ x = tf.clip_by_value(x, -1.0, 1.0)
24
+ return tf.round((x + 1.0) * 0.5 * (levels - 1.0)) / (levels - 1.0) * 2.0 - 1.0
25
+
26
+
27
+ def _mulaw_roundtrip(x: tf.Tensor, levels: tf.Tensor) -> tf.Tensor:
28
+ mu = tf.cast(tf.maximum(levels - 1, 1), tf.float32)
29
+ log_mu = tf.math.log1p(mu)
30
+ encoded = tf.sign(x) * tf.math.log1p(mu * tf.abs(x)) / log_mu
31
+ encoded = _quantize_unit(encoded, levels)
32
+ return tf.sign(encoded) * tf.math.expm1(tf.abs(encoded) * log_mu) / mu
33
+
34
+
35
+ @register_audio_corruption("codec_compression")
36
+ def codec_compression(
37
+ audio_or_features: tf.Tensor,
38
+ severity: int,
39
+ seed: tf.Tensor,
40
+ config: dict | None = None,
41
+ ) -> tf.Tensor:
42
+ del seed
43
+ levels = _severity_value(CODEC_MULAW_LEVELS, severity, dtype=tf.int32)
44
+ if config is not None and "levels" in config:
45
+ levels = tf.cast(config["levels"], tf.int32)
46
+ x = tf.cast(audio_or_features, tf.float32)
47
+ clipped = tf.clip_by_value(x, -1.0, 1.0)
48
+ return tf.clip_by_value(_mulaw_roundtrip(clipped, levels), -1.0, 1.0)
49
+
50
+
51
+ @register_audio_corruption("resampling_degradation")
52
+ def resampling_degradation(
53
+ audio_or_features: tf.Tensor,
54
+ severity: int,
55
+ seed: tf.Tensor,
56
+ config: dict | None = None,
57
+ ) -> tf.Tensor:
58
+ del seed
59
+ sample_rate = _sample_rate(config)
60
+ intermediate_hz = _severity_value(
61
+ RESAMPLING_INTERMEDIATE_HZ_32K,
62
+ severity,
63
+ dtype=tf.int32,
64
+ )
65
+ if config is not None and "intermediate_sample_rate" in config:
66
+ intermediate_hz = tf.cast(config["intermediate_sample_rate"], tf.int32)
67
+
68
+ audio, rank = _as_tc(audio_or_features)
69
+ length = tf.shape(audio)[0]
70
+ intermediate_length = tf.cast(
71
+ tf.maximum(
72
+ tf.round(
73
+ tf.cast(length, tf.float32)
74
+ * tf.cast(intermediate_hz, tf.float32)
75
+ / sample_rate
76
+ ),
77
+ 1.0,
78
+ ),
79
+ tf.int32,
80
+ )
81
+ degraded = _resize_time(audio, intermediate_length)
82
+ restored = _fit_length(_resize_time(degraded, length), length)
83
+ return _restore_rank(restored, rank)
84
+
85
+
86
+ __all__ = [
87
+ "CODEC_BITRATE_KBPS",
88
+ "CODEC_MULAW_LEVELS",
89
+ "RESAMPLING_INTERMEDIATE_HZ_32K",
90
+ "codec_compression",
91
+ "resampling_degradation",
92
+ ]