Skip to content

Commit

Permalink
feat: get settings from .config file
Browse files Browse the repository at this point in the history
  • Loading branch information
senavs committed Sep 25, 2021
1 parent e63c9c7 commit ab12ffb
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 116 deletions.
37 changes: 37 additions & 0 deletions .config
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SUBJECT
MIN_AGE=5
MAX_AGE=90


# PATHOLOGY
PROB_INFECTION=0.15
PROB_DEATH=0.03
EXPOSED_MAX_DAY=7
INFECTIOUS_MAX_DAY=14


# PREVENTION MASK
MASK_PROB_INFECTION=0.2
MASK_PROB_DEATH=1
MASK_PERC_ACTIVATION=0.4

# PREVENTION ISOLATION
ISOLATION_PROB_INFECTION=0.5
ISOLATION_PROB_DEATH=1
ISOLATION_PERC_ACTIVATION=0.2

# PREVENTION VACCINE
VACCINE_PROB_INFECTION=1
VACCINE_PROB_DEATH=0.05
VACCINE_PERC_ACTIVATION=0.6


# BOARD
DIMENSION=51
CELL_SICK_LOCATION=25


# REPORT
OUTPUT_BASE_DIR='./'
OUTPUT_BOARD_RESOLUTION=800
OUTPUT_BOARD_DPI=96
Empty file removed application/__init__.py
Empty file.
40 changes: 0 additions & 40 deletions application/app.py

This file was deleted.

5 changes: 1 addition & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ pyparsing==2.4.7
python-dateutil==2.8.2
pytz==2021.1
six==1.16.0
fastapi==0.68.1
imageio==2.9.0
uvicorn==0.15.0
starlette==0.14.2
pydantic==1.8.2
pydantic[dotenv]==1.8.2
8 changes: 4 additions & 4 deletions simulation/automatos/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from simulation.automatos.cell import Cell
from simulation.core.subject import Subject
from simulation.core.pathology import ConcretePathology
from simulation.settings import BoardSettings
from simulation.settings import board_settings


class Board:

def __init__(self):
self.board = self.initialize_board()
self.n_cells = BoardSettings.DIMENSION * BoardSettings.DIMENSION
self.n_cells = board_settings.DIMENSION * board_settings.DIMENSION

@property
def dimension(self) -> tuple[int, int]:
selected_dimension = BoardSettings.DIMENSION
selected_dimension = board_settings.DIMENSION
dimension = selected_dimension if selected_dimension % 2 == 1 else selected_dimension + 1
return dimension, dimension

Expand All @@ -27,7 +27,7 @@ def initialize_board(self) -> np.ndarray:
for j in range(y):
board[i, j] = Cell(i, j, Subject())

x, y = BoardSettings.CELL_SICK_POSITION
x, y = board_settings.CELL_SICK_POSITION
board[x, y] = Cell(x, y, Subject.create_sick_subject())

return board
Expand Down
12 changes: 5 additions & 7 deletions simulation/automatos/progress.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from typing import Type

from simulation.automatos.board import Board
from simulation.core.subject import Subject
from simulation.report.printer import Printer
from simulation.core.prevention import SocialIsolation, Mask, Vaccine, Prevention, PreventionEnum
from simulation.core.prevention import SocialIsolation, Mask, Vaccine, PreventionEnum
from simulation.report.reporter import Reporter
from simulation.settings import PreventionSettings
from simulation.settings import prevention_settings

PREVS = {
'mask': [Mask, PreventionSettings.MASK_PERC_ACTIVATION],
'isolation': [SocialIsolation, PreventionSettings.ISOLATION_PERC_ACTIVATION],
'vaccine': [Vaccine, PreventionSettings.VACCINE_PERC_ACTIVATION],
'mask': [Mask, prevention_settings.MASK_PERC_ACTIVATION],
'isolation': [SocialIsolation, prevention_settings.ISOLATION_PERC_ACTIVATION],
'vaccine': [Vaccine, prevention_settings.VACCINE_PERC_ACTIVATION],
}


Expand Down
12 changes: 6 additions & 6 deletions simulation/core/pathology.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

from simulation.settings import SubjectSettings, PathologySettings
from simulation.settings import subject_settings, pathology_settings
from simulation.core.condition import Condition

if TYPE_CHECKING:
Expand All @@ -15,10 +15,10 @@ def __init__(self, subject: 'Subject'):
self.infected_days: int = 0
self.subject = subject

self.prob_infection: float = PathologySettings.PROB_INFECTION
self.prob_death: float = PathologySettings.PROB_DEATH
self.exposed_max_day: float = PathologySettings.EXPOSED_MAX_DAY
self.infectious_max_day: float = PathologySettings.INFECTIOUS_MAX_DAY
self.prob_infection: float = pathology_settings.PROB_INFECTION
self.prob_death: float = pathology_settings.PROB_DEATH
self.exposed_max_day: float = pathology_settings.EXPOSED_MAX_DAY
self.infectious_max_day: float = pathology_settings.INFECTIOUS_MAX_DAY

@abstractmethod
def infect(self, subject: 'Subject') -> bool:
Expand Down Expand Up @@ -81,7 +81,7 @@ def should_kill(self) -> bool:
if self.subject.condition not in (Condition.EXPOSED, Condition.INFECTIOUS):
return False

prob = (self.prob_death * (self.subject.age / SubjectSettings.MAX_AGE)) / self.subject.healthy_lifestyle * self.subject.preventions.get_prob_death()
prob = (self.prob_death * (self.subject.age / subject_settings.MAX_AGE)) / self.subject.healthy_lifestyle * self.subject.preventions.get_prob_death()
return random.random() <= prob

def evolve(self):
Expand Down
14 changes: 7 additions & 7 deletions simulation/core/prevention.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from operator import mul
from typing import TYPE_CHECKING, Type

from simulation.settings import PreventionSettings
from simulation.settings import prevention_settings

if TYPE_CHECKING:
from simulation.core.subject import Subject
Expand Down Expand Up @@ -66,8 +66,8 @@ class SocialIsolation(Prevention):
def __init__(self, subject: 'Subject'):
super().__init__(
subject,
PreventionSettings.ISOLATION_PROB_INFECTION,
PreventionSettings.ISOLATION_PROB_DEATH
prevention_settings.ISOLATION_PROB_INFECTION,
prevention_settings.ISOLATION_PROB_DEATH
)

def evolve(self):
Expand All @@ -79,8 +79,8 @@ class Mask(Prevention):
def __init__(self, subject: 'Subject'):
super().__init__(
subject,
PreventionSettings.MASK_PROB_INFECTION,
PreventionSettings.MASK_PROB_DEATH
prevention_settings.MASK_PROB_INFECTION,
prevention_settings.MASK_PROB_DEATH
)

def evolve(self):
Expand All @@ -92,8 +92,8 @@ class Vaccine(Prevention):
def __init__(self, subject: 'Subject'):
super().__init__(
subject,
PreventionSettings.VACCINE_PROB_INFECTION,
PreventionSettings.VACCINE_PROB_DEATH
prevention_settings.VACCINE_PROB_INFECTION,
prevention_settings.VACCINE_PROB_DEATH
)

def evolve(self):
Expand Down
6 changes: 3 additions & 3 deletions simulation/core/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from simulation.core.pathology import ConcretePathology, Pathology, NullPathology
from simulation.core.condition import Condition
from simulation.core.prevention import PreventionGroup, SocialIsolation, Mask, Vaccine, Prevention
from simulation.settings import SubjectSettings
from simulation.core.prevention import PreventionGroup, Prevention
from simulation.settings import subject_settings


class Subject:
Expand All @@ -18,7 +18,7 @@ def __new__(cls):

def __init__(self):
self.id: int = next(self._icounter)
self.age: int = random.randint(SubjectSettings.MIN_AGE, SubjectSettings.MAX_AGE)
self.age: int = random.randint(subject_settings.MIN_AGE, subject_settings.MAX_AGE)
self.condition: Condition = Condition.NORMAL
self.pathology: Pathology = NullPathology(self)
self.preventions: PreventionGroup = PreventionGroup(self, *self._preventions)
Expand Down
14 changes: 7 additions & 7 deletions simulation/report/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from matplotlib.figure import Figure

from simulation.automatos.board import Board
from simulation.settings import ReportSettings
from simulation.settings import report_settings


class Printer:
Expand All @@ -17,8 +17,8 @@ def __init__(self):

@staticmethod
def initialize_figure() -> tuple[Figure, SubplotBase]:
width, height = ReportSettings.OUTPUT_BOARD_RESOLUTION
dpi = ReportSettings.OUTPUT_BOARD_DPI
width, height = report_settings.OUTPUT_BOARD_DIMENSION
dpi = report_settings.OUTPUT_BOARD_DPI

figure = plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi)
axis = plt.subplot(1, 1, 1)
Expand All @@ -43,13 +43,13 @@ def draw(self, board: Board, file_name: str):
draw_board = np.array(func(board.board).tolist(), dtype=np.uint8)
image = self.imshow(draw_board)

self.figure.savefig(f'{ReportSettings.OUTPUT_BOARD_DIR}/{file_name}.png', bbox_inches='tight')
self.figure.savefig(f'{report_settings.OUTPUT_BOARD_DIR}/{file_name}.png', bbox_inches='tight')

return image

@classmethod
def make_gif(cls):
images = []
for filename in sorted(os.listdir(ReportSettings.OUTPUT_BOARD_DIR)):
images.append(imageio.imread(os.path.join(ReportSettings.OUTPUT_BOARD_DIR, filename)))
imageio.mimsave(f'{ReportSettings.OUTPUT_GIF_DIR}/movie.gif', images)
for filename in sorted(os.listdir(report_settings.OUTPUT_BOARD_DIR)):
images.append(imageio.imread(os.path.join(report_settings.OUTPUT_BOARD_DIR, filename)))
imageio.mimsave(f'{report_settings.OUTPUT_GIF_DIR}/movie.gif', images)
14 changes: 7 additions & 7 deletions simulation/report/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd

from simulation.automatos.board import Board
from simulation.settings import ReportSettings
from simulation.settings import report_settings


class Reporter:
Expand All @@ -30,12 +30,12 @@ def report_steps(self):

self.steps_data.loc[len(self.steps_data)] = func(self.flatten_board)

def report_prevt(self, type: str, started_day: int):
assert type in self.prevt_data.columns
def report_prevt(self, prevt_type: str, started_day: int):
assert prevt_type in self.prevt_data.columns

self.prevt_data[type] = started_day
self.prevt_data[prevt_type] = started_day

def save(self):
self.cells_data.to_csv(f'{ReportSettings.OUTPUT_SHEET_DIR}/cells.csv', index=False) # noqa
self.steps_data.to_csv(f'{ReportSettings.OUTPUT_SHEET_DIR}/steps.csv', index=False) # noqa
self.prevt_data.to_csv(f'{ReportSettings.OUTPUT_SHEET_DIR}/prevt.csv', index=False) # noqa
self.cells_data.to_csv(f'{report_settings.OUTPUT_SHEET_DIR}/cells.csv', index=False) # noqa
self.steps_data.to_csv(f'{report_settings.OUTPUT_SHEET_DIR}/steps.csv', index=False) # noqa
self.prevt_data.to_csv(f'{report_settings.OUTPUT_SHEET_DIR}/prevt.csv', index=False) # noqa
Loading

0 comments on commit ab12ffb

Please sign in to comment.