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