Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
mednet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
medai
software
mednet
Commits
17a93c27
Project 'biosignal/software/mednet' was moved to 'medai/software/mednet'. Please update any links and bookmarks that may still have the old path.
Commit
17a93c27
authored
11 months ago
by
Gokhan OZBULAK
Browse files
Options
Downloads
Patches
Plain Diff
Fix qa issues regarding to os usage
#60
parent
dcfa0b5c
No related branches found
No related tags found
No related merge requests found
Pipeline
#85836
failed
11 months ago
Stage: qa
Stage: test
Stage: doc
Stage: dist
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mednet/scripts/upload.py
+53
-45
53 additions, 45 deletions
src/mednet/scripts/upload.py
with
53 additions
and
45 deletions
src/mednet/scripts/upload.py
+
53
−
45
View file @
17a93c27
...
@@ -2,17 +2,16 @@
...
@@ -2,17 +2,16 @@
#
#
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-License-Identifier: GPL-3.0-or-later
import
os
import
configparser
import
json
import
json
import
os
import
pathlib
import
pathlib
import
shutil
import
shutil
import
tempfile
import
tempfile
import
configparser
import
click
import
gitlab
import
gitlab
import
mlflow
import
mlflow
import
click
from
clapper.click
import
ResourceOption
,
verbosity_option
from
clapper.click
import
ResourceOption
,
verbosity_option
from
clapper.logging
import
setup
from
clapper.logging
import
setup
...
@@ -20,15 +19,16 @@ from .click import ConfigCommand
...
@@ -20,15 +19,16 @@ from .click import ConfigCommand
logger
=
setup
(
__name__
.
split
(
"
.
"
)[
0
],
format
=
"
%(levelname)s: %(message)s
"
)
logger
=
setup
(
__name__
.
split
(
"
.
"
)[
0
],
format
=
"
%(levelname)s: %(message)s
"
)
def
get_config
():
def
get_config
():
"""
Return an instance of the Gitlab object for remote operations.
"""
Return an instance of the Gitlab object for remote operations.
Returns
Returns
-------
-------
Gitlab entry and credential.
Gitlab entry and credential.
"""
"""
cfg
=
pathlib
.
Path
(
os
.
path
.
expanduser
(
"
~/.python-gitlab.cfg
"
))
cfg
=
pathlib
.
Path
(
"
~/.python-gitlab.cfg
"
)
.
expanduser
(
)
if
cfg
.
exists
():
if
cfg
.
exists
():
gl
=
gitlab
.
Gitlab
.
from_config
(
"
idiap
"
,
[
str
(
cfg
)])
gl
=
gitlab
.
Gitlab
.
from_config
(
"
idiap
"
,
[
str
(
cfg
)])
config
=
configparser
.
ConfigParser
()
config
=
configparser
.
ConfigParser
()
...
@@ -41,8 +41,9 @@ def get_config():
...
@@ -41,8 +41,9 @@ def get_config():
return
gl
,
config
return
gl
,
config
def
create_temp_copy
(
source
,
target
):
def
create_temp_copy
(
source
,
target
):
"""
Create a copy of original file in temp folder.
"""
Create a copy of original file in temp folder.
Parameters
Parameters
----------
----------
...
@@ -56,11 +57,12 @@ def create_temp_copy(source, target):
...
@@ -56,11 +57,12 @@ def create_temp_copy(source, target):
Path to target file in temp folder.
Path to target file in temp folder.
"""
"""
temp_dir
=
tempfile
.
gettempdir
()
temp_dir
=
pathlib
.
Path
(
tempfile
.
gettempdir
()
)
target
=
os
.
path
.
join
(
temp_dir
,
target
)
target
=
temp_dir
/
target
shutil
.
copy2
(
source
,
target
)
shutil
.
copy2
(
source
,
target
)
return
target
return
target
@click.command
(
@click.command
(
entry_point_group
=
"
mednet.config
"
,
entry_point_group
=
"
mednet.config
"
,
cls
=
ConfigCommand
,
cls
=
ConfigCommand
,
...
@@ -86,30 +88,30 @@ def create_temp_copy(source, target):
...
@@ -86,30 +88,30 @@ def create_temp_copy(source, target):
"""
,
"""
,
)
)
@click.option
(
@click.option
(
"
--output-folder
"
,
"
--output-folder
"
,
"
-o
"
,
"
-o
"
,
help
=
"
Directory in which to upload results from
"
,
help
=
"
Directory in which to upload results from
"
,
required
=
True
,
required
=
True
,
type
=
click
.
Path
(
type
=
click
.
Path
(
file_okay
=
False
,
file_okay
=
False
,
dir_okay
=
True
,
dir_okay
=
True
,
path_type
=
pathlib
.
Path
,
path_type
=
pathlib
.
Path
,
),
),
default
=
"
results
"
,
default
=
"
results
"
,
cls
=
ResourceOption
,
cls
=
ResourceOption
,
)
)
@click.option
(
@click.option
(
"
--experiment-name
"
,
"
--experiment-name
"
,
"
-e
"
,
"
-e
"
,
help
=
'
A string indicating the experiment name (e.g.
"
exp-pasa_mc
"
or
"
exp-densenet_mc-ch
"
)
'
,
help
=
'
A string indicating the experiment name (e.g.
"
exp-pasa_mc
"
or
"
exp-densenet_mc-ch
"
)
'
,
cls
=
ResourceOption
,
cls
=
ResourceOption
,
)
)
@click.option
(
@click.option
(
"
--run-name
"
,
"
--run-name
"
,
"
-r
"
,
"
-r
"
,
help
=
'
A string indicating the run name (e.g.
"
run-1
"
)
'
,
help
=
'
A string indicating the run name (e.g.
"
run-1
"
)
'
,
cls
=
ResourceOption
,
cls
=
ResourceOption
,
)
)
@verbosity_option
(
logger
=
logger
,
cls
=
ResourceOption
,
expose_value
=
False
)
@verbosity_option
(
logger
=
logger
,
cls
=
ResourceOption
,
expose_value
=
False
)
def
upload
(
def
upload
(
output_folder
:
pathlib
.
Path
,
output_folder
:
pathlib
.
Path
,
...
@@ -119,10 +121,9 @@ def upload(
...
@@ -119,10 +121,9 @@ def upload(
)
->
None
:
# numpydoc ignore=PR01
)
->
None
:
# numpydoc ignore=PR01
"""
Upload results from an experiment output folder.
"""
"""
Upload results from an experiment output folder.
"""
logger
.
info
(
f
"
Getting Gitlab credentials for accessing to MLFlow server...
"
)
logger
.
info
(
"
Getting Gitlab credentials for accessing to MLFlow server...
"
)
gitlab
,
config
=
get_config
()
gitlab
,
config
=
get_config
()
project
=
gitlab
.
projects
.
get
(
'
biosignal/software/mednet
'
)
project
=
gitlab
.
projects
.
get
(
"
biosignal/software/mednet
"
)
tracking_uri
=
gitlab
.
api_url
+
f
"
/projects/
{
project
.
id
}
/ml/mlflow
"
os
.
environ
[
"
MLFLOW_TRACKING_TOKEN
"
]
=
config
[
"
idiap
"
][
"
private_token
"
]
os
.
environ
[
"
MLFLOW_TRACKING_TOKEN
"
]
=
config
[
"
idiap
"
][
"
private_token
"
]
os
.
environ
[
"
MLFLOW_TRACKING_URI
"
]
=
(
os
.
environ
[
"
MLFLOW_TRACKING_URI
"
]
=
(
gitlab
.
api_url
+
f
"
/projects/
{
project
.
id
}
/ml/mlflow
"
gitlab
.
api_url
+
f
"
/projects/
{
project
.
id
}
/ml/mlflow
"
...
@@ -132,12 +133,14 @@ def upload(
...
@@ -132,12 +133,14 @@ def upload(
train_output_folder
=
output_folder
/
"
model
"
train_output_folder
=
output_folder
/
"
model
"
train_meta_file
=
train_output_folder
/
"
meta.json
"
train_meta_file
=
train_output_folder
/
"
meta.json
"
train_log_file
=
train_output_folder
/
"
trainlog.pdf
"
train_log_file
=
train_output_folder
/
"
trainlog.pdf
"
train_model_file
=
[
str
(
f
)
for
f
in
train_output_folder
.
glob
(
"
*lowest*
"
)][
0
]
train_model_file
=
[
f
for
f
in
train_output_folder
.
glob
(
"
*lowest*
"
)][
0
]
train_model_temp_file
=
train_model_file
.
split
(
os
.
sep
)[
-
1
].
replace
(
"
=
"
,
"
_
"
)
train_model_temp_file
=
train_model_file
.
parts
[
-
1
].
replace
(
"
=
"
,
"
_
"
)
train_model_temp_file
=
create_temp_copy
(
train_model_file
,
train_model_temp_file
)
train_model_temp_file
=
create_temp_copy
(
train_model_file
,
train_model_temp_file
)
with
train_meta_file
.
open
(
"
r
"
)
as
f
:
with
train_meta_file
.
open
(
"
r
"
)
as
f
:
meta_data
=
json
.
load
(
f
)
meta_data
=
json
.
load
(
f
)
# prepare evaluation files
# prepare evaluation files
evaluation_file
=
output_folder
/
"
evaluation.json
"
evaluation_file
=
output_folder
/
"
evaluation.json
"
evaluation_meta_file
=
output_folder
/
"
evaluation.meta.json
"
evaluation_meta_file
=
output_folder
/
"
evaluation.meta.json
"
...
@@ -147,29 +150,34 @@ def upload(
...
@@ -147,29 +150,34 @@ def upload(
test_data
=
evaluation_data
[
"
test
"
]
test_data
=
evaluation_data
[
"
test
"
]
# prepare experiment and run names
# prepare experiment and run names
experiment_name
=
experiment_name
if
experiment_name
else
f
'
{
meta_data
[
"
model-name
"
]
}
_
{
meta_data
[
"
database-name
"
]
}
'
experiment_name
=
(
experiment_name
if
experiment_name
else
f
'
{
meta_data
[
"
model-name
"
]
}
_
{
meta_data
[
"
database-name
"
]
}
'
)
run_name
=
run_name
if
run_name
else
meta_data
[
"
datetime
"
]
run_name
=
run_name
if
run_name
else
meta_data
[
"
datetime
"
]
logger
.
info
(
f
"
Setting experiment and run names on the MLFlow server...
"
)
logger
.
info
(
"
Setting experiment and run names on the MLFlow server...
"
)
mlflow
.
set_experiment
(
experiment_name
=
experiment_name
)
mlflow
.
set_experiment
(
experiment_name
=
experiment_name
)
with
mlflow
.
start_run
(
run_name
=
run_name
)
as
run
:
with
mlflow
.
start_run
(
run_name
=
run_name
):
# upload metrics
# upload metrics
logger
.
info
(
f
"
Uploading metrics to MLFlow server...
"
)
logger
.
info
(
"
Uploading metrics to MLFlow server...
"
)
mlflow
.
log_metric
(
"
threshold
"
,
test_data
[
"
threshold
"
])
mlflow
.
log_metric
(
"
threshold
"
,
test_data
[
"
threshold
"
])
mlflow
.
log_metric
(
"
precision
"
,
test_data
[
"
precision
"
])
mlflow
.
log_metric
(
"
precision
"
,
test_data
[
"
precision
"
])
mlflow
.
log_metric
(
"
recall
"
,
test_data
[
"
recall
"
])
mlflow
.
log_metric
(
"
recall
"
,
test_data
[
"
recall
"
])
mlflow
.
log_metric
(
"
f1_score
"
,
test_data
[
"
f1_score
"
])
mlflow
.
log_metric
(
"
f1_score
"
,
test_data
[
"
f1_score
"
])
mlflow
.
log_metric
(
"
average_precision_score
"
,
test_data
[
"
average_precision_score
"
])
mlflow
.
log_metric
(
"
average_precision_score
"
,
test_data
[
"
average_precision_score
"
]
)
mlflow
.
log_metric
(
"
specificity
"
,
test_data
[
"
specificity
"
])
mlflow
.
log_metric
(
"
specificity
"
,
test_data
[
"
specificity
"
])
mlflow
.
log_metric
(
"
auc_score
"
,
test_data
[
"
auc_score
"
])
mlflow
.
log_metric
(
"
auc_score
"
,
test_data
[
"
auc_score
"
])
mlflow
.
log_metric
(
"
accuracy
"
,
test_data
[
"
accuracy
"
])
mlflow
.
log_metric
(
"
accuracy
"
,
test_data
[
"
accuracy
"
])
mlflow
.
log_param
(
"
version
"
,
meta_data
[
"
package-version
"
])
mlflow
.
log_param
(
"
version
"
,
meta_data
[
"
package-version
"
])
# upload artifacts
# upload artifacts
logger
.
info
(
f
"
Uploading artifacts to MLFlow server...
"
)
logger
.
info
(
"
Uploading artifacts to MLFlow server...
"
)
mlflow
.
log_artifact
(
train_meta_file
)
mlflow
.
log_artifact
(
train_meta_file
)
mlflow
.
log_artifact
(
train_log_file
)
mlflow
.
log_artifact
(
train_log_file
)
mlflow
.
log_artifact
(
train_model_temp_file
)
mlflow
.
log_artifact
(
train_model_temp_file
)
mlflow
.
log_artifact
(
evaluation_file
)
mlflow
.
log_artifact
(
evaluation_file
)
mlflow
.
log_artifact
(
evaluation_meta_file
)
mlflow
.
log_artifact
(
evaluation_meta_file
)
mlflow
.
log_artifact
(
evaluation_log_file
)
mlflow
.
log_artifact
(
evaluation_log_file
)
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