Skip to content

Commit

Permalink
Used 'Union' for all type unions
Browse files Browse the repository at this point in the history
  • Loading branch information
liebharc committed Sep 11, 2023
1 parent 22b0566 commit 97e30af
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 61 deletions.
40 changes: 20 additions & 20 deletions oemer/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ class Key(enum.Enum):

class Voice:
def __init__(self) -> None:
self.id: int | Any = None
self.id: Union[int, None] = None
self.note_ids: list[int] = []
self.stem_up: bool | Any = None
self.group_id: int | Any = None
self.x_center: float | Any = None
self.label: NoteType | Any = None
self.has_dot: bool | Any = None
self.group: int | Any = None
self.track: int | Any = None
self.duration: int | Any = None
self.rhythm_name: str | Any = None
self.stem_up: Union[bool, None] = None
self.group_id: Union[int, None] = None
self.x_center: Union[float, None] = None
self.label: NoteType = None # type: ignore
self.has_dot: Union[bool, None] = None
self.group: Union[int, None] = None
self.track: Union[int, None] = None
self.duration: int = None # type: ignore
self.rhythm_name: Union[str, None] = None

def init(self) -> None:
notes = layers.get_layer('notes')
Expand All @@ -107,8 +107,8 @@ def init(self) -> None:
notes[nid].force_set_label(self.label)
notes[nid].has_dot = self.has_dot

self.rhythm_name = NOTE_TYPE_TO_RHYTHM[self.label]['name']
self.duration = NOTE_TYPE_TO_RHYTHM[self.label]['duration']
self.rhythm_name = NOTE_TYPE_TO_RHYTHM[self.label]['name'] # type: ignore
self.duration = NOTE_TYPE_TO_RHYTHM[self.label]['duration'] # type: ignore
if self.has_dot:
self.duration = round(self.duration * 1.5) # type: ignore

Expand All @@ -121,18 +121,18 @@ def __repr__(self):
class Measure:
def __init__(self) -> None:
self.symbols: List[Any] = [] # List of symbols
self.double_barline: bool | Any = None
self.double_barline: Union[bool, None] = None
self.has_clef: bool = False
self.clefs: list[Clef] = []
self.voices: list[NoteGroup] = []
self.sfns: list[Sfn] = []
self.rests: list[Rest] = []
self.number: int | Any = None
self.at_beginning: bool | Any = None
self.group: int | Any = None
self.number: Union[int, None] = None
self.at_beginning: bool = None # type: ignore
self.group: Union[int, None] = None

self.time_slots: list[object] = []
self.slot_duras: np.ndarray | Any = None
self.slot_duras: np.ndarray = None # type: ignore

def add_symbols(self, symbols: Union[List[Union[Clef, Rest, Sfn]], List[Voice]]) -> None:
self.symbols.extend(symbols)
Expand Down Expand Up @@ -188,7 +188,7 @@ def get_key(self) -> Key:
# Count occurance
sfn_counts = [0 for _ in range(track_nums)]
for sfn in sfns_cands:
sfn_counts[sfn.track] += 1
sfn_counts[sfn.track] += 1 # type: ignore

# Check validity
all_same = all(ss.label==sfns_cands[0].label for ss in sfns_cands) # All tracks have the same label.
Expand Down Expand Up @@ -466,7 +466,7 @@ def __init__(self, note: NoteHead, chord=False, voice=1, **kwargs):
self.voice = voice

def perform(self, parent_elem: Optional[Element] = None) -> Element:
clef_type = self.ctx.clefs[self.note.track].label
clef_type = self.ctx.clefs[self.note.track].label # type: ignore
chroma = get_chroma_pitch(self.note.staff_line_pos, clef_type)
cur_sfn = self.ctx.sfn_state[chroma]
if (self.note.sfn is not None) and (cur_sfn != self.note.sfn):
Expand Down Expand Up @@ -672,7 +672,7 @@ def gen_measures(self, group_container: Dict[int, List[Any]]) -> None:
num = 1 # Measure count starts from 1 for MusicXML
for grp, insts in group_container.items():
self.measures[grp] = []
buffer: Any = []
buffer: List[Any] = []
at_beginning = True
double_barline = False
for inst in insts:
Expand Down
10 changes: 5 additions & 5 deletions oemer/dewarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from oemer.morph import morph_open
from oemer.logging import get_logger
from numpy import ndarray
from typing import List, Tuple, Any
from typing import List, Tuple, Any, Union
from typing_extensions import Self


Expand All @@ -20,7 +20,7 @@

class Grid:
def __init__(self) -> None:
self.id: int | Any = None
self.id: Union[int, None] = None
self.bbox: list[int] | Any = None # XYXY
self.y_shift: int = 0

Expand All @@ -35,11 +35,11 @@ def height(self):

class GridGroup:
def __init__(self) -> None:
self.id: int | Any = None
self.reg_id: int | Any = None
self.id: Union[int, None] = None
self.reg_id: Union[int, None] = None
self.bbox: list[int] | Any = None
self.gids: list[int] = []
self.split_unit: int | Any = None
self.split_unit: int = None # type: ignore

@property
def y_center(self):
Expand Down
14 changes: 7 additions & 7 deletions oemer/note_group_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
draw_bounding_boxes
)
from numpy import ndarray
from typing import Dict, List, Tuple, Any
from typing import Dict, List, Tuple, Any, Union

# Globals
grp_img: ndarray
Expand All @@ -25,16 +25,16 @@

class NoteGroup:
def __init__(self) -> None:
self.id: int | Any = None
self.id: Union[int, None] = None
self.bbox: list[int] | Any = None
self.note_ids: list[int] = []
self.top_note_ids: list[int] = [] # For multi-melody cases
self.bottom_note_ids: list[int] = [] # For multi-melody cases
self.stem_up: bool | Any = None
self.has_stem: bool | Any = None
self.all_same_type: bool | Any = None # All notes are solid or hollow
self.group: int | Any = None
self.track: int | Any = None
self.stem_up: Union[bool, None] = None
self.has_stem: Union[bool, None] = None
self.all_same_type: Union[bool, None] = None # All notes are solid or hollow
self.group: Union[int, None] = None
self.track: Union[int, None] = None

@property
def x_center(self) -> float:
Expand Down
28 changes: 14 additions & 14 deletions oemer/notehead_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from oemer.logging import get_logger
from oemer.staffline_extraction import Staff
from numpy import ndarray
from typing import List, Tuple, Any
from typing import List, Tuple, Any, Union

# Globals
nn_img: ndarray
Expand All @@ -37,24 +37,24 @@ class NoteType(enum.Enum):
class NoteHead:
def __init__(self) -> None:
self.points: list[tuple] = []
self.pitch: int | Any = None
self.pitch: Union[int, None] = None
self.has_dot: bool = False
self.bbox: list[float] | Any = None # XYXY
self.stem_up: bool | Any = None
self.stem_right: bool | Any = None
self.track: int | Any = None
self.group: int | Any = None
self.staff_line_pos: int | Any = None
self.invalid: bool | Any = False # May be false positive
self.id: int | Any = None
self.note_group_id: int | Any = None
self.stem_up: Union[bool, None] = None
self.stem_right: Union[bool, None] = None
self.track: Union[int, None] = None
self.group: Union[int, None] = None
self.staff_line_pos: int = None # type: ignore
self.invalid: Union[bool, None] = False # May be false positive
self.id: Union[int, None] = None
self.note_group_id: Union[int, None] = None
self.sfn: Any = None # See symbols_extraction.py

# Protected attributes
self._label: NoteType | Any = None
self._label: Union[NoteType, None] = None

@property
def label(self) -> NoteType | Any:
def label(self) -> Union[NoteType, None]:
if self.invalid:
logger.warning(f"Note {self.id} is not a valid note.")
return None
Expand Down Expand Up @@ -356,7 +356,7 @@ def assign_group_track(st: Staff) -> None:
# Estimate position by the closeset center.
pos_idx = np.argmin(np.abs(np.array(pos_cen)-cen_y))
if 0 < pos_idx < len(pos_cen)-1:
nn.staff_line_pos = pos_idx
nn.staff_line_pos = int(pos_idx)
elif pos_idx == 0:
diff = abs(pos_cen[0] - cen_y)
pos = round(diff / step)
Expand Down Expand Up @@ -392,7 +392,7 @@ def parse_stem_info(notes: List[NoteHead]) -> None:
st_cen_x = np.mean(xi)
cen_x = (box[0] + box[2]) / 2
on_right = st_cen_x > cen_x
note.stem_right = on_right
note.stem_right = bool(on_right)
# start_y = box[1] - offset
# end_y = box[3] + offset
# left_sum = np.sum(stems[start_y:end_y, box[0]-offset:box[2]])
Expand Down
29 changes: 14 additions & 15 deletions oemer/symbol_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
from typing import List, Union
from typing import Tuple
from numpy import ndarray
from typing import Any
from typing import Optional
from typing import Any, Union

# Globals
global_cs: ndarray
Expand Down Expand Up @@ -60,9 +59,9 @@ class RestType(enum.Enum):
class Clef:
def __init__(self) -> None:
self.bbox: list[int] | Any = None
self.track: int | Any = None
self.group: int | Any = None
self._label: ClefType | Any = None
self.track: Union[int, None] = None
self.group: Union[int, None] = None
self._label: ClefType = None # type: ignore

@property
def label(self) -> ClefType:
Expand All @@ -84,11 +83,11 @@ def __repr__(self):
class Sfn:
def __init__(self) -> None:
self.bbox: list[int] | Any = None
self.note_id: int | Any = None
self.is_key: bool | Any = None # Whether is key or accidental
self.track: int | Any = None
self.group: int | Any = None
self._label: SfnType | Any = None
self.note_id: Union[int, None] = None
self.is_key: Union[bool, None] = None # Whether is key or accidental
self.track: Union[int, None] = None
self.group: Union[int, None] = None
self._label: SfnType = None # type: ignore

@property
def label(self) -> SfnType:
Expand All @@ -111,10 +110,10 @@ def __repr__(self):
class Rest:
def __init__(self) -> None:
self.bbox: list[int] | Any = None
self.has_dot: bool | Any = None
self.track: int | Any = None
self.group: int | Any = None
self._label: RestType | Any = None
self.has_dot: Union[bool, None] = None
self.track: Union[int, None] = None
self.group: Union[int, None] = None
self._label: RestType = None # type: ignore

@property
def label(self) -> RestType:
Expand All @@ -137,7 +136,7 @@ def __repr__(self):
class Barline:
def __init__(self) -> None:
self.bbox: list[int] | Any = None
self.group: int | Any = None
self.group: Union[int, None] = None

@property
def x_center(self) -> float:
Expand Down

0 comments on commit 97e30af

Please sign in to comment.