Skip to content

Commit

Permalink
Merge pull request #47 from ArcanaFramework/develop
Browse files Browse the repository at this point in the history
changed generate_sample_data signature to take random.Random instances as "seeds"
  • Loading branch information
tclose authored Dec 6, 2023
2 parents b0cecc5 + e41f834 commit d02e8c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
6 changes: 5 additions & 1 deletion extras/fileformats/extras/application/medical.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import typing as ty
from pathlib import Path
from random import Random
import pydicom
from fileformats.core import FileSet
from fileformats.application import Dicom
Expand All @@ -22,7 +23,10 @@ def dicom_read_metadata(

@FileSet.generate_sample_data.register
def dicom_generate_sample_data(
dicom: Dicom, dest_dir: Path, seed: int, stem: ty.Optional[str]
dicom: Dicom,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
return next(
medimages4tests.dummy.dicom.mri.t1w.siemens.skyra.syngo_d13c.get_image().iterdir()
Expand Down
10 changes: 8 additions & 2 deletions fileformats/application/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ class Toml(DataSerialization):

@FileSet.generate_sample_data.register
def generate_json_sample_data(
js: Json, dest_dir: Path, seed: int, stem: ty.Optional[str]
js: Json,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
js_file = dest_dir / gen_filename(seed, file_type=js, stem=stem)
rng = Random(seed + 1)
Expand All @@ -109,7 +112,10 @@ def generate_json_sample_data(

@FileSet.generate_sample_data.register
def generate_yaml_sample_data(
yml: Yaml, dest_dir: Path, seed: int, stem: ty.Optional[str]
yml: Yaml,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
yml_file = dest_dir / gen_filename(seed, file_type=yml, stem=stem)
rng = Random(seed + 1)
Expand Down
6 changes: 5 additions & 1 deletion fileformats/core/fileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from enum import Enum, IntEnum
from warnings import warn
import tempfile
from random import Random
from collections import Counter
import typing as ty
import shutil
Expand Down Expand Up @@ -871,7 +872,10 @@ def sample(

@hook.extra
def generate_sample_data(
self, dest_dir: Path, seed: int = 0, stem: ty.Optional[str] = None
self,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
"""Generate test data at the fspaths of the file-set
Expand Down
25 changes: 20 additions & 5 deletions fileformats/generic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ class SetOf(WithClassifiers, TypedSet):

@FileSet.generate_sample_data.register
def fsobject_generate_sample_data(
fsobject: FsObject, dest_dir: Path, seed: int, stem: ty.Optional[str]
fsobject: FsObject,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
a_file = dest_dir / gen_filename(seed, file_type=fsobject, stem=stem)
a_file.write_text("".join(random.choices(string.printable, k=FILE_LENGTH)))
Expand All @@ -287,7 +290,10 @@ def fsobject_generate_sample_data(

@FileSet.generate_sample_data.register
def file_generate_sample_data(
file: File, dest_dir: Path, seed: int, stem: ty.Optional[str]
file: File,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
fname = gen_filename(seed, file_type=file, stem=stem)
stem = fname[: -len(file.strext)]
Expand Down Expand Up @@ -321,7 +327,10 @@ def file_generate_sample_data(

@FileSet.generate_sample_data.register
def directory_generate_sample_data(
directory: Directory, dest_dir: Path, seed: int, stem: ty.Optional[str]
directory: Directory,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
rng = Random(str(seed) + directory.mime_like)
a_dir = dest_dir / gen_filename(rng, stem=stem)
Expand All @@ -332,7 +341,10 @@ def directory_generate_sample_data(

@FileSet.generate_sample_data.register
def directory_containing_generate_sample_data(
directory: DirectoryContaining, dest_dir: Path, seed: int, stem: ty.Optional[str]
directory: DirectoryContaining,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
rng = Random(str(seed) + directory.mime_like)
a_dir = dest_dir / gen_filename(rng, stem=stem)
Expand All @@ -344,7 +356,10 @@ def directory_containing_generate_sample_data(

@FileSet.generate_sample_data.register
def set_of_sample_data(
set_of: SetOf, dest_dir: Path, seed: int, stem: ty.Optional[str]
set_of: SetOf,
dest_dir: Path,
seed: ty.Union[int, Random] = 0,
stem: ty.Optional[str] = None,
) -> ty.Iterable[Path]:
rng = Random(str(seed) + set_of.mime_like)
return list(
Expand Down

0 comments on commit d02e8c6

Please sign in to comment.