You are an expert in python and using the folium library with streamlit.
Focus only on this code
Run Optuna study
if not st.session_state.study_running:
if st.button('Start Optuna Study'):
st.session_state.study_running = True
with st.spinner('Running Optuna Study...'):
study = optuna.create_study(direction='minimize')
for _ in range(n_trials):
st.write(f"Currently testing trial number: {st.session_state.current_trial_number+1}")
trial = study.ask()
loss = objective(trial, poly=poly, city_grid=city_grid)
study.tell(trial, loss)
code
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
if st.session_state.best_map_data:
map_center, radius, final_contour = st.session_state.best_map_data
m = generate_map(map_center, radius, final_contour)
map_placeholder.empty()
folium_static(m)
st.session_state.study_running = False
#Display best trial info
best_trial = study.best_trial
st.write("Best trial loss:", best_trial.value)
st.write("Best trial params:", best_trial.params)
Abort button to stop the study
if st.session_state.study_running:
if st.button('Abort Study'):
st.session_state.study_running = False
st.warning('Optuna Study Aborted')
Fix two things:
The map is not updated with best_map_data but instead a new map is appended
Once we run the Optuna study, the Abort button is not displayed.
Remember you know folium and streamlit api very well
Assistant
To fix the issues mentioned, you can make the following changes to the code:
Update the map with the best_map_data:
Remove the line map_placeholder.empty() as it is not necessary.
Instead of creating a new map every time, you can update the existing map using m = generate_map(map_center, radius, final_contour, m).
Display the Abort button when the study is running:
Surround the code block for displaying the Abort button with an if condition to check if the study is running.
Here is the updated code:
python
# Run Optuna study
if not st.session_state.study_running:
if st.button('Start Optuna Study'):
st.session_state.study_running = True
with st.spinner('Running Optuna Study...'):
study = optuna.create_study(direction='minimize')
for _ in range(n_trials):
st.write(f"Currently testing trial number: {st.session_state.current_trial_number+1}")
trial = study.ask()
loss = objective(trial, poly=poly, city_grid=city_grid)
study.tell(trial, loss)
if st.session_state.best_map_data:
map_center, radius, final_contour = st.session_state.best_map_data
m = generate_map(map_center, radius, final_contour, m)
folium_static(m)
st.session_state.study_running = False
# Display best trial info
best_trial = study.best_trial
st.write("Best trial loss:", best_trial.value)
st.write("Best trial params:", best_trial.params)
# Abort button to stop the study
if st.session_state.study_running:
if st.button('Abort Study'):
st.session_state.study_running = False
st.warning('Optuna Study Aborted')
With these changes, the map will be updated with the best_map_data instead of appending a new map, and the Abort button will be displayed when the study is running.