Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
MPAI-Private
MPAI-CAE
arp
ARP Library
Commits
d74b8122
Commit
d74b8122
authored
Apr 24, 2023
by
Matteo
Browse files
update
parent
51d4ec30
Pipeline
#17
canceled with stages
in 0 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
mpai_cae_arp/types/irregularity.py
View file @
d74b8122
# pylint: disable=too-few-public-methods
from typing import TypeVar
import uuid
from enum import Enum
...
...
@@ -45,6 +46,9 @@ class IrregularityProperties(BaseModel):
writing_equalisation: EqualizationStandard = Field(
alias="WritingEqualisationStandard")
class Config:
allow_population_by_field_name = True
@staticmethod
def from_json(json_property: dict) -> SelfIrregularityProperties:
return IrregularityProperties(
...
...
@@ -78,17 +82,18 @@ class IrregularityProperties(BaseModel):
class Irregularity(BaseModel):
irregularity_ID
:
uuid
.
UUID
=
Field
(
default_factory
=
uuid
.
uuid4
,
alias
=
"IrregularityID"
)
id: uuid.UUID = Field(default_factory=uuid.uuid4, alias="IrregularityID")
source: Source = Field(alias="Source")
time_label: str = Field(alias="TimeLabel")
irregularity_type
:
IrregularityType
|
None
=
Field
(
default
=
None
,
alias
=
"IrregularityType"
)
irregularity_properties
:
IrregularityProperties
|
None
=
Field
(
default
=
None
,
alias
=
"IrregularityProperties"
)
type: IrregularityType | None = Field(default=None, alias="IrregularityType")
properties: IrregularityProperties | None = Field(default=None,
alias="IrregularityProperties")
image_URI: str | None = Field(default=None, alias="ImageURI")
audio_block_URI: str | None = Field(default=None, alias="AudioBlockURI")
class Config:
allow_population_by_field_name = True
@staticmethod
def from_json(json_irreg: dict) -> SelfIrregularity:
...
...
@@ -109,11 +114,11 @@ class Irregularity(BaseModel):
else:
irregularity_type = IrregularityType.SPEED
return
Irregularity
(
i
rregularity_ID
=
uuid
.
UUID
(
json_irreg
[
"IrregularityID"
]),
return Irregularity(i
d
=uuid.UUID(json_irreg["IrregularityID"]),
source=Source(json_irreg["Source"]),
time_label=json_irreg["TimeLabel"],
irregularity_
type
=
irregularity_type
,
irregularity_
properties
=
properties
,
type=irregularity_type,
properties=properties,
image_URI=json_irreg.get("ImageURI"),
audio_block_URI=json_irreg.get("AudioBlockURI"))
...
...
@@ -125,13 +130,13 @@ class Irregularity(BaseModel):
Use :func:`Irregularity.json` instead.
"""
dictionary = {
"IrregularityID"
:
str
(
self
.
i
rregularity_ID
),
"IrregularityID": str(self.i
d
),
"Source": self.source.value,
"TimeLabel": self.time_label,
}
if
self
.
irregularity_
type
:
dictionary
[
"IrregularityType"
]
=
self
.
irregularity_
type
.
value
if self.type:
dictionary["IrregularityType"] = self.type.value
if self.image_URI:
dictionary["ImageURI"] = self.image_URI
...
...
@@ -139,16 +144,15 @@ class Irregularity(BaseModel):
if self.audio_block_URI:
dictionary["AudioBlockURI"] = self.audio_block_URI
if
self
.
irregularity_properties
:
dictionary
[
"IrregularityProperties"
]
=
self
.
irregularity_properties
.
to_json
(
)
if self.properties:
dictionary["IrregularityProperties"] = self.properties.to_json()
return dictionary
class IrregularityFile(BaseModel):
irregularities
:
list
[
Irregularity
]
offset
:
int
|
None
=
None
irregularities: list[Irregularity]
= Field(alias="Irregularities")
offset: int | None =
Field(default=None, alias="Offset")
def __eq__(self, __o: object) -> bool:
if not isinstance(__o, IrregularityFile):
...
...
@@ -221,8 +225,9 @@ class IrregularityFile(BaseModel):
def save_as_json_file(self, path: str) -> None:
"""
Save the
editing list
as a JSON file at the given path.
Save the
irregularity file
as a JSON file at the given path.
.. versionadded:: 0.4.0
"""
File
(
path
=
path
,
file_type
=
FileType
.
JSON
).
write_content
(
self
.
json
())
File(path=path,
file_type=FileType.JSON).write_content(self.json(by_alias=True, indent=4))
mpai_cae_arp/types/restoration.py
View file @
d74b8122
# pylint: disable=too-few-public-methods
import
uuid
from
typing
import
TypeVar
from
pydantic
import
BaseModel
,
Field
...
...
@@ -6,14 +7,22 @@ from mpai_cae_arp.files import File, FileType
class
Restoration
(
BaseModel
):
id
:
uuid
.
UUID
=
Field
(
default_factory
=
uuid
.
uuid4
)
preservation_audio_file_start
:
str
preservation_audio_file_end
:
str
restored_audio_file_URI
:
str
reading_backwards
:
bool
applied_speed_standard
:
SpeedStandard
applied_sample_frequency
:
int
original_equalization_standard
:
EqualizationStandard
"""
.. versionadded:: 0.4.0
"""
class
Config
:
allow_population_by_field_name
=
True
id
:
uuid
.
UUID
=
Field
(
default_factory
=
uuid
.
uuid4
,
alias
=
"RestorationID"
)
preservation_audio_file_start
:
str
=
Field
(
alias
=
"PreservationAudioFileStart"
)
preservation_audio_file_end
:
str
=
Field
(
alias
=
"PreservationAudioFileEnd"
)
restored_audio_file_URI
:
str
=
Field
(
alias
=
"RestoredAudioFileURI"
)
reading_backwards
:
bool
=
Field
(
alias
=
"ReadingBackwards"
)
applied_speed_standard
:
SpeedStandard
=
Field
(
alias
=
"AppliedSpeedStandard"
)
applied_sample_frequency
:
int
=
Field
(
alias
=
"AppliedSampleFrequency"
)
original_equalization_standard
:
EqualizationStandard
=
Field
(
alias
=
"OriginalEqualizationStandard"
)
Self
=
TypeVar
(
"Self"
,
bound
=
"EditingList"
)
...
...
@@ -23,10 +32,15 @@ class EditingList(BaseModel):
"""
.. versionadded:: 0.4.0
"""
original_speed_standard
:
SpeedStandard
original_equalization_standard
:
EqualizationStandard
original_sample_frequency
:
int
restorations
:
list
[
Restoration
]
class
Config
:
allow_population_by_field_name
=
True
original_speed_standard
:
SpeedStandard
=
Field
(
alias
=
"OriginalSpeedStandard"
)
original_equalization_standard
:
EqualizationStandard
=
Field
(
alias
=
"OriginalEqualizationStandard"
)
original_sample_frequency
:
int
=
Field
(
alias
=
"OriginalSampleFrequency"
)
restorations
:
list
[
Restoration
]
=
Field
(
alias
=
"Restorations"
)
def
add
(
self
,
restoration
:
Restoration
)
->
Self
:
self
.
restorations
.
append
(
restoration
)
...
...
@@ -38,28 +52,13 @@ class EditingList(BaseModel):
def
remove_by_id
(
self
,
restoration_id
:
uuid
.
UUID
)
->
Self
:
filtered
=
list
(
filter
(
lambda
r
:
r
.
id
!=
restoration_id
,
self
.
restorations
))
if
len
(
filtered
)
==
len
(
self
.
restorations
):
raise
ValueError
(
f
"Restoration with ID
{
restoration_id
}
not found."
)
self
.
restorations
=
filtered
return
self
def
save_as_json_file
(
self
,
path
:
str
)
->
None
:
File
(
path
=
path
,
file_type
=
FileType
.
JSON
).
write_content
(
self
.
json
())
if
__name__
==
"__main__"
:
rest
=
Restoration
(
preservation_audio_file_start
=
"00:00:00.000"
,
preservation_audio_file_end
=
"00:00:10.000"
,
restored_audio_file_URI
=
"https://www.google.com"
,
reading_backwards
=
False
,
applied_sample_frequency
=
44100
,
applied_speed_standard
=
SpeedStandard
.
III
,
original_equalization_standard
=
EqualizationStandard
.
CCIR
)
editing_list
=
EditingList
(
original_equalization_standard
=
EqualizationStandard
.
CCIR
,
original_speed_standard
=
SpeedStandard
.
III
,
original_sample_frequency
=
44100
,
restorations
=
[])
editing_list
.
add
(
rest
)
print
(
rest
)
print
(
editing_list
.
json
())
File
(
path
=
path
,
filetype
=
FileType
.
JSON
).
write_content
(
self
.
json
(
by_alias
=
True
,
indent
=
4
))
tests/__init__.py
deleted
100644 → 0
View file @
51d4ec30
tests/test_files.py
View file @
d74b8122
import
os
import
json
import
tempfile
import
pytest
from
mpai_cae_arp
import
files
def
test_test
():
assert
0
==
0
def
test_open_file
():
with
tempfile
.
NamedTemporaryFile
()
as
tmp_file
:
tmp_file
.
write
(
b
'{"test": "test"}'
)
tmp_file
.
seek
(
0
)
my_file
=
files
.
File
(
path
=
tmp_file
.
name
,
filetype
=
files
.
FileType
.
JSON
).
open
(
files
.
FileAction
.
READ
)
assert
my_file
.
read
()
==
'{"test": "test"}'
def
test_failing_read
():
with
pytest
.
raises
(
FileNotFoundError
):
files
.
File
(
"test"
,
files
.
FileType
.
JSON
).
open
(
files
.
FileAction
.
READ
)
def
test_get_content
():
with
tempfile
.
NamedTemporaryFile
()
as
tmp_file
:
tmp_file
.
write
(
b
'{"test": "test"}'
)
tmp_file
.
seek
(
0
)
my_file_content
=
files
.
File
(
path
=
tmp_file
.
name
,
filetype
=
files
.
FileType
.
JSON
).
get_content
()
assert
my_file_content
==
{
"test"
:
"test"
}
tests/types/test_restoration.py
0 → 100644
View file @
d74b8122
import
tempfile
import
pytest
import
json
from
mpai_cae_arp.audio.standards
import
EqualizationStandard
,
SpeedStandard
from
mpai_cae_arp.types.restoration
import
Restoration
,
EditingList
class
TestRestoration
:
correct_data
=
{
"RestorationID"
:
"00000000-0000-0000-0000-000000000000"
,
"PreservationAudioFileStart"
:
"00:00:00.000"
,
"PreservationAudioFileEnd"
:
"00:00:10.000"
,
"RestoredAudioFileURI"
:
"https://www.google.com"
,
"ReadingBackwards"
:
False
,
"AppliedSpeedStandard"
:
15
,
"AppliedSampleFrequency"
:
44100
,
"OriginalEqualizationStandard"
:
"IEC1"
}
my_rest
=
Restoration
(
RestorationID
=
"00000000-0000-0000-0000-000000000000"
,
PreservationAudioFileEnd
=
"00:00:10.000"
,
PreservationAudioFileStart
=
"00:00:00.000"
,
ReadingBackwards
=
False
,
AppliedSampleFrequency
=
44100
,
AppliedSpeedStandard
=
SpeedStandard
.
V
,
OriginalEqualizationStandard
=
EqualizationStandard
.
CCIR
,
RestoredAudioFileURI
=
"https://www.google.com"
)
def
test_init_from_dict
(
self
):
assert
Restoration
(
**
self
.
correct_data
)
==
self
.
my_rest
def
test_init_from_object
(
self
):
assert
Restoration
(
**
self
.
my_rest
.
dict
())
==
self
.
my_rest
def
test_init_from_json
(
self
):
assert
Restoration
.
parse_raw
(
self
.
my_rest
.
json
())
==
self
.
my_rest
def
test_json_serialize
(
self
):
assert
'"id": "00000000-0000-0000-0000-000000000000"'
in
self
.
my_rest
.
json
()
assert
'"RestorationID": "00000000-0000-0000-0000-000000000000"'
in
self
.
my_rest
.
json
(
by_alias
=
True
)
class
TestEditingList
:
data
=
{
"OriginalSpeedStandard"
:
15
,
"OriginalEqualizationStandard"
:
"IEC1"
,
"OriginalSampleFrequency"
:
44100
,
"Restorations"
:
[{
"RestorationID"
:
"00000000-0000-0000-0000-000000000000"
,
"PreservationAudioFileStart"
:
"00:00:00.000"
,
"PreservationAudioFileEnd"
:
"00:00:10.000"
,
"RestoredAudioFileURI"
:
"https://www.google.com"
,
"ReadingBackwards"
:
False
,
"AppliedSpeedStandard"
:
15
,
"AppliedSampleFrequency"
:
44100
,
"OriginalEqualizationStandard"
:
"IEC1"
}]
}
rest
=
Restoration
(
PreservationAudioFileStart
=
"00:00:00.000"
,
PreservationAudioFileEnd
=
"00:00:10.000"
,
RestoredAudioFileURI
=
"https://www.google.com"
,
ReadingBackwards
=
False
,
AppliedSampleFrequency
=
44100
,
AppliedSpeedStandard
=
SpeedStandard
.
V
,
OriginalEqualizationStandard
=
EqualizationStandard
.
CCIR
)
my_editing_list
:
EditingList
=
EditingList
(
OriginalEqualizationStandard
=
EqualizationStandard
.
CCIR
,
OriginalSampleFrequency
=
44100
,
OriginalSpeedStandard
=
SpeedStandard
.
V
,
Restorations
=
[])
\
.
add
(
Restoration
(
RestorationID
=
"00000000-0000-0000-0000-000000000000"
,
PreservationAudioFileStart
=
"00:00:00.000"
,
PreservationAudioFileEnd
=
"00:00:10.000"
,
RestoredAudioFileURI
=
"https://www.google.com"
,
ReadingBackwards
=
False
,
AppliedSpeedStandard
=
SpeedStandard
.
V
,
AppliedSampleFrequency
=
44100
,
OriginalEqualizationStandard
=
EqualizationStandard
.
CCIR
)
)
def
test_init_from_dict
(
self
):
assert
EditingList
(
**
self
.
data
)
==
self
.
my_editing_list
def
test_init_from_object
(
self
):
assert
EditingList
(
**
self
.
my_editing_list
.
dict
())
==
self
.
my_editing_list
def
test_init_from_json
(
self
):
assert
EditingList
.
parse_raw
(
self
.
my_editing_list
.
json
())
==
self
.
my_editing_list
def
test_add
(
self
):
tmp
:
EditingList
=
self
.
my_editing_list
.
copy
(
deep
=
True
)
tmp
.
add
(
self
.
rest
)
assert
len
(
tmp
.
restorations
)
==
2
def
test_remove
(
self
):
tmp
:
EditingList
=
self
.
my_editing_list
\
.
copy
(
deep
=
True
)
\
.
remove
(
self
.
my_editing_list
.
restorations
[
0
])
assert
len
(
tmp
.
restorations
)
==
0
with
pytest
.
raises
(
ValueError
):
tmp
.
remove
(
self
.
rest
)
def
test_remove_by_id
(
self
):
tmp
:
EditingList
=
self
.
my_editing_list
\
.
copy
(
deep
=
True
)
\
.
remove_by_id
(
self
.
my_editing_list
.
restorations
[
0
].
id
)
assert
len
(
tmp
.
restorations
)
==
0
with
pytest
.
raises
(
ValueError
):
tmp
.
remove_by_id
(
self
.
rest
.
id
)
def
test_save_as_json_file
(
self
):
tmp
=
tempfile
.
NamedTemporaryFile
(
delete
=
False
)
tmp
.
close
()
self
.
my_editing_list
.
save_as_json_file
(
tmp
.
name
)
with
open
(
tmp
.
name
,
'r'
)
as
f
:
tmp_content
=
json
.
load
(
f
)
assert
self
.
my_editing_list
.
json
(
by_alias
=
True
,
indent
=
4
)
==
tmp_content
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment