예제 #1
0
    def setUpClass(cls):
        # Setup and run simulation data, single hosts on lattice with exponential kernel

        cls._data_stub = os.path.join("testing", "spatial_sim_output")
        cls._beta_val = 10
        cls._scale_val = 0.3
        cls._end_time = 10
        size = (20, 20)

        # Create host file
        host_raster = raster_tools.RasterData(size, array=np.ones(size))
        host_file = os.path.join("testing", "spatial_host_test_case.txt")
        host_raster.to_file(host_file)

        # Create initial conditions files
        init_stub = os.path.join("testing", "spatial_init_test_case")
        host_raster.array[int(size[0]/2), int(size[1]/2)] = 0
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.array = np.zeros(size)
        host_raster.array[int(size[0]/2), int(size[1]/2)] = 1
        host_raster.to_file(init_stub + "_I.txt")

        # Create kernel file
        kernel_range = 20
        kernel_size = (2*kernel_range+1, 2*kernel_range+1)
        kernel_raster = raster_tools.RasterData(kernel_size, array=np.zeros(kernel_size))
        for i in range(kernel_size[0]):
            for j in range(kernel_size[1]):
                row = i - kernel_range
                col = j - kernel_range
                distance = np.sqrt(row*row + col*col)
                # kernel_raster.array[i, j] = np.exp(-distance/cls._scale_val) / (
                #     2 * np.pi * cls._scale_val * cls._scale_val)
                kernel_raster.array[i, j] = np.exp(-distance/cls._scale_val)
        kernel_raster.to_file(init_stub + "_kernel.txt")

        # Setup config file
        config_filename = os.path.join("testing", "spatial_config.ini")
        config_str = "\n[Epidemiology]\n"
        config_str += "Model = SI\nInfRate = " + str(cls._beta_val)
        config_str += "\nIAdvRate = 0.0\nKernelType = RASTER\n"
        config_str += "\n[Simulation]\n"
        config_str += "SimulationType = RASTER\nFinalTime = " + str(cls._end_time) +"\n"
        config_str += "NIterations = 100\n"
        config_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        config_str += "KernelFile = " + init_stub + "_kernel.txt" + "\n"
        config_str += "VirtualSporulationStart = 3\nMaxHosts = 1"
        config_str += "\n[Output]\n"
        config_str += "SummaryOutputFreq = 0\nOutputFileStub = " + cls._data_stub
        config_str += "\n[Optimisation]\n"
        config_str += "SaveSetup = False\nRateStructure-Infection = ratetree"
        with open(config_filename, "w") as outfile:
            outfile.write(config_str)

        cls._raster_header = host_raster.header_vals

        # Run simulations
        IndividualSimulator.main(configFile=config_filename)
    def setUp(self):
        # Setup and run simulation data, single infected hosts on lattice with non-spatial kernel.

        self._full_lambda = full_lambda
        self._data_stub = os.path.join("testing",
                                       "cont_intervention_sim_output")
        size = (100, 100)
        kernel_size = (3, 3)
        kernel_centre = [int(x / 2) for x in kernel_size]

        # Create host file
        host_raster = raster_tools.RasterData(size, array=np.ones(size))
        host_file = os.path.join("testing",
                                 "cont_intervention_host_test_case.txt")
        host_raster.to_file(host_file)

        # Create initial conditions files
        init_stub = os.path.join("testing", "cont_intervention_init_test_case")
        host_raster.to_file(init_stub + "_I.txt")
        host_raster.array = np.zeros(size)
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.to_file(init_stub + "_R.txt")

        # Create kernel file
        kernel_raster = raster_tools.RasterData(kernel_size,
                                                array=np.full(
                                                    kernel_size, 1.0))
        kernel_raster.array[kernel_centre[0], kernel_centre[1]] = 0
        kernel_raster.to_file(init_stub + "_kernel.txt")

        self._kernel = kernel_raster.array
        self._size = size

        # Setup config file
        self.config_filename = os.path.join("testing",
                                            "cont_intervention_config.ini")
        config_str = "\n[Epidemiology]\n"
        config_str += "Model = SIR\nInfRate = 0"
        config_str += "\nIAdvRate = " + str(
            self._full_lambda / 2) + "\nKernelType = RASTER\n"
        config_str += "\n[Simulation]\n"
        config_str += "SimulationType = RASTER\nFinalTime = 1000\nNIterations = 1\n"
        config_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        config_str += "KernelFile = " + init_stub + "_kernel.txt" + "\n"
        config_str += "VirtualSporulationStart = None"
        config_str += "\n[Output]\n"
        config_str += "SummaryOutputFreq = 0\nOutputFileStub = " + self._data_stub
        config_str += "\n[Optimisation]\n"
        config_str += "SaveSetup = False"
        config_str += "\n[Interventions]\n"
        config_str += "InterventionUpdateFrequencies = None\n"
        config_str += "UpdateOnAllEvents = True\n"

        with open(self.config_filename, "w") as outfile:
            outfile.write(config_str)
