Model/backend
Khalim Conn-Kowlessar 3a21f22bb3 Slice S0380.164: Elmhurst-mirror §12.4.4 summer-immersion CO2/PE double-count
SAP 10.2 §12.4.4 (PDF p.36-37): "With open fire back boilers or closed
room heaters with boilers, an alternative system (electric immersion)
may be provided for heating water in summer. In that case water
heating is provided by the boiler for months October to May and by the
alternative system for months June to September."

The spec-literal CO2 / PE formula multiplies summer immersion fuel by
the Table 12d / 12e monthly cascade (per Table 12 footnotes (s) and
(t): "monthly factors in Table 12d/12e should be used in the SAP
worksheet"). The BRE-approved Elmhurst engine adds an extra
`summer_fuel × Table 12 annual electric` term ON TOP of the monthly
cascade for dual-rate tariffs — same Elmhurst-mirror shape as S0380.163
(§8.1) but additive rather than substitutive. Cost is computed
cleanly per spec — the double-count quirk only affects the (264) HW
CO2 and (278) HW PE factor lines.

Worksheet evidence (heating-systems corpus property 001431,
`solid fuel 2` — Table 4a code 158 closed-room-heater + back boiler,
65 % winter η + 100 % summer η, anthracite, 18-hour off-peak tariff):

  (62)m heat        303.12 .. 168.95 .. 175.91 .. 300.40 kWh
  winter fuel (W)   = 2205.80 / 0.65 = 3393.51 kWh anthracite
  summer fuel (S)   = 684.55 / 1.00  =  684.55 kWh immersion
  total fuel        = (219) = 4078.06 kWh

  (264) HW CO2  = 4078.06 × 0.3710 = 1513.15 kg/yr
    = W × 0.395 + S × (0.116 monthly_summer + 0.136 annual)
    = 1340.43 + 79.61 + 93.10 = 1513.14 ✓ within rounding

  (278) HW PE   = 4078.06 × 1.3771 = 5616.04 kWh/yr
    = W × 1.064 + S × (1.429 monthly_summer + 1.501 annual)
    = 3610.69 + 977.84 + 1027.51 = 5616.04 ✓ exact

The +annual term is precisely `S × Table 12 electric factor` and
matches the SF2 corpus pin's ΔCO2 = −93.10 and ΔPE = −1027.51 exactly.
Per [[feedback-software-no-special-handling]] mirror the engine.

Cascade rule (post-slice):

  STANDARD tariff       → winter × anth_annual + Σ wh_summer_m × Table 12d/e
                          (spec-literal, unchanged)
  7h / 10h / 18h / 24h → winter × anth_annual + Σ wh_summer_m × Table 12d/e
                          + S_fuel × Table 12 annual electric (Elmhurst mirror)

Closures `solid fuel 2`:
  ΔCO2 −93.10 → +0.0000 EXACT
  ΔPE  −1027.51 → +0.0000 EXACT
ΔSAP and Δcost remain EXACT (cascade cost path was already correct).

The 41-variant heating-systems corpus is now closed on its 25-variant
cascade-OK tier: all 25 SAP / cost / CO2 / PE EXACT (|Δ| < 1e-3) vs
the Elmhurst worksheet. Only `pcdb 1` carries a sub-tolerance gap
(−0.011 SAP / +5.7 PE — PCDB Eq D1 cascade gap on PCDF index 716, a
separate small slice).

⚠ Single-cert evidence

SF2 is the only §12.4.4 fixture in the corpus (`solid fuel 1` =
code 156 is an empty folder; no other variant exercises a back-boiler
combo with summer immersion). Per the handover ≥2-cert rule for new
§8 divergence rows, this slice was admitted under an explicit
exception: the divergence shares its shape with §8.1 (S0380.163's
Table 12 annual mirror for dual-rate HW), and the math matches the
worksheet to within rounding. The new §8.2 row is tagged with a
"⚠ Single-cert evidence" subsection so future agents know to revisit
if a second §12.4.4 cert worksheet ever diverges from this rule.

Tests:
  - test_section_12_4_4_hw_blend_mirrors_elmhurst_summer_annual_pe_co2_double_count
  - test_section_12_4_4_hw_blend_standard_tariff_keeps_spec_literal_monthly_cascade

909 pass / 0 fail; pyright net-zero 43 → 43.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-04 15:40:25 +00:00
..
address2UPRN more tests to ensure we don't deploy something that is brokern 2026-06-02 15:03:20 +00:00
addresses fixing broken unit tests 2026-05-01 08:46:17 +00:00
apis GoogleSolarApi translates BuildingInsightsNotFoundError to sentinel dict 🟩 2026-06-01 16:28:47 +00:00
app MCS cert identified by evidence_category in get_core_file_type 🟥 2026-06-04 15:40:25 +00:00
bulk_address2uprn_combiner sanitisation of postcode 2026-05-20 12:57:03 +00:00
categorisation fix dependency issue 2026-05-12 17:03:16 +00:00
condition smoke tests 2026-05-14 16:57:31 +00:00
diagnostics switched off hhrsh for comunity heating in place 2026-01-06 18:35:08 +00:00
docker more tests to ensure we don't deploy something that is brokern 2026-06-02 15:03:20 +00:00
documents_parser Slice S0380.164: Elmhurst-mirror §12.4.4 summer-immersion CO2/PE double-count 2026-06-04 15:40:25 +00:00
ecmk_fetcher more tests to ensure we don't deploy something that is brokern 2026-06-02 15:03:20 +00:00
engine Merge branch 'main' into feature/integrate_new_epc_with_historical_epc 2026-05-13 08:38:50 +00:00
epc_api adding hacky handling for matching on lmk key or uprn 2026-04-09 14:47:54 +01:00
etl fixed s3 location 2026-05-11 08:44:55 +00:00
export merged from main 2026-05-11 12:30:29 +00:00
magic_plan Store uploaded_file_id on magic_plan_plan row 🟩 2026-05-13 11:02:46 +00:00
ml_models Pulling out rebaseling predictions 2026-03-23 19:45:19 +00:00
onboarders testing rebaselining for Instagroup and changing multi glaze proportion for Partiy onboarder to 0-100 2026-03-24 18:21:19 +00:00
ordnanceSurvey tests files 2026-06-04 11:47:42 +00:00
pashub_fetcher All downloaded PasHub files uploaded to SharePoint property folder 🟩 2026-06-04 15:40:25 +00:00
postcode_splitter made everything complete not compelted 2026-04-21 20:37:34 +00:00
scripts added local devconaitner 2026-04-16 22:21:54 +00:00
tests pr review, move domain and orhcestration 2026-06-01 14:00:31 +00:00
utils removing redundant code 2026-05-13 08:40:51 +00:00
.env.example working on integrating new EPC api into address2UPRN 2026-04-27 11:32:44 +00:00
.env.test address final PR comments 2026-02-27 15:27:07 +00:00
__init__.py Trying to get /backend and /model_data working together 2023-07-18 16:45:11 +01:00
DbClient.py pulling together OS client and DBClient 2023-12-30 20:48:40 +00:00
Funding.py implemented some handling for mixed translation descriptions 2025-11-30 18:16:09 +00:00
OrdnanceSurvey.py deleted the wrong folder aded back the origional 2026-03-06 14:58:49 +00:00
Outputs.py rename Plan and Scenario to PlanModel and ScenarioModel 2026-02-12 12:01:39 +00:00
package-lock.json re-deploy route53 to use dev domain and updating github actions and sls 2023-07-17 14:14:02 +01:00
package.json re-deploy route53 to use dev domain and updating github actions and sls 2023-07-17 14:14:02 +01:00
Property.py added env impact scores to db 2026-04-09 16:31:50 +01:00
README.md empty commit 2026-03-06 10:58:20 +00:00
run_curl.sh add my code to main 2025-11-14 13:36:09 +00:00
run_local.sh implemented onboarding 2026-04-21 20:23:33 +00:00
SearchEpc.py adding hacky handling for matching on lmk key or uprn 2026-04-09 14:47:54 +01:00
test_event.json downgrade cryptography 2023-07-17 19:23:09 +01:00

Backend

This is the api service that will supply the frontend with the insights that are driven by the machine learning and data modelling services.

Usage

Prerequisites

Python 3.8+ Poetry for managing project dependencies and virtual environment.

Installation and setup

  1. Clone this directory and navigate into the project directory.
git clone https://github.com/Hestia-Homes/Model.git
cd backend
  1. For environment management, I'm using conda with pycharm which is a convenient setup for development on a mac M1 however using tools such as poetry or pipenv is also fine.

For example, to install conda and create a virtual environment for this project, run the following commands:

conda create -n backend python=3.10
conda activate backend

then enter the virtual environment and install the dependencies using conda.

conda install --file requirements/base.txt
  1. Duplicate .env.example and rename it to .env
cp .env.example .env
  1. Open .env and fill in the required environment variables.

Running the Application

from model/backend/ you can run with the following command:

uvicorn app.main:app --reload

Or run sh run_local.sh, which runs that same uvicorn command.

You application will be available at the designated url

API Documentation

FastAPI automatically generates interactive API documentation for your application. To access the docs, start your server and visit /docs in your browser. Alternatively, you can go to /redoc to view the documentation in the ReDoc format.

Building the lambda's backend docker image locally

To build the backend docker image locally, run the following command from the root of the project directory:

docker build -t fastapi-lambda-image:latest -f backend/docker/lambda.Dockerfile .

To check the size of the resulting image, run the following command:

docker images | grep fastapi-lambda-image

To run a shell inside the Docker container to inspect its contents, run:

docker run -it fastapi-lambda-image:latest /bin/bash

Running in lambda results in running in a slightly different format compared to running the fastapi application locally. If you want to run the fastapi application locally, in docker, we have a docker file which builds the same environment as in lambda but runs the fast api application with uvicorn.

Run

docker build -t fastapi-local-image:latest -f backend/docker/Dockerfile .

This will be the image. To run it, simply run

docker run -p 8000:8000 -v ~/.aws:/root/.aws fastapi-local-image:latest

This assumes you have a ~/.aws folder with your aws credentials in it. If you don't have this, you can run the following command with your aws access token exported into your environment.

docker run -p 8000:8000 -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION fastapi-local-image:latest

Emulating the lambda locally

I have set up a script called run_local_lambda.sh which will allow you to emulate the lambda locally. You need to have a .env file with the necessary environment variables at backend/env and also and aws credentials file at ~/.aws/credentials, locally.

To run this, firstly run:

chmod +x run_lambda_local.sh

Now you can run the script with

./run_lambda_local.sh

In order to make a request to it, there is a specific format the request must be in, to emuate lambda. If using postman, the url you want is http://localhost:8000/2015-03-31/functions/function/invocations and you need to pass a body like this:

{
  "httpMethod": "POST",
  "body": "{\"portfolio_id\": 4, \"housing_type\": \"Private\", \"goal\": \"Increase EPC\", \"goal_value\": \"C\", \"trigger_file_path\": \"2/4/portfolio_plan_properties-20230724T093542483Z.csv\"}",
  "path": "/v1/plan/trigger",
  "resource": "/",
  "headers": {
    "Accept": "*/*",
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_TOKEN_HERE",
    "x-api-key": "YOUR_API_KEY_HERE"
  },
  "requestContext": {},
  "multiValueQueryStringParameters": null
}

Logs for the container can quickly be seen via Docker desktop

Testing

To run tests, run the following command from the root of the project directory:

pytest

Local Development

During local development, you may need to generate and use a dummy JWT to test protected endpoints of the application.

Generating a Dummy JWT

FastAPI provides a convenient way to generate a dummy JWT for testing. To generate a dummy JWT, follow the steps below:

Make sure your application is running in a local environment. The dummy token endpoint is only available in a local environment.

While your application is running, visit the /dummy-token endpoint using a tool like curl or any HTTP client like Postman.

For instance, if your server is running locally on port 8000, you can use curl to get a dummy token:

curl http://localhost:8000/local/dummy-token

You will receive a response containing the dummy JWT

{
  "dummy_token": "<Your Dummy Token>"
}

Using the Dummy JWT

Once you've obtained a dummy JWT, you can use it to make requests to protected endpoints in your application:

  1. When making a request, include an Authorization header with the value Bearer . Replace with the token you received from the /dummy-token endpoint.

  2. Now you can make requests to the protected endpoints of the application.

Remember, the dummy JWT is meant for testing purposes only and should not be used in production environments. The /dummy-token endpoint is not available in non-local environments.

Custom Domain Setup for AWS API Gateway

Before you deploy your Serverless application for the first time, you need to set up a custom domain for AWS API Gateway. This is done using the sls create_domain command, which creates a custom domain in API Gateway that your services can use.

To set up a custom domain, use the following command:

sls create_domain --stage dev --aws-profile DevAdmin --verbose

Replace dev with the name of the stage you're deploying to. This command only needs to be run once per custom domain, and not every time you deploy your application. After running this command, you can associate your AWS Lambda functions with this domain using the customDomain configuration in your serverless.yml file.

This command requires the Serverless Domain Manager plugin, so make sure you have it installed and properly configured in your serverless.yml file.

Please note that the process of creating and associating a custom domain can take up to 40 minutes. Once the custom domain is created, it's immediately available for use in your Serverless applications.

Remember to replace DevAdmin with the profile that has appropriate permissions in your AWS account. The --verbose flag is optional and is used to print detailed logs to the console.

Creating a CNAME Record in Google Domains

After deploying the AWS Lambda function for the first time, you need to set up a CNAME record in Google Domains to route traffic from your custom domain to the CloudFront distribution created by API Gateway. This will re-route traffic from your custom domain to the CloudFront distribution created by API Gateway, and therefore to your lambda. See here for AWS' documentation on this.

You can find the CloudFront domain by going to the API Gateway console and clicking on Custom Domain Names.

Here are the steps to create a CNAME record:

  1. Log in to Google Domains.
  2. Select the name of your domain.
  3. Open the menu, if it's not already open.
  4. Click "DNS."
  5. Scroll down to the "Custom resource records" section.
  6. In the "Name" field, enter your subdomain (e.g., api if your API is available at api.example.com).
  7. In the "Type" dropdown menu, select "CNAME."
  8. In the "TTL" field, enter 1H to set it to 1 hour (or another suitable value).
  9. In the "Data" field, enter the CloudFront domain that was created by API Gateway (you can find this in the API Gateway console, under Custom Domain Names).
  10. Click "Add."

This will direct any traffic from your custom domain to your AWS CloudFront distribution. Please note that DNS changes might take some time (up to 24-48 hours in some cases) to propagate across the internet.

Also, please make sure that your CloudFront distribution is configured to accept your custom domain as a valid domain name. In AWS API Gateway, under Custom Domain Names, make sure that your custom domain is listed and mapped to the appropriate API stage.

Remember to replace api and the CloudFront domain with your actual subdomain and CloudFront domain.

Certainly! Here's a detailed documentation for your README:


Deployment Troubleshooting for fastapi-lambda

Context:

When deploying the fastapi-lambda using Serverless Framework, you may encounter issues related to domain management, especially if you're using a custom domain for your API. This documentation provides troubleshooting steps and details on how to resolve potential conflicts.

Potential Issues & Solutions:

1. Conflict with Existing CloudFront Distribution:

Error Message:

csharpCopy code

One or more aliases specified for the distribution includes an incorrectly configured DNS record that points to another CloudFront distribution.

Cause: This can occur if there's an existing CNAME record in your DNS provider pointing to a CloudFront distribution.

Solution:

  • Check your DNS provider (e.g., Google Domains) and verify the CNAME record for api.dev.hestia.homes.
  • Temporarily remove or update the conflicting CNAME record.
  • Run the sls create_domain command again.
  • Update the DNS settings in your DNS provider based on the new configuration provided by the serverless-domain-manager plugin.

2. Conflict with Route53:

Error Message:

csharpCopy code

Deleting RestApi failed. Please remove all base path mappings related to the RestApi in your domains.

Cause: This can occur if there are residual AWS configurations, especially in Route53, from previous deployments.

Solution:

  • Navigate to the AWS Route53 Console.
  • Identify and delete any residual Hosted Zones or Record Sets related to api.dev.hestia.homes.
  • Ensure that you have backed up any necessary configurations before deleting.

3. Other AWS Resources Conflicts:

You might encounter issues where AWS resources, such as S3 buckets or CloudFront distributions, are not properly deleted or are conflicting with new deployments.

Solution:

  • Navigate to the respective AWS service dashboard.
  • Manually identify and rectify any conflicting resources. This might involve emptying S3 buckets or deleting CloudFront distributions.
  • Ensure backups and proper precautions before deleting any resources.

Additional Notes:

  • Backup Configurations: Always backup your configurations before making changes. This ensures that you can revert to a previous state if needed.
  • DNS Propagation: Remember that DNS changes can take some time to propagate globally. After making DNS changes, you might not see immediate effects.
  • CloudFront Distributions: If you can't find a CloudFront distribution in the AWS CloudFront console, it's possible that it was automatically created by another AWS service like API Gateway. It might need to be managed or deleted from that service's dashboard.

After succesfully running creating the custom domain

After successfully creating the custom domain with the serverless-domain-manager plugin, you should add back the CNAME record into Google Domains (or whatever platform is being used to manage domains now) to ensure that the custom domain properly points to the CloudFront distribution managed by AWS.

Here's what you should do:

  1. Log in to Google Domains:

    • Go to Google Domains.
    • Navigate to the management page for hestia.homes.
  2. Add/Update the CNAME Record:

    • Find the section for custom resource records.
    • Add (or update if it already exists) a CNAME record for api.dev.
    • Point it to the CloudFront distribution domain name (e.g., d2d269kjy1nyhz.cloudfront.net.). Ensure you include the trailing dot at the end. This can be found in API gateway
  3. Check DNS Propagation:

    • Keep in mind that DNS changes might take some time to propagate. You can use online tools like DNS Checker to verify the propagation status worldwide.
    • Test your API endpoint api.dev.hestia.homes to ensure it's resolving correctly and accessing your Lambda function.

By following these steps, you should have your custom domain properly configured and pointing to your AWS Lambda function via the CloudFront distribution