Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Mar 19, 2024
1 parent 7a9d209 commit b4e942a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
2 changes: 0 additions & 2 deletions pde/storage/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

import json
import logging
from pathlib import Path
from typing import Any, Literal

Expand Down Expand Up @@ -69,7 +68,6 @@ def __init__(
self.keep_opened = keep_opened
self.check_mpi = check_mpi

self._logger = logging.getLogger(self.__class__.__name__)
self._file: Any = None
self._is_writing = False
self._data_length: int = None # type: ignore
Expand Down
31 changes: 13 additions & 18 deletions pde/storage/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
.. codeauthor:: David Zwicker <david.zwicker@ds.mpg.de>
"""

# TODO: write time as the time stamps (potentially using a factor to convert simulation
# time to real time); this might not be possible with rawvideo. An alternative
# might be to store the time stamps and apply them later, e.g., using `mkvmerge`
from __future__ import annotations

import json
import logging
import shlex
from collections.abc import Iterator, Sequence
from pathlib import Path
Expand Down Expand Up @@ -119,7 +115,6 @@ def __init__(
self.bitrate = bitrate
self.loglevel = loglevel

self._logger = logging.getLogger(self.__class__.__name__)
self._ffmpeg: Any = None
self._state: Literal["closed", "reading", "writing"] = "closed"
self._norms: list[Normalize] | None = None
Expand Down Expand Up @@ -189,26 +184,26 @@ def _read_metadata(self) -> None:
self.info["num_frames"] = None # number of frames was not stored

Check warning on line 184 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L183-L184

Added lines #L183 - L184 were not covered by tests
self.info["width"] = stream["width"]
self.info["height"] = stream["height"]
video_format = self.info.get("video_format")
if video_format is None:
video_format = stream.get("pix_fmt")
if video_format is None:
if self.video_format == "auto":
if self.video_format == "auto":
video_format = self.info.get("video_format")
if video_format is None:
video_format = stream.get("pix_fmt")

Check warning on line 190 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L190

Added line #L190 was not covered by tests
if video_format is None:
raise RuntimeError("Could not determine video format from file")

Check warning on line 192 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L192

Added line #L192 was not covered by tests
else:
video_format = self.video_format
else:
video_format = self.video_format

Check warning on line 194 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L194

Added line #L194 was not covered by tests
try:
self._format = FFmpeg.formats[video_format]
except KeyError:
self._logger.warning(f"Unknown pixel format `{video_format}`")

Check warning on line 198 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L197-L198

Added lines #L197 - L198 were not covered by tests
else:
if self._format.pix_fmt_file != stream["pix_fmt"]:
if self._format.pix_fmt_file != stream.get("pix_fmt"):
self._logger.info(
"Pixel format differs from requested one: "
f"{self._format.pix_fmt_file} != {stream['pix_fmt']}"
f"{self._format.pix_fmt_file} != {stream.get('pix_fmt')}"
)

def _init_normalization(self, field: FieldBase, *, inverse: bool = False) -> None:
def _init_normalization(self, field: FieldBase) -> None:
"""initialize the normalizations of the color information
Args:
Expand Down Expand Up @@ -290,7 +285,7 @@ def start_writing(self, field: FieldBase, info: InfoDict | None = None) -> None:
self._frame_shape = (width, height, self._format.channels)

# set up the normalization
self._init_normalization(field, inverse=False)
self._init_normalization(field)

# set input
self._logger.debug(f"Start ffmpeg process for `{self.filename}`")
Expand Down Expand Up @@ -421,7 +416,7 @@ def _get_field(self, t_index: int) -> FieldBase:
if self._field is None:
self._init_field()

Check warning on line 417 in pde/storage/movie.py

View check run for this annotation

Codecov / codecov/patch

pde/storage/movie.py#L417

Added line #L417 was not covered by tests
assert self._field is not None
self._init_normalization(self._field, inverse=True)
self._init_normalization(self._field)
assert self._norms is not None
frame_shape = (self.info["width"], self.info["height"], self._format.channels)
data_shape = (len(self._norms), self.info["width"], self.info["height"])
Expand Down Expand Up @@ -457,7 +452,7 @@ def __iter__(self) -> Iterator[FieldBase]:
if self._field is None:
self._init_field()
assert self._field is not None
self._init_normalization(self._field, inverse=True)
self._init_normalization(self._field)
assert self._norms is not None
frame_shape = (self.info["width"], self.info["height"], self._format.channels)
data_shape = (len(self._norms), self.info["width"], self.info["height"])
Expand Down

0 comments on commit b4e942a

Please sign in to comment.