예제 #3
0
def create_initial_conditions(host_array,
                              out_stub="InitialConditions",
                              seed_inf_cell=(0, 0),
                              host_numbers=False,
                              prop_infected=1.0):

    init_s_array = np.zeros(
        (host_array.header_vals['nrows'], host_array.header_vals['ncols']))
    init_i_array = np.zeros(
        (host_array.header_vals['nrows'], host_array.header_vals['ncols']))
    init_r_array = np.zeros(
        (host_array.header_vals['nrows'], host_array.header_vals['ncols']))

    for row in range(host_array.header_vals['nrows']):
        for col in range(host_array.header_vals['ncols']):
            if host_array.array[row, col] > 0:
                if (row, col) != seed_inf_cell:
                    if host_numbers:
                        init_s_array[row, col] = host_array.array[row, col]
                    else:
                        init_s_array[row, col] = 1.0
                else:
                    if host_numbers:
                        init_i_array[row, col] = int(
                            np.ceil(prop_infected *
                                    host_array.array[row, col]))
                        init_s_array[row, col] = int(
                            np.floor((1 - prop_infected) *
                                     host_array.array[row, col]))
                    else:
                        init_i_array[row, col] = prop_infected
                        init_s_array[row, col] = 1 - prop_infected

    init_s_raster = raster_tools.RasterData(
        array=init_s_array,
        cellsize=host_array.header_vals['cellsize'],
        shape=init_s_array.shape,
        llcorner=(host_array.header_vals['xllcorner'],
                  host_array.header_vals['yllcorner']))
    init_i_raster = raster_tools.RasterData(
        array=init_i_array,
        cellsize=host_array.header_vals['cellsize'],
        shape=init_i_array.shape,
        llcorner=(host_array.header_vals['xllcorner'],
                  host_array.header_vals['yllcorner']))
    init_r_raster = raster_tools.RasterData(
        array=init_r_array,
        cellsize=host_array.header_vals['cellsize'],
        shape=init_r_array.shape,
        llcorner=(host_array.header_vals['xllcorner'],
                  host_array.header_vals['yllcorner']))

    init_s_raster.to_file(os.path.join("GeneratedData", out_stub + "_S.txt"))
    init_i_raster.to_file(os.path.join("GeneratedData", out_stub + "_I.txt"))
    init_r_raster.to_file(os.path.join("GeneratedData", out_stub + "_R.txt"))
