Commit bcb8607c authored by Matteo's avatar Matteo
Browse files

update

parent bd641223
...@@ -5,7 +5,7 @@ import numpy as np ...@@ -5,7 +5,7 @@ import numpy as np
import librosa import librosa
from mpai_cae_arp.audio._noise import Noise from mpai_cae_arp.audio._noise import Noise
import mpai_cae_arp.audio.utils as utils from mpai_cae_arp.audio import utils
class AudioWave: class AudioWave:
...@@ -486,4 +486,4 @@ class AudioWave: ...@@ -486,4 +486,4 @@ class AudioWave:
idxs[min(noise_list).label].append((i, i + window_frames)) idxs[min(noise_list).label].append((i, i + window_frames))
i += window_frames i += window_frames
return idxs return idxs
\ No newline at end of file
from io import TextIOWrapper
from enum import Enum
import json import json
import yaml import yaml
from pydantic import BaseModel
def get_file_content(file_name: str, format: str) -> dict: class FileAction(Enum):
""" READ = "r"
Parameters WRITE = "w"
---------- APPEND = "a"
file_name : str
the path to the file to read
format : str class FileType(Enum):
the format of the file to read (yaml or json) YAML = "yaml"
JSON = "json"
Raises
------
ValueError class File(BaseModel):
if the format is not supported encoding: str
format: FileType
Returns path: str
-------
dict def __init__(self, path: str, filetype: FileType, encoding: str = 'utf-8'):
the parsed content of the file super().__init__(path=path, format=filetype, encoding=encoding)
"""
def open(self, action: FileAction) -> TextIOWrapper:
with open(file_name) as fd: """
if format == "yaml": Open the file with the given action
content = yaml.safe_load(fd)
return content Parameters
elif format == "json": ----------
content = json.load(fd) action : FileAction
else: the action to perform on the file at opening
raise ValueError("Format not supported")
Returns
return content -------
TextIOWrapper
the file descriptor
"""
return open(self.path, action.value, encoding=self.encoding)
def get_content(self) -> dict:
"""
Return the file content
Raises
------
ValueError
if the format is not supported
Returns
-------
dict
the parsed content of the file
"""
with self.open(FileAction.READ) as fd:
if self.format == FileType.YAML:
content = yaml.safe_load(fd)
return content
if self.format == FileType.JSON:
content = json.load(fd)
else:
raise ValueError("Format not supported")
return content
def write_content(self, content: dict) -> None:
"""
Write the given content in the file
Parameters
----------
content : dict
the content to write in the file
"""
with self.open(FileAction.WRITE) as fd:
if self.format == FileType.YAML:
yaml.safe_dump(content, fd)
elif self.format == FileType.JSON:
json.dump(content, fd)
else:
raise ValueError("Format not supported")
...@@ -52,3 +52,20 @@ def prettify(text: str, color: Color = None, styles: list[Style] = None) -> str: ...@@ -52,3 +52,20 @@ def prettify(text: str, color: Color = None, styles: list[Style] = None) -> str:
prepend += style.value prepend += style.value
return prepend + text + END return prepend + text + END
def pprint(text: str, color: Color = None, styles: list[Style] = None) -> None:
"""
Formats a string with some styles and prints it
Parameters
----------
text : str
string to format
color : Color, optional
color to use
styles : list[Style], optional
styles to use
"""
print(prettify(text, color, styles))
import uuid import uuid
from enum import Enum from enum import Enum
from pydantic import BaseModel, Field
from typing import Annotated from typing import Annotated
from pydantic import BaseModel, Field
from mpai_cae_arp.audio.standards import EqualizationStandard, SpeedStandard from mpai_cae_arp.audio.standards import EqualizationStandard, SpeedStandard
...@@ -119,38 +119,35 @@ class Irregularity(BaseModel): ...@@ -119,38 +119,35 @@ class Irregularity(BaseModel):
class IrregularityFile(BaseModel): class IrregularityFile(BaseModel):
# TODO: the offset calculation is not implemented yet, so it is set to None # TODO: the offset calculation is not implemented yet, so it is set to None
irregularities: list[Irregularity] irregularities: Annotated[list[Irregularity], Field(default=[])]
offset: Annotated[int, Field(gt=0)] | None offset: Annotated[int, Field(default=0)]
class Config: class Config:
schema_extra = { schema_extra = {
"example": { "example": {
"offset": 0, "offset":
"irregularities": [ 0,
{ "irregularities": [{
"irregularity_ID": "a0a0a0a0-a0a0-a0a0-a0a0-a0a0a0a0a0a0", "irregularity_ID":
"source": "a", "a0a0a0a0-a0a0-a0a0-a0a0-a0a0a0a0a0a0",
"time_label": "00:00:00:00", "source":
"irregularity_type": "b", "a",
"irregularity_properties": { "time_label":
"reading_speed": "n", "00:00:00:00",
"reading_equalisation": "n", "irregularity_type":
"writing_speed": "n", "b",
"writing_equalisation": "n" "irregularity_properties": {
}, "reading_speed": "n",
"audio_block_URI": "https://example.com/audio.wav", "reading_equalisation": "n",
} "writing_speed": "n",
] "writing_equalisation": "n"
},
"audio_block_URI":
"https://example.com/audio.wav",
}]
} }
} }
def __init__(self,
irregularities: list[Irregularity] = [],
offset: int | None = None):
self.irregularities = irregularities
self.offset = offset
def __eq__(self, __o: object) -> bool: def __eq__(self, __o: object) -> bool:
if not isinstance(__o, IrregularityFile): if not isinstance(__o, IrregularityFile):
return False return False
......
[tool.poetry] [tool.poetry]
name = "mpai-cae-arp" name = "mpai-cae-arp"
version = "0.1.0" version = "0.2.1"
description = "The MPAI CAE-ARP software API" description = "The MPAI CAE-ARP software API"
authors = ["Matteo Spanio <dev2@audioinnova.com>"] authors = ["Matteo Spanio <dev2@audioinnova.com>"]
readme = "README.md" readme = "README.md"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment