Conditions | 3 |
Total Lines | 253 |
Code Lines | 139 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from datetime import datetime |
||
432 | def select(): |
||
433 | """ |
||
434 | |||
435 | Random assignment of intray-day profiles to each day based on their temeprature class |
||
436 | and household stock count |
||
437 | |||
438 | Returns |
||
439 | ------- |
||
440 | None. |
||
441 | |||
442 | """ |
||
443 | start_profile_selector = datetime.now() |
||
444 | |||
445 | # Drop old table and re-create it |
||
446 | engine = db.engine() |
||
447 | EgonHeatTimeseries.__table__.drop(bind=engine, checkfirst=True) |
||
448 | EgonHeatTimeseries.__table__.create(bind=engine, checkfirst=True) |
||
449 | |||
450 | # Select all intra-day-profiles |
||
451 | idp_df = db.select_dataframe( |
||
452 | """ |
||
453 | SELECT index, house, temperature_class |
||
454 | FROM demand.egon_heat_idp_pool |
||
455 | """, |
||
456 | index_col="index", |
||
457 | ) |
||
458 | |||
459 | # Select daily heat demand shares per climate zone from table |
||
460 | temperature_classes = db.select_dataframe( |
||
461 | """ |
||
462 | SELECT climate_zone, day_of_year, temperature_class |
||
463 | FROM demand.egon_daily_heat_demand_per_climate_zone |
||
464 | """ |
||
465 | ) |
||
466 | |||
467 | # Calculate annual heat demand per census cell |
||
468 | annual_demand = annual_demand_generator( |
||
469 | scenario=egon.data.config.settings()["egon-data"]["--scenarios"][0] |
||
470 | ) |
||
471 | |||
472 | # Count number of SFH and MFH per climate zone |
||
473 | houses_per_climate_zone = ( |
||
474 | annual_demand.groupby("climate_zone")[["SFH", "MFH"]].sum().astype(int) |
||
475 | ) |
||
476 | |||
477 | # Set random seed to make code reproducable |
||
478 | np.random.seed( |
||
479 | seed=egon.data.config.settings()["egon-data"]["--random-seed"] |
||
480 | ) |
||
481 | |||
482 | for station in houses_per_climate_zone.index: |
||
483 | result_SFH = pd.DataFrame(columns=range(1, 366)) |
||
484 | result_MFH = pd.DataFrame(columns=range(1, 366)) |
||
485 | |||
486 | # Randomly select individual daily demand profile for selected climate zone |
||
487 | for day in range(1, 366): |
||
488 | t_class = temperature_classes.loc[ |
||
489 | (temperature_classes.climate_zone == station) |
||
490 | & (temperature_classes.day_of_year == day), |
||
491 | "temperature_class", |
||
492 | ].values[0] |
||
493 | |||
494 | result_SFH[day] = np.random.choice( |
||
495 | np.array( |
||
496 | idp_df[ |
||
497 | (idp_df.temperature_class == t_class) |
||
498 | & (idp_df.house == "SFH") |
||
499 | ].index.values |
||
500 | ), |
||
501 | houses_per_climate_zone.loc[station, "SFH"], |
||
502 | ) |
||
503 | |||
504 | result_MFH[day] = np.random.choice( |
||
505 | np.array( |
||
506 | idp_df[ |
||
507 | (idp_df.temperature_class == t_class) |
||
508 | & (idp_df.house == "MFH") |
||
509 | ].index.values |
||
510 | ), |
||
511 | houses_per_climate_zone.loc[station, "MFH"], |
||
512 | ) |
||
513 | |||
514 | result_SFH["zensus_population_id"] = ( |
||
515 | annual_demand[annual_demand.climate_zone == station] |
||
516 | .loc[ |
||
517 | annual_demand[ |
||
518 | annual_demand.climate_zone == station |
||
519 | ].index.repeat( |
||
520 | annual_demand[ |
||
521 | annual_demand.climate_zone == station |
||
522 | ].SFH.astype(int) |
||
523 | ) |
||
524 | ] |
||
525 | .index.values |
||
526 | ) |
||
527 | |||
528 | result_SFH["building_id"] = ( |
||
529 | db.select_dataframe( |
||
530 | """ |
||
531 | |||
532 | SELECT cell_id as zensus_population_id, building_id FROM |
||
533 | ( |
||
534 | SELECT cell_id, COUNT(*), building_id |
||
535 | FROM demand.egon_household_electricity_profile_of_buildings |
||
536 | GROUP BY (cell_id, building_id) |
||
537 | ) a |
||
538 | WHERE a.count = 1 |
||
539 | """, |
||
540 | index_col="zensus_population_id", |
||
541 | ) |
||
542 | .loc[result_SFH["zensus_population_id"].unique(), "building_id"] |
||
543 | .values |
||
544 | ) |
||
545 | |||
546 | result_MFH["zensus_population_id"] = ( |
||
547 | annual_demand[annual_demand.climate_zone == station] |
||
548 | .loc[ |
||
549 | annual_demand[ |
||
550 | annual_demand.climate_zone == station |
||
551 | ].index.repeat( |
||
552 | annual_demand[ |
||
553 | annual_demand.climate_zone == station |
||
554 | ].MFH.astype(int) |
||
555 | ) |
||
556 | ] |
||
557 | .index.values |
||
558 | ) |
||
559 | |||
560 | result_MFH["building_id"] = ( |
||
561 | db.select_dataframe( |
||
562 | """ |
||
563 | |||
564 | SELECT cell_id as zensus_population_id, building_id FROM |
||
565 | ( |
||
566 | SELECT cell_id, COUNT(*), building_id |
||
567 | FROM demand.egon_household_electricity_profile_of_buildings |
||
568 | GROUP BY (cell_id, building_id) |
||
569 | ) a |
||
570 | WHERE a.count > 1 |
||
571 | """, |
||
572 | index_col="zensus_population_id", |
||
573 | ) |
||
574 | .loc[result_MFH["zensus_population_id"].unique(), "building_id"] |
||
575 | .values |
||
576 | ) |
||
577 | |||
578 | df_sfh = pd.DataFrame( |
||
579 | data={ |
||
580 | "selected_idp_profiles": result_SFH[ |
||
581 | range(1, 366) |
||
582 | ].values.tolist(), |
||
583 | "zensus_population_id": ( |
||
584 | annual_demand[annual_demand.climate_zone == station] |
||
585 | .loc[ |
||
586 | annual_demand[ |
||
587 | annual_demand.climate_zone == station |
||
588 | ].index.repeat( |
||
589 | annual_demand[ |
||
590 | annual_demand.climate_zone == station |
||
591 | ].SFH.astype(int) |
||
592 | ) |
||
593 | ] |
||
594 | .index.values |
||
595 | ), |
||
596 | "building_id": ( |
||
597 | db.select_dataframe( |
||
598 | """ |
||
599 | |||
600 | SELECT cell_id as zensus_population_id, building_id FROM |
||
601 | ( |
||
602 | SELECT cell_id, COUNT(*), building_id |
||
603 | FROM demand.egon_household_electricity_profile_of_buildings |
||
604 | GROUP BY (cell_id, building_id) |
||
605 | ) a |
||
606 | WHERE a.count = 1 |
||
607 | """, |
||
608 | index_col="zensus_population_id", |
||
609 | ) |
||
610 | .loc[ |
||
611 | result_SFH["zensus_population_id"].unique(), |
||
612 | "building_id", |
||
613 | ] |
||
614 | .values |
||
615 | ), |
||
616 | } |
||
617 | ) |
||
618 | start_sfh = datetime.now() |
||
619 | df_sfh.set_index(["zensus_population_id", "building_id"]).to_sql( |
||
620 | EgonHeatTimeseries.__table__.name, |
||
621 | schema=EgonHeatTimeseries.__table__.schema, |
||
622 | con=db.engine(), |
||
623 | if_exists="append", |
||
624 | chunksize=5000, |
||
625 | method="multi", |
||
626 | ) |
||
627 | print(f"SFH insertation for zone {station}:") |
||
628 | print(datetime.now() - start_sfh) |
||
629 | |||
630 | df_mfh = pd.DataFrame( |
||
631 | data={ |
||
632 | "selected_idp_profiles": result_MFH[ |
||
633 | range(1, 366) |
||
634 | ].values.tolist(), |
||
635 | "zensus_population_id": ( |
||
636 | annual_demand[annual_demand.climate_zone == station] |
||
637 | .loc[ |
||
638 | annual_demand[ |
||
639 | annual_demand.climate_zone == station |
||
640 | ].index.repeat( |
||
641 | annual_demand[ |
||
642 | annual_demand.climate_zone == station |
||
643 | ].MFH.astype(int) |
||
644 | ) |
||
645 | ] |
||
646 | .index.values |
||
647 | ), |
||
648 | "building_id": ( |
||
649 | db.select_dataframe( |
||
650 | """ |
||
651 | |||
652 | SELECT cell_id as zensus_population_id, building_id FROM |
||
653 | ( |
||
654 | SELECT cell_id, COUNT(*), building_id |
||
655 | FROM demand.egon_household_electricity_profile_of_buildings |
||
656 | GROUP BY (cell_id, building_id) |
||
657 | ) a |
||
658 | WHERE a.count > 1 |
||
659 | """, |
||
660 | index_col="zensus_population_id", |
||
661 | ) |
||
662 | .loc[ |
||
663 | result_MFH["zensus_population_id"].unique(), |
||
664 | "building_id", |
||
665 | ] |
||
666 | .values |
||
667 | ), |
||
668 | } |
||
669 | ) |
||
670 | |||
671 | start_mfh = datetime.now() |
||
672 | df_mfh.set_index(["zensus_population_id", "building_id"]).to_sql( |
||
673 | EgonHeatTimeseries.__table__.name, |
||
674 | schema=EgonHeatTimeseries.__table__.schema, |
||
675 | con=db.engine(), |
||
676 | if_exists="append", |
||
677 | chunksize=5000, |
||
678 | method="multi", |
||
679 | ) |
||
680 | print(f"MFH insertation for zone {station}:") |
||
681 | print(datetime.now() - start_mfh) |
||
682 | |||
683 | print("Time for overall profile selection:") |
||
684 | print(datetime.now() - start_profile_selector) |
||
685 |