예제 #4
0
    def setUp(cls):
        # Setup and run simulation data, single hosts on lattice with exponential kernel

        cls._data_stub = os.path.join("testing", "VS_rates_sim_output")
        cls._beta_val = 10e3
        cls._scale_val = 1
        size = (21, 21)
        kernel_size = (43, 43)
        kernel_centre = [int(x / 2) for x in kernel_size]

        # Create host file
        host_raster = raster_tools.RasterData(size, array=np.ones(size))
        host_file = os.path.join("testing", "VS_rates_host_test_case.txt")
        host_raster.to_file(host_file)

        # Create initial conditions files
        init_stub = os.path.join("testing", "VS_rates_init_test_case")
        host_raster.array[int(size[0] / 2), int(size[1] / 2)] = 0
        host_raster.to_file(init_stub + "_E.txt")
        host_raster.array = np.zeros(size)
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.array[int(size[0] / 2), int(size[1] / 2)] = 1
        host_raster.to_file(init_stub + "_I.txt")

        # Create kernel file
        kernel_raster = raster_tools.RasterData(kernel_size,
                                                array=np.full(
                                                    kernel_size, 1.0))
        kernel_raster.array[kernel_centre[0], kernel_centre[1]] = 0
        kernel_raster.to_file(init_stub + "_kernel.txt")

        cls._kernel = kernel_raster.array
        cls._size = size

        # Setup config file
        config_filename = os.path.join("testing", "VS_rates_config.ini")
        config_str = "\n[Epidemiology]\n"
        config_str += "Model = SEI\nInfRate = " + str(cls._beta_val)
        config_str += "\nEAdvRate = 0.0\nIAdvRate = 0.0\nKernelType = RASTER\n"
        config_str += "\n[Simulation]\n"
        config_str += "SimulationType = RASTER\nFinalTime = 1\nNIterations = 1\n"
        config_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        config_str += "KernelFile = " + init_stub + "_kernel.txt" + "\n"
        config_str += "VirtualSporulationStart = 1"
        config_str += "\n[Output]\n"
        config_str += "SummaryOutputFreq = 0\nOutputFileStub = " + cls._data_stub
        config_str += "\n[Optimisation]\n"
        config_str += "SaveSetup = False"
        with open(config_filename, "w") as outfile:
            outfile.write(config_str)

        cls._simulator = simulator.Simulator(config_file=config_filename)
        cls._simulator.setup()
예제 #5
0
def average_mask(target_header):
    """Average weather and forest type mask over full 18 years of data for given target."""

    analysis_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                 "..")

    weather_stub = os.path.join(analysis_path, "InputData", "weather",
                                "gis_m_c_")
    llcorner = (target_header['xllcorner'], target_header['yllcorner'])
    urcorner = (target_header['xllcorner'] +
                (target_header['ncols']) * target_header['cellsize'] - 125,
                target_header['yllcorner'] +
                (target_header['nrows']) * target_header['cellsize'] - 125)

    forest_mask = raster_tools.extract_raster(
        os.path.join(analysis_path, "InputData", "forestType_Map.txt"),
        llcorner, urcorner)
    forest_mask.array[forest_mask.array == -9999] = np.nan

    average_array = raster_tools.RasterData(
        shape=(target_header['nrows'], target_header['ncols']),
        llcorner=(target_header['xllcorner'], target_header['yllcorner']),
        cellsize=target_header['cellsize'],
        NODATA_value=target_header['NODATA_value'],
        array=np.zeros((target_header['nrows'], target_header['ncols'])))

    for year in range(1990, 2008):
        for week in range(1, 29):
            # Extract correct region of weather file
            weather_raster = raster_tools.extract_raster(
                weather_stub + str(year) + "_" + str(week) + ".txt", llcorner,
                urcorner)

            weather_raster.array[weather_raster.array == -9999] = np.nan
            if week < 7:
                weather_raster.array[forest_mask.array == 2] = 0

            if target_header['cellsize'] == weather_raster.header_vals[
                    'cellsize']:
                average_array.array += np.square(weather_raster.array)
            else:
                weather_raster.array = np.square(weather_raster.array)
                averaged_weather_array = raster_tools.extract_raster(
                    weather_raster,
                    llcorner,
                    urcorner,
                    resolution=target_header['cellsize'])
                averaged_weather_array.array[averaged_weather_array.array ==
                                             -9999] = 0
                average_array.array += averaged_weather_array.array

    average_array.array /= (28 * 18)
    average_array.array = np.sqrt(average_array.array)
    logging.info("Overall mean mask: %f", np.nanmean(average_array.array))

    average_array.array[np.where(np.isnan(
        average_array.array))] = average_array.header_vals['NODATA_value']

    return average_array
예제 #6
0
def read_sus_inf_files(all_cells,
                       header,
                       sus_file,
                       inf_file,
                       sim_type="INDIVIDUAL"):
    """Read all files associated with host susceptibility and infectiousness."""

    if sim_type == "INDIVIDUAL":

        return

    if sim_type == "RASTER":

        if isinstance(sus_file, raster_tools.RasterData):
            sus_raster = sus_file
        else:
            try:
                sus_raster = raster_tools.RasterData.from_file(sus_file)
            except (FileNotFoundError, TypeError):
                sus_raster = raster_tools.RasterData(
                    shape=(header['nrows'], header['ncols']),
                    llcorner=(header['xllcorner'], header['yllcorner']),
                    cellsize=header['cellsize'],
                    NODATA_value=header['NODATA_value'],
                    array=np.ones((header['nrows'], header['ncols'])))

        if isinstance(inf_file, raster_tools.RasterData):
            inf_raster = inf_file
        else:
            try:
                inf_raster = raster_tools.RasterData.from_file(inf_file)
            except (FileNotFoundError, TypeError):
                inf_raster = raster_tools.RasterData(
                    shape=(header['nrows'], header['ncols']),
                    llcorner=(header['xllcorner'], header['yllcorner']),
                    cellsize=header['cellsize'],
                    NODATA_value=header['NODATA_value'],
                    array=np.ones((header['nrows'], header['ncols'])))

        for cell in all_cells:
            row, col = cell.cell_position
            cell.susceptibility = sus_raster.array[row, col]
            cell.infectiousness = inf_raster.array[row, col]
예제 #7
0
    def setUpClass(cls):
        # Setup and run simulation data, multiple hosts in single cell to make non-spatial

        cls._data_stub = os.path.join("testing", "nonspatial_sim_output")
        cls._beta_val = 0.0007

        # Create host file
        host_raster = raster_tools.RasterData((1, 1), array=np.array([[1000]], dtype=float))
        host_file = os.path.join("testing", "nonspatial_host_test_case.txt")
        host_raster.to_file(host_file)

        # Create simulation initial conditions files
        init_stub = os.path.join("testing", "nonspatial_init_test_case")
        host_raster.array[0, 0] = 995
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.array[0, 0] = 5
        host_raster.to_file(init_stub + "_I.txt")

        # Create raster model initial conditions files
        host_raster.array[0, 0] = 1
        host_raster.to_file(init_stub + "_host_density.txt")
        host_raster.array[0, 0] = 0.995
        host_raster.to_file(init_stub + "_S_density.txt")
        host_raster.array[0, 0] = 0.005
        host_raster.to_file(init_stub + "_I_density.txt")

        # Create kernel file
        host_raster.array[0, 0] = 1
        host_raster.to_file(init_stub + "_kernel.txt")

        # Setup config file
        config_filename = os.path.join("testing", "nonspatial_config.ini")
        config_str = "\n[Epidemiology]\n"
        config_str += "Model = SI\nInfRate = " + str(cls._beta_val)
        config_str += "\nIAdvRate = 0.0\nKernelType = RASTER\n"
        config_str += "\n[Simulation]\n"
        config_str += "SimulationType = RASTER\nFinalTime = 10.0\nNIterations = 100\n"
        config_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        config_str += "KernelFile = " + init_stub + "_kernel.txt" + "\n"
        config_str += "VirtualSporulationStart = 1\nMaxHosts = 1000"
        config_str += "\n[Output]\n"
        config_str += "SummaryOutputFreq = 0\nOutputFileStub = " + cls._data_stub
        config_str += "\n[Optimisation]\n"
        config_str += "SaveSetup = False\nRateStructure-Infection = ratetree"
        with open(config_filename, "w") as outfile:
            outfile.write(config_str)

        cls._raster_header = host_raster.header_vals
        cls._init_stub = init_stub

        # Run simulations
        IndividualSimulator.main(configFile=config_filename, silent=True)
    def setUpClass(cls):
        # Setup fake simulation data
        cls._data_stub = os.path.join("testing", "quick_sim_output")

        # Create host file
        host_raster = raster_tools.RasterData((2, 2), array=np.ones((2, 2)))
        host_file = os.path.join("testing", "host_test_case.txt")
        host_raster.to_file(host_file)

        # Create initial conditions files
        init_stub = os.path.join("testing", "init_test_case")
        host_raster.array[1, 1] = 0
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.array = np.zeros((2, 2))
        host_raster.array[1, 1] = 1
        host_raster.to_file(init_stub + "_I.txt")

        # Create log file
        log_str = "Configuration File Used\n" + "#" * 20 + "\n"
        log_str += "\n[Epidemiology]\n"
        log_str += "Model = SI\nInfRate = 0.8\nKernelType = RASTER\n"
        log_str += "\n[Simulation]\n"
        log_str += "SimulationType = RASTER\nFinalTime = 10.0\nNIterations = 2\n"
        log_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        log_str += "\n[Output]\n"
        log_str += "SummaryOutputFreq = 0\nOutputFileStub = " + cls._data_stub
        with open(cls._data_stub + ".log", "w") as outfile:
            outfile.write(log_str)

        # Create host data files
        host_data_dict = {
            "posX": np.tile(np.arange(0.5, 2.5), 2),
            "posY": np.repeat(np.arange(1.5, -0.5, -1), 2),
            "hostID": list(range(4)),
            "initial_state": ["S"] * 3 + ["I"]
        }
        host_data = pd.DataFrame(host_data_dict)
        for i in range(2):
            filename = cls._data_stub + "_hosts_" + str(i) + ".csv"
            host_data.to_csv(filename, index=False)

        # Create event data files
        cls._inf_hosts = np.array([[0, 1, 2], [1, 2, 0]])
        for i in range(2):
            event_data_dict = {"time": [2, 4, 6], "hostID": cls._inf_hosts[i]}
            event_data = pd.DataFrame(event_data_dict)
            filename = cls._data_stub + "_events_" + str(i) + ".csv"
            event_data.to_csv(filename, index=False)

        cls._raster_header = host_raster.header_vals
예제 #9
0
def make_np_mask(target_header):
    """Generate mask of cells that are in Redwood National Park"""

    rdr = Reader(os.path.join("InputData", "nps_boundary", "nps_boundary.shp"))
    redw_records = []
    for x in rdr.records():
        if 'Redwood' in x.attributes['UNIT_NAME']:
            redw_records.append(x)

    redw_shape_nps = redw_records[0].geometry[0]

    NAD83_Cali_Albers = pyproj.Proj("+init=EPSG:3310")
    nps_proj = pyproj.Proj("+init=EPSG:4269")

    project = partial(pyproj.transform, nps_proj, NAD83_Cali_Albers)

    redw_shape = transform(project, redw_shape_nps)

    lower_x = np.array([
        target_header['xllcorner'] + i * target_header['cellsize']
        for i in range(target_header['ncols'])
    ])
    upper_x = lower_x + target_header['cellsize']

    lower_y = np.array([
        target_header['yllcorner'] + i * target_header['cellsize']
        for i in range(target_header['nrows'])
    ])[::-1]
    upper_y = lower_y + target_header['cellsize']

    np_array = np.zeros((target_header['nrows'], target_header['ncols']))

    for i in range(target_header['nrows']):
        for j in range(target_header['ncols']):
            points = [[lower_x[j], lower_y[i]], [upper_x[j], lower_y[i]],
                      [upper_x[j], upper_y[i]], [lower_x[j], upper_y[i]]]
            cell = geometry.Polygon(points)
            intersection_area = redw_shape.intersection(cell).area / (
                target_header['cellsize'] * target_header['cellsize'])

            np_array[i, j] = intersection_area

    np_raster = raster_tools.RasterData(
        (target_header['nrows'], target_header['ncols']),
        (target_header['xllcorner'], target_header['yllcorner']),
        target_header['cellsize'],
        array=np_array)

    return np_raster
