moseq2-extract 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- moseq2_extract-1.4.0/LICENSE.md +49 -0
- moseq2_extract-1.4.0/PKG-INFO +108 -0
- moseq2_extract-1.4.0/README.md +72 -0
- moseq2_extract-1.4.0/moseq2_extract/__init__.py +1 -0
- moseq2_extract-1.4.0/moseq2_extract/_yaml.py +73 -0
- moseq2_extract-1.4.0/moseq2_extract/cli.py +818 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/__init__.py +0 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/extract.py +261 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/flip_classifier.py +236 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/proc.py +730 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/roi.py +125 -0
- moseq2_extract-1.4.0/moseq2_extract/extract/track.py +321 -0
- moseq2_extract-1.4.0/moseq2_extract/gui.py +354 -0
- moseq2_extract-1.4.0/moseq2_extract/helpers/__init__.py +0 -0
- moseq2_extract-1.4.0/moseq2_extract/helpers/data.py +490 -0
- moseq2_extract-1.4.0/moseq2_extract/helpers/extract.py +289 -0
- moseq2_extract-1.4.0/moseq2_extract/helpers/wrappers.py +824 -0
- moseq2_extract-1.4.0/moseq2_extract/io/__init__.py +0 -0
- moseq2_extract-1.4.0/moseq2_extract/io/image.py +136 -0
- moseq2_extract-1.4.0/moseq2_extract/io/video.py +788 -0
- moseq2_extract-1.4.0/moseq2_extract/util.py +1093 -0
- moseq2_extract-1.4.0/pyproject.toml +66 -0
- moseq2_extract-1.4.0/tests/__init__.py +0 -0
- moseq2_extract-1.4.0/tests/integration_tests/__init__.py +0 -0
- moseq2_extract-1.4.0/tests/integration_tests/test_cli.py +286 -0
- moseq2_extract-1.4.0/tests/integration_tests/test_gui.py +292 -0
- moseq2_extract-1.4.0/tests/unit_tests/__init__.py +0 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_extract_proc.py +322 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_extract_roi.py +57 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_extract_track.py +72 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_helper_data.py +125 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_helper_extract.py +174 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_io_image.py +52 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_io_video.py +138 -0
- moseq2_extract-1.4.0/tests/unit_tests/test_util.py +303 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Non-Commercial Research and Academic Use Software License and Terms of Use
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
MoSeq is a software package that includes original code created by the Harvard researchers listed below (the “Software”), and third-party code that may be obtained by End Users separately. The Software is designed to label behaviors identified from 3D pose dynamics data through the use of computational modeling and fitting approaches. The Software was developed by Alexander Wiltschko, Ph.D., Sandeep Robert Datta, Ph.D., and Matthew J. Johnson, Ph.D. at Harvard University. It is distributed for free academic and non-commercial research use by the President and Fellows of Harvard College (“Harvard”).
|
|
5
|
+
|
|
6
|
+
Using the Software indicates your agreement to be bound by the terms of this Software Use Agreement (“Agreement”). Absent your agreement to the terms below, you (the “End User”) have no rights to hold or use the Software whatsoever.
|
|
7
|
+
|
|
8
|
+
Harvard agrees to grant hereunder a limited non-exclusive license to End User for the use of the Software in the performance of End User’s internal, non-commercial research and academic use at End User’s academic or not-for-profit research institution (“Institution”) on the following terms and conditions:
|
|
9
|
+
|
|
10
|
+
1. **NO COMMERCIAL USE.** End User shall not use the Software for Commercial use and any such use of the Software is expressly prohibited. “Commercial use” includes, but is not limited to, (i) use of the Software in fee-for-service arrangements, (ii) use of the Software by core facilities or laboratories to provide research services to (or in collaboration with) for-profit third parties for a fee, and (iii) use of the Software in industry-sponsored and/or collaborative research projects in which any commercial rights are granted to the sponsor or collaborator. If End User wishes to use the Software for Commercial use, End User must execute a separate license agreement with Harvard.
|
|
11
|
+
|
|
12
|
+
_Requests for use of the Software for Commercial use, please contact:_
|
|
13
|
+
|
|
14
|
+
Office of Technology Development
|
|
15
|
+
Harvard University
|
|
16
|
+
Smith Campus Center, Suite 727E
|
|
17
|
+
1350 Massachusetts Avenue Cambridge, MA 02138 USA Telephone: (617) 495-3067
|
|
18
|
+
Facsimile: (617) 495-9568
|
|
19
|
+
E-mail: otd@harvard.edu
|
|
20
|
+
|
|
21
|
+
2. **OWNERSHIP AND COPYRIGHT NOTICE.** Harvard owns all intellectual property in the Software. End User shall gain no ownership to the Software. End User shall not remove or delete, and shall retain in the Software (including in any modifications to the Software and in any Derivative Works), the copyright, trademark, or other notices pertaining to Software as provided with the Software.
|
|
22
|
+
|
|
23
|
+
3. **DERIVATIVE WORKS.** End User may create use and distribute Derivative Works, as such term is defined under U.S. copyright laws, provided that any such Derivative Works shall be restricted to non-commercial research and academic use. End User may not distribute Derivative Works to any for-profit third parties for Commercial use.
|
|
24
|
+
|
|
25
|
+
4. **FEEDBACK.** In order to improve the Software, comments from End Users may be useful. End User agrees to provide Harvard with feedback on the End User’s use of the Software (e.g., any bugs in the Software, the user experience, etc.). Harvard is permitted to use such information provided by End User in making changes and improvements to the Software without compensation or accounting to End User.
|
|
26
|
+
|
|
27
|
+
5. **NON ASSERT.** End User acknowledges that Harvard may develop modifications to the Software that may be based on the feedback provided by End User under Section 5 above. Harvard shall not be restricted in any way by End User regarding its use of such information. End User acknowledges the right of Harvard to prepare, publish, display, reproduce, transmit and or use modifications to the Software that may be substantially similar or functionally equivalent to End User’s modifications and/or improvements if any. In the event that End User obtains patent protection for any modification or improvement to Software, End User agrees not to allege or enjoin infringement of End User’s patent against Harvard, or any of the researchers, medical or research staff, officers, directors and employees of those institutions.
|
|
28
|
+
|
|
29
|
+
6. **PUBLICATION & ATTRIBUTION.** End User has the right to publish, present, or share results from the use of the Software. In accordance with customary academic practice, End User will acknowledge Harvard as the provider of the Software and may cite the relevant reference(s) from the following list of publications:
|
|
30
|
+
|
|
31
|
+
Wiltschko AB, Johnson MJ, Iurilli G, Peterson RE, Katon JE, Pashkovski SL, Abraira VE, Adams RP and Datta SR. (2015). Mapping sub-second Structure in Behavior. Neuron, 88:1121.
|
|
32
|
+
|
|
33
|
+
Markowitz JE, Gillis WF, Beron CC, Neufeld SQ, Robertson K, Bhagat ND, Peterson RE, Peterson E, Hyun M, Linderman SW, Sabatini BL and Datta SR. (2018). The striatum organizes 3D behavior via moment-to-moment action selection. Cell, in press.
|
|
34
|
+
|
|
35
|
+
7.** NO WARRANTIES.** THE SOFTWARE IS PROVIDED "AS IS." TO THE FULLEST EXTENT PERMITTED BY LAW, HARVARD HEREBY DISCLAIMS ALL WARRANTIES OF ANY KIND (EXPRESS, IMPLIED OR OTHERWISE) REGARDING THE SOFTWARE, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OWNERSHIP, AND NON-INFRINGEMENT. HARVARD MAKES NO WARRANTY ABOUT THE ACCURACY, RELIABILITY, COMPLETENESS, TIMELINESS, SUFFICIENCY OR QUALITY OF THE SOFTWARE. HARVARD DOES NOT WARRANT THAT THE SOFTWARE WILL OPERATE WITHOUT ERROR OR INTERRUPTION.
|
|
36
|
+
|
|
37
|
+
8. **LIMITATIONS OF LIABILITY AND REMEDIES.** USE OF THE SOFTWARE IS AT END USER’S OWN RISK. IF END USER IS DISSATISFIED WITH THE SOFTWARE, ITS EXCLUSIVE REMEDY IS TO STOP USING IT. IN NO EVENT SHALL HARVARD BE LIABLE TO END USER OR ITS INSTITUTION, IN CONTRACT, TORT OR OTHERWISE, FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR OTHER DAMAGES OF ANY KIND WHATSOEVER ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE, EVEN IF HARVARD IS NEGLIGENT OR OTHERWISE AT FAULT, AND REGARDLESS OF WHETHER HARVARD IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
|
38
|
+
|
|
39
|
+
9. **INDEMNIFICATION.** To the extent permitted by law, End User shall indemnify, defend and hold harmless Harvard, their corporate affiliates, current or future directors, trustees, officers, faculty, medical and professional staff, employees, students and agents and their respective successors, heirs and assigns (the "Indemnitees"), against any liability, damage, loss or expense (including reasonable attorney's fees and expenses of litigation) incurred by or imposed upon the Indemnitees or any one of them in connection with any claims, suits, actions, demands or judgments arising from End User’s breach of this Agreement or its Institution’s use of the Software except to the extent caused by the gross negligence or willful misconduct of Harvard. This indemnification provision shall survive expiration or termination of this Agreement.
|
|
40
|
+
|
|
41
|
+
10. **GOVERNING LAW.** This Agreement shall be construed and governed by the laws of the Commonwealth of Massachusetts regardless of otherwise applicable choice of law standards.
|
|
42
|
+
|
|
43
|
+
11. **NON-USE OF NAME.** Nothing in this License and Terms of Use shall be construed as granting End Users or their Institutions any rights or licenses to use any trademarks, service marks or logos associated with the Software. End User may not use the terms “Harvard” (or a substantially similar term) in any way that is inconsistent with the permitted uses described herein. End Users may not use any name or emblem of Harvard or any of its schools or subdivisions for any purpose, or to falsely suggest any relationship between End User (or its Institution) and Harvard, or in any manner that would infringe or violate any of its rights.
|
|
44
|
+
|
|
45
|
+
12. End User represents and warrants that it has the legal authority to enter into this License and Terms of Use on behalf of itself and its Institution.
|
|
46
|
+
|
|
47
|
+
***
|
|
48
|
+
|
|
49
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: moseq2-extract
|
|
3
|
+
Version: 1.4.0
|
|
4
|
+
Summary: To boldly go where no mouse has gone before
|
|
5
|
+
Author: Datta Lab
|
|
6
|
+
License-Expression: LicenseRef-Harvard-Academic-Non-Commercial
|
|
7
|
+
License-File: LICENSE.md
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: click>=7.0
|
|
19
|
+
Requires-Dist: cytoolz>=0.10.1
|
|
20
|
+
Requires-Dist: h5py>=2.10.0
|
|
21
|
+
Requires-Dist: joblib>=0.15.1
|
|
22
|
+
Requires-Dist: matplotlib>=3.1.2
|
|
23
|
+
Requires-Dist: numpy>=1.18.3
|
|
24
|
+
Requires-Dist: opencv-python>=4.1.2.30
|
|
25
|
+
Requires-Dist: ruamel.yaml>=0.16.5
|
|
26
|
+
Requires-Dist: scikit-image>=0.16.2
|
|
27
|
+
Requires-Dist: scipy>=1.3.2
|
|
28
|
+
Requires-Dist: statsmodels>=0.10.2
|
|
29
|
+
Requires-Dist: tqdm>=4.48.0
|
|
30
|
+
Requires-Dist: tifffile>=2024.7.24
|
|
31
|
+
Provides-Extra: docs
|
|
32
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
33
|
+
Requires-Dist: sphinx-click; extra == "docs"
|
|
34
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# MoSeq2-Extract: Depth Video Rodent-Tracking Toolkit
|
|
38
|
+
|
|
39
|
+
[](https://travis-ci.com/dattalab/moseq2-extract)
|
|
40
|
+
[](https://codecov.io/gh/dattalab/moseq2-extract)
|
|
41
|
+
[](https://zenodo.org/badge/latestdoi/120829989)
|
|
42
|
+
|
|
43
|
+
# Requirements
|
|
44
|
+
|
|
45
|
+
`moseq2-extract` reads and writes depth video by calling **ffmpeg** and
|
|
46
|
+
**ffprobe**, which it expects to find on your `PATH`. They are not Python
|
|
47
|
+
packages and installing `moseq2-extract` does not bring them with it, so install
|
|
48
|
+
them yourself first:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
conda install -c conda-forge ffmpeg # any platform, if you use conda
|
|
52
|
+
sudo apt install ffmpeg # Debian / Ubuntu
|
|
53
|
+
brew install ffmpeg # macOS
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
ffmpeg 4.2 and newer are supported. `moseq2-extract` reports what it found and
|
|
57
|
+
what it needs if either program is missing, and every command checks before it
|
|
58
|
+
starts work.
|
|
59
|
+
|
|
60
|
+
# Installation
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install moseq2-extract
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Python 3.10 or newer, on Linux. macOS and Windows support is being verified.
|
|
67
|
+
|
|
68
|
+
ffmpeg has to be on your `PATH` first; see Requirements above.
|
|
69
|
+
|
|
70
|
+
Most people install the whole pipeline in one step with `pip install
|
|
71
|
+
moseq2-app`, which brings this package with it. See [the installation
|
|
72
|
+
guide](https://github.com/dattalab/moseq2-app/wiki/MoSeq2-Installation).
|
|
73
|
+
|
|
74
|
+
# [Documentation: MoSeq2 Wiki](https://github.com/dattalab/moseq2-app/wiki)
|
|
75
|
+
You can find more information about MoSeq Pipeline, installation, step-by-step instructions, documentation for Command Line Interface(CLI), tutorials etc in [MoSeq2 Wiki](https://github.com/dattalab/moseq2-app/wiki).
|
|
76
|
+
|
|
77
|
+
You can run `moseq2-extract --version` to check the current version and `moseq2-extract --help` to see all the commands.
|
|
78
|
+
```bash
|
|
79
|
+
Usage: moseq2-extract [OPTIONS] COMMAND [ARGS]...
|
|
80
|
+
|
|
81
|
+
Options:
|
|
82
|
+
--version Show the version and exit. [default: False]
|
|
83
|
+
--help Show this message and exit. [default: False]
|
|
84
|
+
|
|
85
|
+
Commands:
|
|
86
|
+
aggregate-results Copies all extracted results (h5, yaml, avi) files...
|
|
87
|
+
batch-extract Batch processes all the raw depth recordings located...
|
|
88
|
+
convert-raw-to-avi Converts/Compresses a raw depth file into an avi file...
|
|
89
|
+
copy-slice Copies a segment of an input depth recording into a...
|
|
90
|
+
download-flip-file Downloads Flip-correction model that helps with...
|
|
91
|
+
extract Processes raw input depth recordings to output a...
|
|
92
|
+
find-roi Finds the ROI and background distance to subtract
|
|
93
|
+
from...
|
|
94
|
+
generate-config Generates a configuration file that holds editable...
|
|
95
|
+
generate-index Generates an index YAML file containing all extracted...
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# Community Support and Contributing
|
|
100
|
+
- Please join [](https://moseqworkspace.slack.com) to post questions and interactive with MoSeq developers and users.
|
|
101
|
+
- If you encounter bugs, errors or issues, please submit a Bug report [here](https://github.com/dattalab/moseq2-app/issues/new/choose). We encourage you to check out the [troubleshooting and tips section](https://github.com/dattalab/moseq2-app/wiki/Troubleshooting-and-Tips) and search your issues in [the existing issues](https://github.com/dattalab/moseq2-app/issues) first.
|
|
102
|
+
- If you want to see certain features in MoSeq or you have new ideas, please submit a Feature request [here](https://github.com/dattalab/moseq2-app/issues/new/choose).
|
|
103
|
+
- If you want to contribute to our codebases, please check out our [Developer Guidelines](https://github.com/dattalab/moseq2-app/wiki/MoSeq-Developer-Guidelines).
|
|
104
|
+
- Please tell us what you think by filling out [this user survey](https://forms.gle/FbtEN8E382y8jF3p6).
|
|
105
|
+
|
|
106
|
+
# License
|
|
107
|
+
|
|
108
|
+
MoSeq is freely available for academic use under a license provided by Harvard University. Please refer to the license file for details. If you are interested in using MoSeq for commercial purposes please contact Bob Datta directly at srdatta@hms.harvard.edu, who will put you in touch with the appropriate people in the Harvard Technology Transfer office.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# MoSeq2-Extract: Depth Video Rodent-Tracking Toolkit
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.com/dattalab/moseq2-extract)
|
|
4
|
+
[](https://codecov.io/gh/dattalab/moseq2-extract)
|
|
5
|
+
[](https://zenodo.org/badge/latestdoi/120829989)
|
|
6
|
+
|
|
7
|
+
# Requirements
|
|
8
|
+
|
|
9
|
+
`moseq2-extract` reads and writes depth video by calling **ffmpeg** and
|
|
10
|
+
**ffprobe**, which it expects to find on your `PATH`. They are not Python
|
|
11
|
+
packages and installing `moseq2-extract` does not bring them with it, so install
|
|
12
|
+
them yourself first:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
conda install -c conda-forge ffmpeg # any platform, if you use conda
|
|
16
|
+
sudo apt install ffmpeg # Debian / Ubuntu
|
|
17
|
+
brew install ffmpeg # macOS
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
ffmpeg 4.2 and newer are supported. `moseq2-extract` reports what it found and
|
|
21
|
+
what it needs if either program is missing, and every command checks before it
|
|
22
|
+
starts work.
|
|
23
|
+
|
|
24
|
+
# Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install moseq2-extract
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Python 3.10 or newer, on Linux. macOS and Windows support is being verified.
|
|
31
|
+
|
|
32
|
+
ffmpeg has to be on your `PATH` first; see Requirements above.
|
|
33
|
+
|
|
34
|
+
Most people install the whole pipeline in one step with `pip install
|
|
35
|
+
moseq2-app`, which brings this package with it. See [the installation
|
|
36
|
+
guide](https://github.com/dattalab/moseq2-app/wiki/MoSeq2-Installation).
|
|
37
|
+
|
|
38
|
+
# [Documentation: MoSeq2 Wiki](https://github.com/dattalab/moseq2-app/wiki)
|
|
39
|
+
You can find more information about MoSeq Pipeline, installation, step-by-step instructions, documentation for Command Line Interface(CLI), tutorials etc in [MoSeq2 Wiki](https://github.com/dattalab/moseq2-app/wiki).
|
|
40
|
+
|
|
41
|
+
You can run `moseq2-extract --version` to check the current version and `moseq2-extract --help` to see all the commands.
|
|
42
|
+
```bash
|
|
43
|
+
Usage: moseq2-extract [OPTIONS] COMMAND [ARGS]...
|
|
44
|
+
|
|
45
|
+
Options:
|
|
46
|
+
--version Show the version and exit. [default: False]
|
|
47
|
+
--help Show this message and exit. [default: False]
|
|
48
|
+
|
|
49
|
+
Commands:
|
|
50
|
+
aggregate-results Copies all extracted results (h5, yaml, avi) files...
|
|
51
|
+
batch-extract Batch processes all the raw depth recordings located...
|
|
52
|
+
convert-raw-to-avi Converts/Compresses a raw depth file into an avi file...
|
|
53
|
+
copy-slice Copies a segment of an input depth recording into a...
|
|
54
|
+
download-flip-file Downloads Flip-correction model that helps with...
|
|
55
|
+
extract Processes raw input depth recordings to output a...
|
|
56
|
+
find-roi Finds the ROI and background distance to subtract
|
|
57
|
+
from...
|
|
58
|
+
generate-config Generates a configuration file that holds editable...
|
|
59
|
+
generate-index Generates an index YAML file containing all extracted...
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Community Support and Contributing
|
|
64
|
+
- Please join [](https://moseqworkspace.slack.com) to post questions and interactive with MoSeq developers and users.
|
|
65
|
+
- If you encounter bugs, errors or issues, please submit a Bug report [here](https://github.com/dattalab/moseq2-app/issues/new/choose). We encourage you to check out the [troubleshooting and tips section](https://github.com/dattalab/moseq2-app/wiki/Troubleshooting-and-Tips) and search your issues in [the existing issues](https://github.com/dattalab/moseq2-app/issues) first.
|
|
66
|
+
- If you want to see certain features in MoSeq or you have new ideas, please submit a Feature request [here](https://github.com/dattalab/moseq2-app/issues/new/choose).
|
|
67
|
+
- If you want to contribute to our codebases, please check out our [Developer Guidelines](https://github.com/dattalab/moseq2-app/wiki/MoSeq-Developer-Guidelines).
|
|
68
|
+
- Please tell us what you think by filling out [this user survey](https://forms.gle/FbtEN8E382y8jF3p6).
|
|
69
|
+
|
|
70
|
+
# License
|
|
71
|
+
|
|
72
|
+
MoSeq is freely available for academic use under a license provided by Harvard University. Please refer to the license file for details. If you are interested in using MoSeq for commercial purposes please contact Bob Datta directly at srdatta@hms.harvard.edu, who will put you in touch with the appropriate people in the Harvard Technology Transfer office.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.4.0"
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""YAML reading and writing, in one place.
|
|
2
|
+
|
|
3
|
+
``ruamel.yaml`` removed the module-level ``safe_load``/``safe_dump`` functions in
|
|
4
|
+
0.18 in favour of instantiating a parser. Routing every call through here keeps
|
|
5
|
+
that detail in one file rather than at each of the call sites.
|
|
6
|
+
|
|
7
|
+
The parser stays ``ruamel``, deliberately. ``ruamel``'s safe loader is YAML 1.2;
|
|
8
|
+
PyYAML is YAML 1.1 and reads unquoted ``yes``/``no``/``on``/``off`` as booleans
|
|
9
|
+
and ``06:00:00`` as a sexagesimal integer. moseq2 config and index files contain
|
|
10
|
+
both shapes, so switching parsers would quietly change how existing user files
|
|
11
|
+
are read.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import io
|
|
15
|
+
|
|
16
|
+
from ruamel.yaml import YAML
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _parser():
|
|
20
|
+
"""A YAML parser configured the way ``safe_dump`` was.
|
|
21
|
+
|
|
22
|
+
A new one per call, because a ``YAML`` instance carries a serializer across
|
|
23
|
+
calls: if a value cannot be represented, the failed call leaves that
|
|
24
|
+
serializer open and every later dump writes nothing at all, silently. Two
|
|
25
|
+
callers here move their output over a file the user already has, so a
|
|
26
|
+
dumper that quietly produces nothing destroys data.
|
|
27
|
+
|
|
28
|
+
``pure=True`` selects the Python implementation, matching what the
|
|
29
|
+
module-level functions used.
|
|
30
|
+
|
|
31
|
+
The two assignments restore emitter behaviour that ``YAML()`` does not
|
|
32
|
+
share with ``safe_dump``. ``allow_unicode`` escapes non-ASCII rather than
|
|
33
|
+
writing it literally, which also keeps a write from depending on the
|
|
34
|
+
locale encoding of the file handle. ``width`` is 80 in both, but the two
|
|
35
|
+
versions measure the column at which a flow sequence may break two
|
|
36
|
+
characters apart, so 82 here reproduces where ``safe_dump`` put its line
|
|
37
|
+
breaks -- both for the two-element ``path: [x.h5, x.yaml]`` entries that
|
|
38
|
+
fill an index file and for sequences long enough to wrap.
|
|
39
|
+
|
|
40
|
+
One emitter difference is left, and no configuration reaches it: where a
|
|
41
|
+
flow sequence does wrap, this version writes a space before the break. The
|
|
42
|
+
breaks land in the same places, so the files moseq2 writes -- indexes,
|
|
43
|
+
session results, configs, crowd-movie info -- come out byte for byte as
|
|
44
|
+
they did.
|
|
45
|
+
"""
|
|
46
|
+
parser = YAML(typ="safe", pure=True)
|
|
47
|
+
parser.allow_unicode = False
|
|
48
|
+
parser.width = 82
|
|
49
|
+
return parser
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def read_yaml(path_or_stream):
|
|
53
|
+
"""Parse a YAML file, given a path or an open stream."""
|
|
54
|
+
if hasattr(path_or_stream, "read"):
|
|
55
|
+
return _parser().load(path_or_stream)
|
|
56
|
+
with open(path_or_stream, "r") as handle:
|
|
57
|
+
return _parser().load(handle)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def write_yaml(data, path_or_stream):
|
|
61
|
+
"""Write ``data`` as YAML, given a path or an open stream."""
|
|
62
|
+
if hasattr(path_or_stream, "write"):
|
|
63
|
+
_parser().dump(data, path_or_stream)
|
|
64
|
+
return
|
|
65
|
+
with open(path_or_stream, "w") as handle:
|
|
66
|
+
_parser().dump(data, handle)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def dump_yaml(data):
|
|
70
|
+
"""Serialize ``data`` to a YAML string."""
|
|
71
|
+
stream = io.StringIO()
|
|
72
|
+
_parser().dump(data, stream)
|
|
73
|
+
return stream.getvalue()
|