reset_overall.py 3.7 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
3
4
5
6
7
8
9
10
11
from flask import Flask, render_template, request, redirect
import yaml
import ast

app = Flask(__name__)

key_type_map = {  
    'work_dir': str,
    'run_name': str,
    'project_cycle': int,
    'run_counter': int,
Wei Chengliang's avatar
Wei Chengliang committed
12
    'run_option': {
Wei Chengliang's avatar
Wei Chengliang committed
13
14
        'out_cat_only': bool,
    },
Wei Chengliang's avatar
Wei Chengliang committed
15
16
    'catalog_options': {
        'input_path': {
Wei Chengliang's avatar
Wei Chengliang committed
17
18
19
20
            'cat_dir': str,
            'star_cat': str,
            'galaxy_cat': str,
        },
Wei Chengliang's avatar
Wei Chengliang committed
21
        'SED_templates_path': {
Wei Chengliang's avatar
Wei Chengliang committed
22
23
24
25
26
27
28
29
30
31
            'star_SED': str,
            'galaxy_SED': str,
            'AGN_SED': str,
        },
        'star_only': bool, 
        'galaxy_only': bool,
        'rotateEll': float,
        'enable_mw_ext_gal': bool,
        'planck_ebv_map':str,
    },
Wei Chengliang's avatar
Wei Chengliang committed
32
    'obs_setting': {
Wei Chengliang's avatar
Wei Chengliang committed
33
34
35
36
37
38
39
40
        'pointing_file': str,
        'obs_config_file': str,
        'run_pointings': list,
        'enable_astrometric_model': bool,
        'cut_in_band': str,
        'mag_sat_margin': float,
        'mag_lim_margin': float,
    },
Wei Chengliang's avatar
Wei Chengliang committed
41
    'psf_setting': {
Wei Chengliang's avatar
Wei Chengliang committed
42
43
44
45
        'psf_model': str,
        'psf_pho_dir': str,
        'psf_sls_dir': str,
    },
Wei Chengliang's avatar
Wei Chengliang committed
46
    'shear_setting': {
Wei Chengliang's avatar
Wei Chengliang committed
47
48
49
50
        'shear_type': str,
        'reduced_g1': float,
        'reduced_g2': float,
    },
Wei Chengliang's avatar
Wei Chengliang committed
51
    'output_setting': {
Wei Chengliang's avatar
Wei Chengliang committed
52
53
54
55
        'output_format': str,
        'shutter_output': bool,
        'prnu_output': bool,
    },
Wei Chengliang's avatar
Wei Chengliang committed
56
    'random_seeds': {
Wei Chengliang's avatar
Wei Chengliang committed
57
58
59
60
61
62
63
64
65
66
67
68
        'seed_poisson': int,
        'seed_CR': int,
        'seed_flat': int,
        'seed_prnu': int,
        'seed_gainNonUniform': int,
        'seed_biasNonUniform': int,
        'seed_rnNonUniform': int,
        'seed_badcolumns': int,
        'seed_defective': int,
        'seed_readout': int,
    },
}
Wei Chengliang's avatar
Wei Chengliang committed
69
70


Wei Chengliang's avatar
Wei Chengliang committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def convert_dict_values(d, key_type_map):  
    for key, value in d.items():  
        if isinstance(value, dict):  
            convert_dict_values(value, key_type_map[key])  
        elif key in key_type_map:  
            if key_type_map[key] is int:
                d[key] = int(value)
            if key_type_map[key] is float:
                d[key] = float(value)
            if key_type_map[key] is bool:
                if d[key].lower() == 'yes' or d[key].lower() == 'true':
                    d[key] = True
                else:
                    d[key] = False
            if key_type_map[key] is str:
                d[key] = str(value)
            if key_type_map[key] is list:
                d[key] = ast.literal_eval(value)

Wei Chengliang's avatar
Wei Chengliang committed
90

Wei Chengliang's avatar
Wei Chengliang committed
91
92
93
94
def load_yaml():
    with open('templates/config_overall.yaml', 'r') as file:
        return yaml.safe_load(file)

Wei Chengliang's avatar
Wei Chengliang committed
95

Wei Chengliang's avatar
Wei Chengliang committed
96
97
98
99
100
def save_yaml(data):
    convert_dict_values(data, key_type_map) 
    with open('config_reset/config_overall_reset.yaml', 'w') as file:
        yaml.dump(data, file, default_flow_style=False, sort_keys=False)

Wei Chengliang's avatar
Wei Chengliang committed
101

Wei Chengliang's avatar
Wei Chengliang committed
102
103
104
105
106
107
108
109
110
111
112
def render_form(data, parent_key=''):
    form_html = ''
    for key, value in data.items():
        full_key = f"{parent_key}.{key}" if parent_key else key
        if isinstance(value, dict):  # 处理字典
            form_html += f"<div class='block'><h2>{key}</h2>{render_form(value, full_key)}</div>"
        else:
            form_html += f"<label for='{full_key}'>{key}:</label>"
            form_html += f"<input type='text' id='{full_key}' name='{full_key}' value='{value}'><br>"
    return form_html

Wei Chengliang's avatar
Wei Chengliang committed
113

Wei Chengliang's avatar
Wei Chengliang committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        data = load_yaml()
        for key in request.form:
            keys = key.split('.')
            temp = data
            for k in keys[:-1]:
                temp = temp[k]
            temp[keys[-1]] = request.form[key]
        save_yaml(data)
        return redirect('/')

    data = load_yaml()
    form_html = render_form(data)
    return render_template('index_overall.html', form_html=form_html)

Wei Chengliang's avatar
Wei Chengliang committed
131

Wei Chengliang's avatar
Wei Chengliang committed
132
133
if __name__ == '__main__':
    app.run(debug=True)