예제 #10
0
def output_raster_data(parent_sim, time=None, iteration=None, states=None):
    """Output current state raster"""

    all_cells = parent_sim.all_cells
    header = parent_sim.params['header']

    if states is None:
        states = list(parent_sim.params['Model']) + ["Culled"]

    output_stub = parent_sim.params['RasterFileStub']

    if iteration is not None:
        iterstub = "_" + str(iteration)
    else:
        iterstub = ""

    if time is not None:
        timestub = "_" + str(time)
    else:
        timestub = ""

    for state in states:
        cell_state = np.full((header['nrows'], header['ncols']),
                             header['NODATA_value'])
        for row in range(header['nrows']):
            for col in range(header['ncols']):
                cell_id = parent_sim.params['cell_map'].get((row, col), None)
                if cell_id is not None:
                    cell_state[row, col] = all_cells[cell_id].states[state]
        cell_state = cell_state.reshape((header['nrows'], header['ncols']))

        raster = raster_tools.RasterData(shape=(header['nrows'],
                                                header['ncols']),
                                         llcorner=(header['xllcorner'],
                                                   header['yllcorner']),
                                         cellsize=header['cellsize'],
                                         NODATA_value=header['NODATA_value'],
                                         array=cell_state)

        raster.to_file(output_stub + iterstub + "_" + state + timestub +
                       ".txt")
예제 #11
0
    def setUpClass(cls):
        # Setup and run simulation data, single hosts on lattice with exponential kernel

        cls._data_stub = os.path.join("testing", "target_sim_output")
        cls._beta_val = 6
        cls._scale_val = 0.9
        cls._end_time = 10.0
        cls._size = (21, 21)

        # Create host file
        motif = np.zeros((3, 3))
        motif[1, 1] = 1
        host_array = np.tile(motif, cls._size)
        # host_array = host_array * np.random.randint(1, 4, host_array.shape)
        host_raster = raster_tools.RasterData([3*x for x in cls._size], array=host_array)
        host_file = os.path.join("testing", "target_host_test_case.txt")
        host_raster.to_file(host_file)

        # Create initial conditions files
        centre_host = tuple(3*int(x/2) + 1 for x in cls._size)
        init_stub = os.path.join("testing", "target_init_test_case")
        n_init_inf = host_raster.array[centre_host]
        host_raster.array[centre_host] = 0
        host_raster.to_file(init_stub + "_S.txt")
        host_raster.array = np.zeros_like(host_array)
        host_raster.array[centre_host] = n_init_inf
        host_raster.to_file(init_stub + "_I.txt")

        # TODO think about kernel normalisation

        # Create kernel file
        kernel_range = max(cls._size)*3
        kernel_size = (2*kernel_range+1, 2*kernel_range+1)
        kernel_raster = raster_tools.RasterData(kernel_size, array=np.zeros(kernel_size))
        for i in range(kernel_size[0]):
            for j in range(kernel_size[1]):
                row = i - kernel_range
                col = j - kernel_range
                distance = np.sqrt(row*row + col*col)
                # host_raster.array[i, j] = np.exp(-distance/cls._scale_val) / (
                #     2 * np.pi * cls._scale_val * cls._scale_val)
                if row % 3 == 0 and col % 3 == 0:
                    kernel_raster.array[i, j] = np.exp(-distance/cls._scale_val)

        kernel_raster.to_file(init_stub + "_kernel.txt")

        # Setup config file
        config_filename = os.path.join("testing", "target_config.ini")
        config_str = "\n[Epidemiology]\n"
        config_str += "Model = SI\nInfRate = " + str(cls._beta_val)
        config_str += "\nIAdvRate = 0.0\nKernelType = RASTER\n"
        config_str += "\n[Simulation]\n"
        config_str += "SimulationType = RASTER\nFinalTime = " + str(cls._end_time) + "\n"
        config_str += "NIterations = 200\n"
        config_str += "HostPosFile = " + host_file + "\nInitCondFile = " + init_stub + "\n"
        config_str += "KernelFile = " + init_stub + "_kernel.txt" + "\n"
        config_str += "VirtualSporulationStart = 3\nMaxHosts = 1"
        config_str += "\n[Output]\n"
        config_str += "SummaryOutputFreq = 0\nOutputFileStub = " + cls._data_stub
        config_str += "\n[Optimisation]\n"
        config_str += "SaveSetup = False\nRateStructure-Infection = ratetree"
        with open(config_filename, "w") as outfile:
            outfile.write(config_str)

        # Run simulations
        IndividualSimulator.main(configFile=config_filename)