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
justdata/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ __version__ = "1.0.0"
2
+
3
+ __all__ = ["__version__"]
@@ -0,0 +1,381 @@
1
+ from justdata.acoustic import (
2
+ adapters,
3
+ augment,
4
+ batching,
5
+ channel,
6
+ compat,
7
+ configs,
8
+ corruptions,
9
+ datasets,
10
+ decoding,
11
+ dcase2025,
12
+ evals,
13
+ eval_views,
14
+ filters,
15
+ frontends,
16
+ jax,
17
+ labels,
18
+ layouts,
19
+ metadata,
20
+ normalization,
21
+ pipelines,
22
+ postprocessing,
23
+ preprocessing,
24
+ presets,
25
+ registry,
26
+ resampling,
27
+ schema,
28
+ segment,
29
+ sources,
30
+ stages,
31
+ stats,
32
+ tasks,
33
+ )
34
+ from justdata.acoustic.adapters import (
35
+ AcousticAdapter,
36
+ adapt_acoustic_sample,
37
+ ensure_audio_rank,
38
+ infer_duration,
39
+ make_audio_metadata,
40
+ standardize_waveform_layout,
41
+ to_float32_waveform,
42
+ )
43
+ from justdata.acoustic.batching import pad_nested
44
+ from justdata.acoustic.configs import (
45
+ AudioPreset,
46
+ AudioPreprocessConfig,
47
+ FeatureNormConfig,
48
+ FrontendConfig,
49
+ LabelTransformConfig,
50
+ LogCompressionConfig,
51
+ MelConfig,
52
+ STFTConfig,
53
+ SegmentStrategyConfig,
54
+ )
55
+ from justdata.acoustic.corruptions import (
56
+ apply_audio_corruption,
57
+ create_audio_corruption_datasets,
58
+ )
59
+ from justdata.acoustic.datasets import (
60
+ AUDIO_CLASSIFICATION_MULTI_LABEL,
61
+ AUDIO_CLASSIFICATION_SINGLE_LABEL,
62
+ AUDIO_EVENT_DETECTION,
63
+ AUDIO_RETRIEVAL,
64
+ AUDIO_SELF_SUPERVISED,
65
+ AUDIO_TEXT_PAIR,
66
+ )
67
+ from justdata.acoustic.decoding import (
68
+ decode_audio_file,
69
+ decode_wav_bytes,
70
+ decode_wav_file,
71
+ )
72
+ from justdata.acoustic.dcase2025 import (
73
+ DCASE2025Task1Adapter,
74
+ SplitLeakageError,
75
+ assert_stats_allowed,
76
+ make_source_dataset,
77
+ make_target_dataset,
78
+ parse_city_from_row,
79
+ parse_device_from_row,
80
+ parse_scene_from_row,
81
+ parse_source_recording_id,
82
+ )
83
+ from justdata.acoustic.evals import (
84
+ confusion_matrix,
85
+ macro_classwise_accuracy,
86
+ per_device_accuracy,
87
+ per_scene_accuracy,
88
+ )
89
+ from justdata.acoustic.filters import filter_by_metadata, groupby_metadata
90
+ from justdata.acoustic.channel import (
91
+ apply_channel_strategy,
92
+ keep,
93
+ mono_left,
94
+ mono_mean,
95
+ mono_right,
96
+ )
97
+ from justdata.acoustic.eval_views import generate_eval_views, make_eval_views
98
+ from justdata.acoustic.frontends import (
99
+ ast_kaldi_fbank,
100
+ compress_log,
101
+ compute_num_frames,
102
+ kaldi_fbank,
103
+ logmel,
104
+ mel_power,
105
+ mel_spectrogram,
106
+ mel_weight_matrix,
107
+ mfcc,
108
+ pcen_mel,
109
+ raw_waveform,
110
+ stft_magnitude,
111
+ stft_power_spectrogram,
112
+ )
113
+ from justdata.acoustic.jax import as_numpy_iterator, to_numpy
114
+ from justdata.acoustic.labels import transform_label
115
+ from justdata.acoustic.layouts import convert_audio_layout
116
+ from justdata.acoustic.metadata import (
117
+ MetadataEncoder,
118
+ MetadataSidecar,
119
+ stable_int64_hash,
120
+ )
121
+ from justdata.acoustic.normalization import apply_feature_normalization
122
+ from justdata.acoustic.postprocessing import (
123
+ default_output_key,
124
+ expected_audio_static_shape,
125
+ frontend_output_kind,
126
+ make_model_input_stage,
127
+ set_static_audio_shape,
128
+ )
129
+ from justdata.acoustic.preprocessing import (
130
+ make_preprocessing,
131
+ normalize_waveform,
132
+ remove_dc_offset,
133
+ )
134
+ from justdata.acoustic.presets import (
135
+ get_dataset_presets,
136
+ get_resolved_preset,
137
+ merge_with_presets,
138
+ register_preset,
139
+ )
140
+ from justdata.acoustic.resampling import resample_sample, resample_waveform
141
+ from justdata.acoustic.segment import pad_waveform, segment_waveform
142
+ from justdata.acoustic.stages import make_preprocessing_stage, make_segment_stage
143
+ from justdata.acoustic.stats import (
144
+ compute_feature_stats,
145
+ make_stats_iterator,
146
+ )
147
+ from justdata.acoustic.registry import (
148
+ get_audio_batch_augment,
149
+ get_audio_batch_augment_metadata,
150
+ get_audio_channel_strategy,
151
+ get_audio_corruption,
152
+ get_audio_decoder,
153
+ get_audio_eval_view_strategy,
154
+ get_audio_frontend,
155
+ get_audio_normalization,
156
+ get_audio_postprocessor,
157
+ get_audio_resampler,
158
+ get_audio_segment_strategy,
159
+ get_audio_spectrogram_augment,
160
+ get_audio_spectrogram_augment_metadata,
161
+ get_audio_waveform_augment,
162
+ get_audio_waveform_augment_metadata,
163
+ has_audio_batch_augment,
164
+ has_audio_channel_strategy,
165
+ has_audio_corruption,
166
+ has_audio_decoder,
167
+ has_audio_eval_view_strategy,
168
+ has_audio_frontend,
169
+ has_audio_normalization,
170
+ has_audio_postprocessor,
171
+ has_audio_resampler,
172
+ has_audio_segment_strategy,
173
+ has_audio_spectrogram_augment,
174
+ has_audio_waveform_augment,
175
+ list_audio_batch_augments,
176
+ list_audio_batch_augment_metadata,
177
+ list_audio_channel_strategies,
178
+ list_audio_corruptions,
179
+ list_audio_decoders,
180
+ list_audio_eval_view_strategies,
181
+ list_audio_frontends,
182
+ list_audio_normalizations,
183
+ list_audio_postprocessors,
184
+ list_audio_resamplers,
185
+ list_audio_segment_strategies,
186
+ list_audio_spectrogram_augments,
187
+ list_audio_spectrogram_augment_metadata,
188
+ list_audio_waveform_augments,
189
+ list_audio_waveform_augment_metadata,
190
+ register_audio_batch_augment,
191
+ register_audio_channel_strategy,
192
+ register_audio_corruption,
193
+ register_audio_decoder,
194
+ register_audio_eval_view_strategy,
195
+ register_audio_frontend,
196
+ register_audio_normalization,
197
+ register_audio_postprocessor,
198
+ register_audio_resampler,
199
+ register_audio_segment_strategy,
200
+ register_audio_spectrogram_augment,
201
+ register_audio_waveform_augment,
202
+ )
203
+
204
+ __all__ = [
205
+ "AUDIO_CLASSIFICATION_MULTI_LABEL",
206
+ "AUDIO_CLASSIFICATION_SINGLE_LABEL",
207
+ "AUDIO_EVENT_DETECTION",
208
+ "AUDIO_RETRIEVAL",
209
+ "AUDIO_SELF_SUPERVISED",
210
+ "AUDIO_TEXT_PAIR",
211
+ "AcousticAdapter",
212
+ "AudioPreset",
213
+ "AudioPreprocessConfig",
214
+ "DCASE2025Task1Adapter",
215
+ "apply_channel_strategy",
216
+ "apply_audio_corruption",
217
+ "as_numpy_iterator",
218
+ "ast_kaldi_fbank",
219
+ "FeatureNormConfig",
220
+ "FrontendConfig",
221
+ "LabelTransformConfig",
222
+ "LogCompressionConfig",
223
+ "MelConfig",
224
+ "STFTConfig",
225
+ "SegmentStrategyConfig",
226
+ "adapt_acoustic_sample",
227
+ "adapters",
228
+ "augment",
229
+ "apply_feature_normalization",
230
+ "batching",
231
+ "channel",
232
+ "compat",
233
+ "compress_log",
234
+ "compute_num_frames",
235
+ "configs",
236
+ "corruptions",
237
+ "compute_feature_stats",
238
+ "create_audio_corruption_datasets",
239
+ "confusion_matrix",
240
+ "convert_audio_layout",
241
+ "datasets",
242
+ "decode_audio_file",
243
+ "decode_wav_bytes",
244
+ "decode_wav_file",
245
+ "decoding",
246
+ "dcase2025",
247
+ "default_output_key",
248
+ "evals",
249
+ "eval_views",
250
+ "ensure_audio_rank",
251
+ "expected_audio_static_shape",
252
+ "frontend_output_kind",
253
+ "filters",
254
+ "frontends",
255
+ "generate_eval_views",
256
+ "filter_by_metadata",
257
+ "get_audio_batch_augment",
258
+ "get_audio_batch_augment_metadata",
259
+ "get_audio_channel_strategy",
260
+ "get_audio_corruption",
261
+ "get_audio_decoder",
262
+ "get_audio_eval_view_strategy",
263
+ "get_audio_frontend",
264
+ "get_audio_normalization",
265
+ "get_audio_postprocessor",
266
+ "get_audio_resampler",
267
+ "get_audio_segment_strategy",
268
+ "get_audio_spectrogram_augment",
269
+ "get_audio_spectrogram_augment_metadata",
270
+ "get_audio_waveform_augment",
271
+ "get_audio_waveform_augment_metadata",
272
+ "get_dataset_presets",
273
+ "get_resolved_preset",
274
+ "groupby_metadata",
275
+ "has_audio_batch_augment",
276
+ "has_audio_channel_strategy",
277
+ "has_audio_corruption",
278
+ "has_audio_decoder",
279
+ "has_audio_eval_view_strategy",
280
+ "has_audio_frontend",
281
+ "has_audio_normalization",
282
+ "has_audio_postprocessor",
283
+ "has_audio_resampler",
284
+ "has_audio_segment_strategy",
285
+ "has_audio_spectrogram_augment",
286
+ "has_audio_waveform_augment",
287
+ "infer_duration",
288
+ "jax",
289
+ "kaldi_fbank",
290
+ "labels",
291
+ "layouts",
292
+ "list_audio_batch_augments",
293
+ "list_audio_batch_augment_metadata",
294
+ "list_audio_channel_strategies",
295
+ "list_audio_corruptions",
296
+ "list_audio_decoders",
297
+ "list_audio_eval_view_strategies",
298
+ "list_audio_frontends",
299
+ "list_audio_normalizations",
300
+ "list_audio_postprocessors",
301
+ "list_audio_resamplers",
302
+ "list_audio_segment_strategies",
303
+ "list_audio_spectrogram_augments",
304
+ "list_audio_spectrogram_augment_metadata",
305
+ "list_audio_waveform_augments",
306
+ "list_audio_waveform_augment_metadata",
307
+ "logmel",
308
+ "macro_classwise_accuracy",
309
+ "merge_with_presets",
310
+ "make_eval_views",
311
+ "make_audio_metadata",
312
+ "make_model_input_stage",
313
+ "make_preprocessing",
314
+ "make_preprocessing_stage",
315
+ "make_segment_stage",
316
+ "make_source_dataset",
317
+ "make_stats_iterator",
318
+ "make_target_dataset",
319
+ "metadata",
320
+ "MetadataEncoder",
321
+ "MetadataSidecar",
322
+ "mono_left",
323
+ "mono_mean",
324
+ "mono_right",
325
+ "mel_power",
326
+ "mel_spectrogram",
327
+ "mel_weight_matrix",
328
+ "mfcc",
329
+ "keep",
330
+ "normalization",
331
+ "normalize_waveform",
332
+ "pad_nested",
333
+ "pad_waveform",
334
+ "pcen_mel",
335
+ "pipelines",
336
+ "per_device_accuracy",
337
+ "per_scene_accuracy",
338
+ "postprocessing",
339
+ "preprocessing",
340
+ "presets",
341
+ "register_audio_batch_augment",
342
+ "register_audio_channel_strategy",
343
+ "register_audio_corruption",
344
+ "register_audio_decoder",
345
+ "register_audio_eval_view_strategy",
346
+ "register_audio_frontend",
347
+ "register_audio_normalization",
348
+ "register_audio_postprocessor",
349
+ "register_audio_resampler",
350
+ "register_audio_segment_strategy",
351
+ "register_audio_spectrogram_augment",
352
+ "register_audio_waveform_augment",
353
+ "register_preset",
354
+ "registry",
355
+ "raw_waveform",
356
+ "resample_sample",
357
+ "resample_waveform",
358
+ "resampling",
359
+ "schema",
360
+ "segment",
361
+ "segment_waveform",
362
+ "set_static_audio_shape",
363
+ "sources",
364
+ "stages",
365
+ "stft_magnitude",
366
+ "stft_power_spectrogram",
367
+ "SplitLeakageError",
368
+ "stats",
369
+ "stable_int64_hash",
370
+ "standardize_waveform_layout",
371
+ "remove_dc_offset",
372
+ "tasks",
373
+ "to_numpy",
374
+ "to_float32_waveform",
375
+ "transform_label",
376
+ "parse_city_from_row",
377
+ "parse_device_from_row",
378
+ "parse_scene_from_row",
379
+ "parse_source_recording_id",
380
+ "assert_stats_allowed",
381
+ ]
@@ -0,0 +1,329 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Literal, Protocol
4
+
5
+ import tensorflow as tf
6
+
7
+ from justdata.acoustic.decoding import decode_audio_file
8
+ from justdata.acoustic.schema import (
9
+ AUDIO,
10
+ CLIP_ID,
11
+ DATASET,
12
+ DURATION,
13
+ END_TIME,
14
+ EXAMPLE_ID,
15
+ FILENAME,
16
+ LABEL,
17
+ METADATA,
18
+ ORIGINAL_SAMPLE_RATE,
19
+ PATH,
20
+ SAMPLE_RATE,
21
+ SOURCE_ID,
22
+ SPLIT,
23
+ START_TIME,
24
+ WAVEFORM,
25
+ )
26
+ from justdata.core.adapters import register_adapter
27
+
28
+
29
+ class AcousticAdapter(Protocol):
30
+ def __call__(self, sample: dict) -> dict: ...
31
+
32
+
33
+ def ensure_audio_rank(audio: tf.Tensor) -> tf.Tensor:
34
+ audio = tf.convert_to_tensor(audio)
35
+ rank = audio.shape.rank
36
+ if rank is not None and rank not in (1, 2):
37
+ raise ValueError(f"Audio tensor must have rank 1 or 2; got rank {rank}.")
38
+ return audio
39
+
40
+
41
+ def to_float32_waveform(
42
+ audio: tf.Tensor,
43
+ input_dtype: tf.DType | None = None,
44
+ *,
45
+ clip_value: float | None = None,
46
+ ) -> tf.Tensor:
47
+ dtype = tf.as_dtype(input_dtype or audio.dtype)
48
+ audio = tf.cast(audio, tf.float32)
49
+
50
+ if dtype == tf.int16:
51
+ audio = audio / 32768.0
52
+ elif dtype == tf.int32:
53
+ audio = audio / 2147483648.0
54
+ elif dtype == tf.uint8:
55
+ audio = (audio - 128.0) / 128.0
56
+
57
+ if clip_value is not None:
58
+ audio = tf.clip_by_value(audio, -float(clip_value), float(clip_value))
59
+
60
+ return audio
61
+
62
+
63
+ def standardize_waveform_layout(
64
+ audio: tf.Tensor,
65
+ layout_hint: Literal["tc", "ct", "t", "auto"] = "auto",
66
+ ) -> tf.Tensor:
67
+ audio = ensure_audio_rank(audio)
68
+
69
+ def _rank2(value: tf.Tensor) -> tf.Tensor:
70
+ if layout_hint == "ct":
71
+ return tf.transpose(value, [1, 0])
72
+ if layout_hint in {"tc", "t"}:
73
+ return value
74
+ if layout_hint != "auto":
75
+ raise ValueError(f"Unknown audio layout_hint: {layout_hint!r}")
76
+
77
+ shape = value.shape
78
+ if shape.rank == 2 and shape[0] is not None and shape[-1] is not None:
79
+ if shape[0] <= 8 and shape[-1] > 8:
80
+ return tf.transpose(value, [1, 0])
81
+ return value
82
+
83
+ is_channels_first = tf.logical_and(
84
+ tf.shape(value)[0] <= 8,
85
+ tf.shape(value)[-1] > 8,
86
+ )
87
+ return tf.cond(
88
+ is_channels_first, lambda: tf.transpose(value, [1, 0]), lambda: value
89
+ )
90
+
91
+ rank = audio.shape.rank
92
+ if rank == 1:
93
+ return audio[:, tf.newaxis]
94
+ if rank == 2:
95
+ return _rank2(audio)
96
+
97
+ return tf.cond(
98
+ tf.equal(tf.rank(audio), 1),
99
+ lambda: audio[:, tf.newaxis],
100
+ lambda: _rank2(audio),
101
+ )
102
+
103
+
104
+ def infer_duration(num_samples: tf.Tensor, sample_rate: tf.Tensor) -> tf.Tensor:
105
+ return tf.math.divide_no_nan(
106
+ tf.cast(num_samples, tf.float32),
107
+ tf.cast(sample_rate, tf.float32),
108
+ )
109
+
110
+
111
+ def make_audio_metadata(
112
+ *,
113
+ dataset: Any | None = None,
114
+ split: Any | None = None,
115
+ example_id: Any | None = None,
116
+ filename: Any | None = None,
117
+ path: Any | None = None,
118
+ clip_id: Any | None = None,
119
+ source_id: Any | None = None,
120
+ start_time: Any | None = None,
121
+ end_time: Any | None = None,
122
+ original_sample_rate: Any | None = None,
123
+ duration: Any | None = None,
124
+ **extra: Any,
125
+ ) -> dict:
126
+ fields = {
127
+ DATASET: dataset,
128
+ SPLIT: split,
129
+ EXAMPLE_ID: example_id,
130
+ FILENAME: filename,
131
+ PATH: path,
132
+ CLIP_ID: clip_id,
133
+ SOURCE_ID: source_id,
134
+ START_TIME: start_time,
135
+ END_TIME: end_time,
136
+ ORIGINAL_SAMPLE_RATE: original_sample_rate,
137
+ DURATION: duration,
138
+ **extra,
139
+ }
140
+ return {key: value for key, value in fields.items() if value is not None}
141
+
142
+
143
+ def _get(sample: dict, *keys: str) -> Any | None:
144
+ for key in keys:
145
+ if key in sample:
146
+ return sample[key]
147
+ return None
148
+
149
+
150
+ def _basename(path: Any | None) -> Any | None:
151
+ if path is None:
152
+ return None
153
+ path = tf.convert_to_tensor(path, dtype=tf.string)
154
+ return tf.strings.regex_replace(path, r"^.*[\\/]", "")
155
+
156
+
157
+ def _audio_payload(sample: dict, audio_key: str) -> tuple[Any | None, Any | None]:
158
+ if WAVEFORM in sample:
159
+ return sample[WAVEFORM], None
160
+
161
+ audio = sample.get(audio_key)
162
+ if isinstance(audio, dict):
163
+ sample_rate = _get(audio, SAMPLE_RATE, "sampling_rate")
164
+ return _get(audio, "array", WAVEFORM), sample_rate
165
+
166
+ return audio, None
167
+
168
+
169
+ def _decode_or_get_waveform(
170
+ sample: dict,
171
+ *,
172
+ audio_key: str,
173
+ path_key: str,
174
+ ) -> tuple[tf.Tensor, tf.Tensor | None]:
175
+ waveform, sample_rate = _audio_payload(sample, audio_key)
176
+ if waveform is not None:
177
+ return tf.convert_to_tensor(waveform), sample_rate
178
+
179
+ path = sample.get(path_key)
180
+ if path is None:
181
+ raise ValueError(
182
+ f"Acoustic sample must contain '{WAVEFORM}', '{audio_key}', or '{path_key}'."
183
+ )
184
+
185
+ waveform, decoded_sample_rate = decode_audio_file(path)
186
+ return waveform, decoded_sample_rate
187
+
188
+
189
+ def _slice_window(
190
+ sample: dict, waveform: tf.Tensor, sample_rate: tf.Tensor
191
+ ) -> tf.Tensor:
192
+ if START_TIME not in sample or END_TIME not in sample:
193
+ return waveform
194
+
195
+ start_time = tf.cast(sample[START_TIME], tf.float32)
196
+ end_time = tf.cast(sample[END_TIME], tf.float32)
197
+
198
+ def _slice() -> tf.Tensor:
199
+ start = tf.cast(
200
+ tf.round(start_time * tf.cast(sample_rate, tf.float32)), tf.int32
201
+ )
202
+ end = tf.cast(tf.round(end_time * tf.cast(sample_rate, tf.float32)), tf.int32)
203
+ start = tf.clip_by_value(start, 0, tf.shape(waveform)[0])
204
+ end = tf.clip_by_value(end, start, tf.shape(waveform)[0])
205
+ return waveform[start:end]
206
+
207
+ return tf.cond(tf.greater(end_time, start_time), _slice, lambda: waveform)
208
+
209
+
210
+ def adapt_acoustic_sample(
211
+ sample: dict,
212
+ *,
213
+ dataset: str | None = None,
214
+ audio_key: str = AUDIO,
215
+ label_key: str | None = LABEL,
216
+ sample_rate_key: str = SAMPLE_RATE,
217
+ path_key: str = PATH,
218
+ layout_hint: Literal["tc", "ct", "t", "auto"] = "auto",
219
+ clip_value: float | None = None,
220
+ ) -> dict:
221
+ waveform, decoded_sample_rate = _decode_or_get_waveform(
222
+ sample,
223
+ audio_key=audio_key,
224
+ path_key=path_key,
225
+ )
226
+ input_dtype = waveform.dtype
227
+
228
+ sample_rate = _get(sample, sample_rate_key, "sampling_rate")
229
+ if sample_rate is None:
230
+ sample_rate = decoded_sample_rate
231
+ if sample_rate is None:
232
+ raise ValueError("Acoustic sample is missing a sample rate.")
233
+
234
+ sample_rate = tf.cast(sample_rate, tf.int32)
235
+ original_sample_rate = _get(sample, ORIGINAL_SAMPLE_RATE)
236
+ if original_sample_rate is None:
237
+ original_sample_rate = sample_rate
238
+ original_sample_rate = tf.cast(original_sample_rate, tf.int32)
239
+
240
+ waveform = to_float32_waveform(
241
+ waveform, input_dtype=input_dtype, clip_value=clip_value
242
+ )
243
+ waveform = standardize_waveform_layout(waveform, layout_hint=layout_hint)
244
+ waveform = _slice_window(sample, waveform, sample_rate)
245
+
246
+ duration = infer_duration(tf.shape(waveform)[0], sample_rate)
247
+
248
+ result = {
249
+ WAVEFORM: waveform,
250
+ SAMPLE_RATE: sample_rate,
251
+ DURATION: duration,
252
+ }
253
+ if label_key is not None and label_key in sample:
254
+ result[LABEL] = sample[label_key]
255
+
256
+ metadata = {}
257
+ if isinstance(sample.get(METADATA), dict):
258
+ metadata.update(sample[METADATA])
259
+
260
+ path = sample.get(path_key)
261
+ clip_id = _get(sample, CLIP_ID)
262
+ example_id = _get(sample, EXAMPLE_ID)
263
+ if example_id is None:
264
+ example_id = clip_id
265
+ filename = _get(sample, FILENAME)
266
+ if filename is None:
267
+ filename = _basename(path)
268
+ metadata.update(
269
+ make_audio_metadata(
270
+ dataset=_get(sample, DATASET) if DATASET in sample else dataset,
271
+ split=_get(sample, SPLIT),
272
+ example_id=example_id,
273
+ filename=filename,
274
+ path=path,
275
+ clip_id=clip_id,
276
+ source_id=_get(sample, SOURCE_ID),
277
+ start_time=_get(sample, START_TIME),
278
+ end_time=_get(sample, END_TIME),
279
+ original_sample_rate=original_sample_rate,
280
+ duration=duration,
281
+ )
282
+ )
283
+ result[METADATA] = metadata
284
+ return result
285
+
286
+
287
+ @register_adapter("hf_audio:")
288
+ @register_adapter("local_audio:")
289
+ def acoustic_source_adapter(sample: dict) -> dict:
290
+ return adapt_acoustic_sample(sample)
291
+
292
+
293
+ @register_adapter("esc50")
294
+ def esc50_adapter(sample: dict) -> dict:
295
+ return adapt_acoustic_sample(sample, dataset="esc50")
296
+
297
+
298
+ @register_adapter("speech_commands")
299
+ def speech_commands_adapter(sample: dict) -> dict:
300
+ sample = dict(sample)
301
+ if SAMPLE_RATE not in sample and "sampling_rate" not in sample:
302
+ sample[SAMPLE_RATE] = 16000
303
+ return adapt_acoustic_sample(sample, dataset="speech_commands")
304
+
305
+
306
+ @register_adapter("audioset")
307
+ def audioset_adapter(sample: dict) -> dict:
308
+ return adapt_acoustic_sample(sample, dataset="audioset")
309
+
310
+
311
+ def dcase2025_task1_adapter(sample: dict) -> dict:
312
+ from justdata.acoustic.dcase2025 import dcase2025_task1_adapter as adapter
313
+
314
+ return adapter(sample)
315
+
316
+
317
+ __all__ = [
318
+ "AcousticAdapter",
319
+ "adapt_acoustic_sample",
320
+ "audioset_adapter",
321
+ "dcase2025_task1_adapter",
322
+ "ensure_audio_rank",
323
+ "esc50_adapter",
324
+ "infer_duration",
325
+ "make_audio_metadata",
326
+ "speech_commands_adapter",
327
+ "standardize_waveform_layout",
328
+ "to_float32_waveform",
329
+ ]