Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
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
esde
machine-learning
MLAir
Commits
44c5ed0c
Commit
44c5ed0c
authored
3 years ago
by
Felix Kleinert
Browse files
Options
Downloads
Patches
Plain Diff
fist ideas for sectorial set boots
parent
6560872e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mlair/data_handler/input_bootstraps.py
+92
-17
92 additions, 17 deletions
mlair/data_handler/input_bootstraps.py
with
92 additions
and
17 deletions
mlair/data_handler/input_bootstraps.py
+
92
−
17
View file @
44c5ed0c
...
@@ -75,6 +75,14 @@ class BootstrapIterator(Iterator):
...
@@ -75,6 +75,14 @@ class BootstrapIterator(Iterator):
else
:
else
:
return
self
.
_method
.
apply
(
data
)
return
self
.
_method
.
apply
(
data
)
def
_prepare_data_for_next
(
self
):
index_dimension_collection
=
self
.
_collection
[
self
.
_position
]
nboot
=
self
.
_data
.
number_of_bootstraps
_X
,
_Y
=
self
.
_data
.
data
.
get_data
(
as_numpy
=
False
)
_X
=
list
(
map
(
lambda
x
:
x
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
),
_X
))
_Y
=
_Y
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
)
return
_X
,
_Y
,
index_dimension_collection
class
BootstrapIteratorSingleInput
(
BootstrapIterator
):
class
BootstrapIteratorSingleInput
(
BootstrapIterator
):
_position
:
int
=
None
_position
:
int
=
None
...
@@ -85,11 +93,12 @@ class BootstrapIteratorSingleInput(BootstrapIterator):
...
@@ -85,11 +93,12 @@ class BootstrapIteratorSingleInput(BootstrapIterator):
def
__next__
(
self
):
def
__next__
(
self
):
"""
Return next element or stop iteration.
"""
"""
Return next element or stop iteration.
"""
try
:
try
:
index
,
dimension
=
self
.
_collection
[
self
.
_position
]
_X
,
_Y
,
(
index
,
dimension
)
=
self
.
_prepare_data_for_next
()
nboot
=
self
.
_data
.
number_of_bootstraps
# index, dimension = self._collection[self._position]
_X
,
_Y
=
self
.
_data
.
data
.
get_data
(
as_numpy
=
False
)
# nboot = self._data.number_of_bootstraps
_X
=
list
(
map
(
lambda
x
:
x
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
),
_X
))
# _X, _Y = self._data.data.get_data(as_numpy=False)
_Y
=
_Y
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
)
# _X = list(map(lambda x: x.expand_dims({self.boot_dim: range(nboot)}, axis=-1), _X))
# _Y = _Y.expand_dims({self.boot_dim: range(nboot)}, axis=-1)
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
bootstrapped_variable
=
self
.
apply_bootstrap_method
(
single_variable
.
values
)
bootstrapped_variable
=
self
.
apply_bootstrap_method
(
single_variable
.
values
)
bootstrapped_data
=
xr
.
DataArray
(
bootstrapped_variable
,
coords
=
single_variable
.
coords
,
bootstrapped_data
=
xr
.
DataArray
(
bootstrapped_variable
,
coords
=
single_variable
.
coords
,
...
@@ -117,11 +126,7 @@ class BootstrapIteratorVariable(BootstrapIterator):
...
@@ -117,11 +126,7 @@ class BootstrapIteratorVariable(BootstrapIterator):
def
__next__
(
self
):
def
__next__
(
self
):
"""
Return next element or stop iteration.
"""
"""
Return next element or stop iteration.
"""
try
:
try
:
dimension
=
self
.
_collection
[
self
.
_position
]
_X
,
_Y
,
dimension
=
self
.
_prepare_data_for_next
()
nboot
=
self
.
_data
.
number_of_bootstraps
_X
,
_Y
=
self
.
_data
.
data
.
get_data
(
as_numpy
=
False
)
_X
=
list
(
map
(
lambda
x
:
x
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
),
_X
))
_Y
=
_Y
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
)
for
index
in
range
(
len
(
_X
)):
for
index
in
range
(
len
(
_X
)):
if
dimension
in
_X
[
index
].
coords
[
self
.
_dimension
]:
if
dimension
in
_X
[
index
].
coords
[
self
.
_dimension
]:
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
...
@@ -150,11 +155,12 @@ class BootstrapIteratorBranch(BootstrapIterator):
...
@@ -150,11 +155,12 @@ class BootstrapIteratorBranch(BootstrapIterator):
def
__next__
(
self
):
def
__next__
(
self
):
try
:
try
:
index
=
self
.
_collection
[
self
.
_position
]
_X
,
_Y
,
index
=
self
.
_prepare_data_for_next
()
nboot
=
self
.
_data
.
number_of_bootstraps
# index = self._collection[self._position]
_X
,
_Y
=
self
.
_data
.
data
.
get_data
(
as_numpy
=
False
)
# nboot = self._data.number_of_bootstraps
_X
=
list
(
map
(
lambda
x
:
x
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
),
_X
))
# _X, _Y = self._data.data.get_data(as_numpy=False)
_Y
=
_Y
.
expand_dims
({
self
.
boot_dim
:
range
(
nboot
)},
axis
=-
1
)
# _X = list(map(lambda x: x.expand_dims({self.boot_dim: range(nboot)}, axis=-1), _X))
# _Y = _Y.expand_dims({self.boot_dim: range(nboot)}, axis=-1)
for
dimension
in
_X
[
index
].
coords
[
self
.
_dimension
].
values
:
for
dimension
in
_X
[
index
].
coords
[
self
.
_dimension
].
values
:
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
bootstrapped_variable
=
self
.
apply_bootstrap_method
(
single_variable
.
values
)
bootstrapped_variable
=
self
.
apply_bootstrap_method
(
single_variable
.
values
)
...
@@ -172,6 +178,68 @@ class BootstrapIteratorBranch(BootstrapIterator):
...
@@ -172,6 +178,68 @@ class BootstrapIteratorBranch(BootstrapIterator):
return
list
(
range
(
len
(
data
.
get_X
(
as_numpy
=
False
))))
return
list
(
range
(
len
(
data
.
get_X
(
as_numpy
=
False
))))
class
BootstrapIteratorVariableSets
(
BootstrapIterator
):
_variable_set_splitters
:
list
=
[
'
Sect
'
,
'
SectLeft
'
,
'
SectRight
'
,]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
#self.variable_set_splitters = ['Sect', 'SectLeft', 'SectRight']
def
__next__
(
self
):
try
:
_X
,
_Y
,
(
_index
,
_dimension
)
=
self
.
_prepare_data_for_next
()
for
index
,
dimensions
in
self
.
_collection
:
print
(
index
,
dimensions
)
for
dimension
in
dimensions
:
print
(
index
,
dimension
)
single_variable
=
_X
[
index
].
sel
({
self
.
_dimension
:
[
dimension
]})
bootstrapped_variable
=
self
.
apply_bootstrap_method
(
single_variable
.
values
)
bootstrapped_data
=
xr
.
DataArray
(
bootstrapped_variable
,
coords
=
single_variable
.
coords
,
dims
=
single_variable
.
dims
)
_X
[
index
]
=
bootstrapped_data
.
combine_first
(
_X
[
index
]).
transpose
(
*
_X
[
index
].
dims
)
# for dimension in _X[index].coords[self._dimension].values:
# single_variable = _X[index].sel({self._dimension: [dimension]})
# bootstrapped_variable = self.apply_bootstrap_method(single_variable.values)
# bootstrapped_data = xr.DataArray(bootstrapped_variable, coords=single_variable.coords,
# dims=single_variable.dims)
# _X[index] = bootstrapped_data.combine_first(_X[index]).transpose(*_X[index].dims)
self
.
_position
+=
1
except
IndexError
:
StopIteration
()
_X
,
_Y
=
self
.
_to_numpy
(
_X
),
self
.
_to_numpy
(
_Y
)
return
self
.
_reshape
(
_X
),
self
.
_reshape
(
_Y
),
(
index
,
dimension
)
# return self._reshape(_X), self._reshape(_Y), (None, index)
@classmethod
def
create_collection
(
cls
,
data
,
dim
):
# l = set()
# for i, x in enumerate(data.get_X(as_numpy=False)):
# l.update(x.indexes[dim].to_list())
# # l.update(['O3Sect', 'O3SectLeft', 'O3SectRight']) # ToDo Remove : just for testing
# return [[var for var in to_list(l) if var.endswith(collection_name)] for collection_name in cls._variable_set_splitters]
l
=
[]
for
i
,
x
in
enumerate
(
data
.
get_X
(
as_numpy
=
False
)):
l
.
append
(
x
.
indexes
[
dim
].
to_list
())
l
[
0
]
=
l
[
0
]
+
[
'
o3Sect
'
,
'
o3SectLeft
'
,
'
o3SectRight
'
,
'
no2Sect
'
,
'
no2SectLeft
'
,
'
no2SectRight
'
]
res
=
[[
var
for
var
in
l
[
i
]
if
var
.
endswith
(
collection_name
)]
for
collection_name
in
cls
.
_variable_set_splitters
]
res
=
[(
i
,
dimensions
)
for
i
,
_
in
enumerate
(
data
.
get_X
(
as_numpy
=
False
))
for
dimensions
in
res
]
return
res
# return list(chain(*res))
# [[(0, 'o3'), (0, 'relhum'), (0, 'temp'), (0, 'u'), (0, 'v'), (0, 'no'), (0, 'no2'), (0, 'cloudcover'),
# (0, 'pblheight')]]
# l = []
# for i, x in enumerate(data.get_X(as_numpy=False)):
# l.append(list(map(lambda y: (i, y), x.indexes[dim])))
# return list(chain(*l))
class
ShuffleBootstraps
:
class
ShuffleBootstraps
:
@staticmethod
@staticmethod
...
@@ -225,10 +293,12 @@ class Bootstraps(Iterable):
...
@@ -225,10 +293,12 @@ class Bootstraps(Iterable):
self
.
bootstrap_method
=
{
"
shuffle
"
:
ShuffleBootstraps
(),
self
.
bootstrap_method
=
{
"
shuffle
"
:
ShuffleBootstraps
(),
"
zero_mean
"
:
MeanBootstraps
(
mean
=
0
)}.
get
(
"
zero_mean
"
:
MeanBootstraps
(
mean
=
0
)}.
get
(
bootstrap_method
)
# todo adjust number of bootstraps if mean bootstrapping
bootstrap_method
)
# todo adjust number of bootstraps if mean bootstrapping
self
.
bootstrap_type
=
bootstrap_type
self
.
BootstrapIterator
=
{
"
singleinput
"
:
BootstrapIteratorSingleInput
,
self
.
BootstrapIterator
=
{
"
singleinput
"
:
BootstrapIteratorSingleInput
,
"
branch
"
:
BootstrapIteratorBranch
,
"
branch
"
:
BootstrapIteratorBranch
,
"
variable
"
:
BootstrapIteratorVariable
}.
get
(
bootstrap_type
,
"
variable
"
:
BootstrapIteratorVariable
,
BootstrapIteratorSingleInput
)
"
group_of_variables
"
:
BootstrapIteratorVariableSets
,
}.
get
(
bootstrap_type
,
BootstrapIteratorSingleInput
)
def
__iter__
(
self
):
def
__iter__
(
self
):
return
self
.
BootstrapIterator
(
self
,
self
.
bootstrap_method
)
return
self
.
BootstrapIterator
(
self
,
self
.
bootstrap_method
)
...
@@ -236,6 +306,11 @@ class Bootstraps(Iterable):
...
@@ -236,6 +306,11 @@ class Bootstraps(Iterable):
def
__len__
(
self
):
def
__len__
(
self
):
return
len
(
self
.
BootstrapIterator
.
create_collection
(
self
.
data
,
self
.
bootstrap_dimension
))
return
len
(
self
.
BootstrapIterator
.
create_collection
(
self
.
data
,
self
.
bootstrap_dimension
))
def
__repr__
(
self
):
return
f
"
Bootstraps(data=
{
self
.
data
}
, number_of_bootstraps=
{
self
.
number_of_bootstraps
}
,
"
\
f
"
bootstrap_dimension=
'
{
self
.
bootstrap_dimension
}
'
, bootstrap_type=
'
{
self
.
bootstrap_type
}
'
,
"
\
f
"
bootstrap_method=
'
{
self
.
bootstrap_method
}
'
)
"
def
bootstraps
(
self
):
def
bootstraps
(
self
):
return
self
.
BootstrapIterator
.
create_collection
(
self
.
data
,
self
.
bootstrap_dimension
)
return
self
.
BootstrapIterator
.
create_collection
(
self
.
data
,
self
.
bootstrap_dimension
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment