Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhance] Substitute the environment variable in config file. #744

Merged
merged 12 commits into from
Feb 1, 2023

Conversation

jbwang1997
Copy link
Contributor

@jbwang1997 jbwang1997 commented Nov 18, 2022

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Enable using environment variables in config files. Can make config more flexible.

Example 1

demo_config.py

# dataset settings
dataset_type = 'CocoDataset'
data_root = '{{$MMDET_DATASET:/data/coco/}}'

# file_client_args = dict(
#     backend='petrel',
#     path_mapping=dict({
#         './data/': 's3://openmmlab/datasets/detection/',
#         'data/': 's3://openmmlab/datasets/detection/'
#     }))
file_client_args = dict(backend='disk')

test.py

from mmengine.config import Config

cfg = Config.fromfile('demo_config.py')
print(cfg)

The results of this pr are as below:

>> python test.py
Config (path: demo_config.py): {'dataset_type': 'CocoDataset', 'data_root': '/data/coco/', 'file_client_args': {'backend': 'disk'}}
>> MMDET_DATASET=/home/my_dataset/ python test.py
Config (path: demo_config.py): {'dataset_type': 'CocoDataset', 'data_root': '/home/my_dataset/', 'file_client_args': {'backend': 'disk'}}

Example 2

demo_config.py

model=dict(
    bbox_head=dict(
        num_classes={{'$NUM_CLASSES:80'}}))

The results of this pr are as below:

>> python test.py
Config (path: demo_config.py): {'model': {'bbox_head': {'num_classes': 80}}}
>> NUM_CLASSES=20 python test.py
Config (path: demo_config.py): {'model': {'bbox_head': {'num_classes': 20}}}

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

Copy link
Collaborator

@HAOCHENYE HAOCHENYE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update the config tutorial after the document refactoring. I'll make a notification when it is ready to update the config tutorial.

mmengine/config/config.py Outdated Show resolved Hide resolved
mmengine/config/config.py Outdated Show resolved Hide resolved
mmengine/config/config.py Show resolved Hide resolved
@HAOCHENYE HAOCHENYE added the enhancement New feature or request label Nov 20, 2022
@HAOCHENYE HAOCHENYE added this to the 0.5.0 milestone Nov 20, 2022
@codecov
Copy link

codecov bot commented Nov 27, 2022

Codecov Report

❗ No coverage uploaded for pull request base (main@2242fca). Click here to learn what that means.
Patch has no changes to coverable lines.

❗ Current head aa0355a differs from pull request most recent head 70b9754. Consider uploading reports for the commit 70b9754 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #744   +/-   ##
=======================================
  Coverage        ?   78.24%           
=======================================
  Files           ?      132           
  Lines           ?     9997           
  Branches        ?     1997           
=======================================
  Hits            ?     7822           
  Misses          ?     1837           
  Partials        ?      338           
Flag Coverage Δ
unittests 78.24% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@CLAassistant
Copy link

CLAassistant commented Dec 14, 2022

CLA assistant check
All committers have signed the CLA.

HAOCHENYE
HAOCHENYE previously approved these changes Dec 15, 2022
Copy link
Collaborator

@HAOCHENYE HAOCHENYE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved!!!
image

@jbwang1997
Copy link
Contributor Author

Do I need to update config tutorial now or waiting for document refactoring finished?

@HAOCHENYE
Copy link
Collaborator

Do I need to update config tutorial now or waiting for document refactoring finished?

Document refactoring has been finished! You can update the tutorial now 😄 ~

docs/en/advanced_tutorials/config.md Outdated Show resolved Hide resolved
docs/en/advanced_tutorials/config.md Show resolved Hide resolved
docs/zh_cn/advanced_tutorials/config.md Outdated Show resolved Hide resolved
@HAOCHENYE HAOCHENYE modified the milestones: 0.5.0, 0.6.0 Jan 16, 2023
C1rN09
C1rN09 previously approved these changes Jan 30, 2023
@zhouzaida zhouzaida merged commit c46f891 into open-mmlab:main Feb 1, 2023
@captainIT
Copy link

在 Windows 环境中 如果有个环境变量包含windows路径,如下:

import regex as re
regexp = '\\{\\{[\\\'\\"]?\\s*\\$WORK_DIR\\s*\\:\\s*None\\s*[\\\'\\"]?\\}\\}'
value = "D:\\666\\work_dir"
config_file='_work_dir = {{"$DLP_WORK_DIR:None"}}'
config_file = re.sub(regexp, value, config_file)

返回错误
regex._regex_core.error: bad escape \w at position 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awating-CI-pass enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants