Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rechunk-data
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ch1187
rechunk-data
Merge requests
!4
Tests
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Tests
tests
into
main
Overview
0
Commits
12
Pipelines
1
Changes
14
Merged
Martin Bergemann
requested to merge
tests
into
main
2 years ago
Overview
0
Commits
12
Pipelines
1
Changes
14
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
fb8087b8
12 commits,
2 years ago
14 files
+
554
−
138
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
14
Search (e.g. *.vue) (Ctrl+P)
src/rechunk_data/tests/conftest.py
0 → 100644
+
118
−
0
Options
"""
pytest definitions to run the unittests.
"""
from
pathlib
import
Path
from
tempfile
import
TemporaryDirectory
,
NamedTemporaryFile
from
typing
import
Generator
,
Tuple
import
dask
import
pytest
import
numpy
as
np
import
xarray
as
xr
def
create_data
(
variable_name
:
str
,
chunk_size
:
Tuple
[
int
,
...],
dims
:
Tuple
[
str
,
...]
)
->
xr
.
Dataset
:
"""
Create a netcdf dataset.
"""
coords
=
{
d
:
np
.
ones
(
chunk_size
[
n
])
for
(
n
,
d
)
in
enumerate
(
dims
)}
dset
=
xr
.
DataArray
(
np
.
zeros
(
chunk_size
),
dims
=
dims
,
coords
=
coords
,
name
=
variable_name
,
).
chunk
(
dict
(
zip
(
dims
,
chunk_size
)))
dset
.
encoding
=
{
"
chunksizes
"
:
chunk_size
}
return
xr
.
Dataset
({
variable_name
:
dset
})
@pytest.fixture
(
scope
=
"
session
"
)
def
small_chunk
()
->
Generator
[
Tuple
[
int
,
int
,
int
,
int
],
None
,
None
]:
"""
Define tuple for smaller chunks sizes.
"""
yield
(
1
,
1
,
24
,
24
)
@pytest.fixture
(
scope
=
"
session
"
)
def
large_chunk
()
->
Generator
[
Tuple
[
int
,
int
,
int
,
int
],
None
,
None
]:
"""
Define tuple for smaller chunks sizes.
"""
yield
(
720
,
12
,
4
,
4
)
@pytest.fixture
(
scope
=
"
session
"
)
def
dims
()
->
Generator
[
Tuple
[
str
,
str
,
str
,
str
],
None
,
None
]:
"""
Dimensions of all datasets.
"""
yield
(
"
time
"
,
"
height
"
,
"
Latitude
"
,
"
Longitude
"
)
@pytest.fixture
(
scope
=
"
function
"
)
def
temp_dir
()
->
Generator
[
Path
,
None
,
None
]:
"""
Temporary Directory for creating data files.
"""
with
TemporaryDirectory
()
as
temporary_dir
:
yield
Path
(
temporary_dir
)
@pytest.fixture
(
scope
=
"
session
"
)
def
small_chunk_data
(
small_chunk
:
Tuple
[
int
,
int
,
int
,
int
],
dims
:
Tuple
[
str
,
str
,
str
,
str
],
variable_name
:
str
,
)
->
Generator
[
xr
.
Dataset
,
None
,
None
]:
"""
Create a dataset with small chunks.
"""
yield
create_data
(
variable_name
,
small_chunk
,
dims
)
@pytest.fixture
(
scope
=
"
function
"
)
def
large_chunk_data
(
large_chunk
:
Tuple
[
int
,
int
,
int
,
int
],
dims
:
Tuple
[
str
,
str
,
str
,
str
],
variable_name
:
str
,
)
->
Generator
[
xr
.
Dataset
,
None
,
None
]:
"""
Create a dataset with small chunks.
"""
with
dask
.
config
.
set
({
"
array.chunk-size
"
:
"
1MiB
"
}):
yield
create_data
(
variable_name
,
large_chunk
,
dims
)
@pytest.fixture
(
scope
=
"
session
"
)
def
variable_name
()
->
str
:
return
"
tas
"
@pytest.fixture
(
scope
=
"
function
"
)
def
data_dir
(
temp_dir
:
Path
,
variable_name
:
str
,
small_chunk
:
Tuple
[
int
,
int
,
int
,
int
],
dims
:
Tuple
[
str
,
str
,
str
],
small_chunk_data
:
xr
.
Dataset
,
)
->
Generator
[
Path
,
None
,
None
]:
"""
Create a directory with netcdf files.
"""
encoding
=
{
variable_name
:
{
"
chunksizes
"
:
small_chunk
}}
for
number
in
range
(
1
,
10
):
file_name
=
temp_dir
/
"
foo
"
/
"
bar
"
/
f
"
tas_model1_
{
number
}
.nc
"
file_name
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
small_chunk_data
.
to_netcdf
(
file_name
,
encoding
=
encoding
)
yield
temp_dir
@pytest.fixture
(
scope
=
"
function
"
)
def
data_file
(
variable_name
:
str
,
large_chunk
:
Tuple
[
int
,
int
,
int
,
int
],
dims
:
Tuple
[
str
,
str
,
str
],
large_chunk_data
:
xr
.
Dataset
,
)
->
Generator
[
Path
,
None
,
None
]:
"""
Create a directory with netcdf files.
"""
encoding
=
{
variable_name
:
{
"
chunksizes
"
:
large_chunk
}}
with
NamedTemporaryFile
(
suffix
=
"
.nc
"
)
as
temp_file
:
file_name
=
Path
(
temp_file
.
name
)
large_chunk_data
.
to_netcdf
(
file_name
,
encoding
=
encoding
)
yield
file_name
@pytest.fixture
(
scope
=
"
function
"
)
def
wrong_file_type
(
temp_dir
)
->
Generator
[
Path
,
None
,
None
]:
"""
Temporary Directory contaning non netcdf files.
"""
for
_
in
range
(
1
,
3
):
file_name
=
temp_dir
/
"
worng_file.txt
"
file_name
.
touch
()
yield
temp_dir
Loading