# SPDX-FileCopyrightText: PyPSA-Earth and PyPSA-Eur Authors # # SPDX-License-Identifier: AGPL-3.0-or-later name: Update reference objectives on: workflow_dispatch: jobs: run-tests-and-extract: name: Run tests and extract objectives runs-on: ubuntu-latest permissions: contents: read defaults: run: shell: bash -l {0} steps: - uses: actions/checkout@v7 - uses: conda-incubator/setup-miniconda@v4 with: miniforge-version: latest activate-environment: pypsa-earth channel-priority: strict conda-remove-defaults: true - name: Cache Conda env id: cache-env uses: actions/cache@v6 with: path: ${{ env.CONDA }}/envs key: conda-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('envs/linux-64.lock.yaml') }} - name: Update environment if: steps.cache-env.outputs.cache-hit != 'true' run: conda env update -n pypsa-earth -f envs/linux-64.lock.yaml - name: Run tests run: make test - name: Extract objectives and build updated reference CSV run: | # Collect log files where to search for objective values shopt -s nullglob LOG_FILES=(./logs/solve_network/*_python.log ./logs/*/solve_network/*_python.log) OBJ_FOUND=$( awk '/INFO:__main__:Objective function: /{ sub(/\r$/, ""); sub(/.*INFO:__main__:Objective function: /, ""); print FILENAME ":" $0 }' "${LOG_FILES[@]}" || true ) shopt -u nullglob if [ -z "$OBJ_FOUND" ]; then echo "Objective not found." exit 1 fi # Add REUSE headers to the output file # REUSE-IgnoreStart { printf "# -*- coding: utf-8 -*-\n" printf "# SPDX-FileCopyrightText: PyPSA-Earth and PyPSA-Eur Authors\n" printf "#\n" printf "# SPDX-License-Identifier: CC-BY-4.0\n" } > updated_obj_ref.csv # REUSE-IgnoreEnd # Add the CSV header to the output file printf "folder,file,objective\n" >> updated_obj_ref.csv # Loop over log files, extract objective values, and write value to CSV while IFS= read -r line; do FILE=$(echo "$line" | cut -d: -f1) VALUE=$(echo "$line" | cut -d: -f2-) BASENAME=$(basename "$FILE") FOLDER=$(echo "$FILE" | sed -E 's|.*/logs/([^/]+)/solve_network/.*|\1|') if [[ "$FILE" == ./logs/solve_network/* ]]; then FOLDER="default" fi VALUE_RAW=$(awk -v v="$VALUE" 'BEGIN { printf "%.2f\n", v }') printf "%s,%s,%s\n" "$FOLDER" "$BASENAME" "$VALUE_RAW" >> updated_obj_ref.csv done <<< "$OBJ_FOUND" - name: Upload updated reference CSV uses: actions/upload-artifact@v7 with: name: updated-ref-objectives path: updated_obj_ref.csv create-pull-request: name: Create PR with updated reference objectives needs: run-tests-and-extract runs-on: ubuntu-latest permissions: contents: write pull-requests: write steps: - uses: actions/checkout@v7 - name: Download updated reference CSV uses: actions/download-artifact@v8 with: name: updated-ref-objectives - name: Move CSV to correct location run: mv updated_obj_ref.csv test/utils/obj_ref.csv - name: Create Pull Request uses: peter-evans/create-pull-request@v8 with: token: ${{ secrets.GITHUB_TOKEN }} branch: update-reference-objectives title: "[github-actions.ci] Update reference objectives" body: | Automatically generated PR to update reference objective values in `test/utils/obj_ref.csv`. These values were extracted from a manual dispatch CI run triggered by @${{ github.actor }}. commit-message: "Update reference objective values"