Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ jobs:
- name: Script checks
run: |
shellcheck ./scripts/run.sh
shellcheck ./scripts/slurm.sh
2 changes: 1 addition & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ do
python3 ./utils/raster_sum.py --rasters_directory "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/"${TAXA}"/ --output "${DATADIR}"/deltap_sum/"${SCENARIO}"/"${CURVE}"/"${TAXA}".tif
done

python3 ./utils/species_totals.py --aohs "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/ --output "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/totals.csv
python3 ./utils/species_totals.py --deltaps "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/ --output "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/totals.csv

# Generate final map
python3 ./deltap/delta_p_scaled.py --input "${DATADIR}"/deltap_sum/"${SCENARIO}"/"${CURVE}"/ \
Expand Down
15 changes: 8 additions & 7 deletions scripts/slurm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

set -e

source ${HOME}/venvs/life/bin/activate
cd ${HOME}/dev/life
export PATH=$PATH:$HOME/go/bin
# shellcheck disable=SC1091
source "${HOME}"/venvs/life/bin/activate
cd "${HOME}"/dev/life
export PATH="${PATH}":"${HOME}"/go/bin

if [ -z "${DATADIR}" ]; then
echo "Please specify $DATADIR"
Expand Down Expand Up @@ -170,7 +171,7 @@ python3 ./utils/speciesgenerator.py --datadir "${DATADIR}" --output "${DATADIR}"
python3 ./utils/persistencegenerator.py --datadir "${DATADIR}" --curve "${CURVE}" --output "${DATADIR}"/persistencebatch.csv

# Calculate all the AoHs
littlejohn -j ${SLURM_JOB_CPUS_PER_NODE} -o "${DATADIR}"/aohbatch.log -c "${DATADIR}"/aohbatch.csv "${VIRTUAL_ENV}"/bin/python3 -- ./aoh-calculator/aohcalc.py --force-habitat
littlejohn -j "${SLURM_JOB_CPUS_PER_NODE}" -o "${DATADIR}"/aohbatch.log -c "${DATADIR}"/aohbatch.csv "${VIRTUAL_ENV}"/bin/python3 -- ./aoh-calculator/aohcalc.py --force-habitat

# Generate validation summaries
python3 ./aoh-calculator/validation/collate_data.py --aoh_results "${DATADIR}"/aohs/current/ --output "${DATADIR}"/aohs/current.csv
Expand All @@ -188,16 +189,16 @@ python3 ./aoh-calculator/summaries/endemism.py --aohs_folder "${DATADIR}"/aohs/c
--output "${DATADIR}"/predictors/endemism.tif

# Calculate the per species Delta P values
littlejohn -j ${SLURM_JOB_CPUS_PER_NODE} -o "${DATADIR}"/persistencebatch.log -c "${DATADIR}"/persistencebatch.csv "${VIRTUAL_ENV}"/bin/python3 -- ./deltap/global_code_residents_pixel.py
littlejohn -j "${SLURM_JOB_CPUS_PER_NODE}" -o "${DATADIR}"/persistencebatch.log -c "${DATADIR}"/persistencebatch.csv "${VIRTUAL_ENV}"/bin/python3 -- ./deltap/global_code_residents_pixel.py

for SCENARIO in "${SCENARIOS[@]}"
do
for TAXA in "${TAXAS[@]}"
do
python3 ./utils/raster_sum.py --rasters_directory "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/"${TAXA}"/ --output "${DATADIR}"/deltap_sum/"${SCENARIO}"/"${CURVE}"/"${TAXA}".tif
python3 ./utils/raster_sum.py --rasters_directory "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/"${TAXA}"/ --output "${DATADIR}"/deltap_sum/"${SCENARIO}"/"${CURVE}"/"${TAXA}".tif -j "${SLURM_JOB_CPUS_PER_NODE}"
done

python3 ./utils/species_totals.py --aohs "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/ --output "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/totals.csv
python3 ./utils/species_totals.py --deltaps "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/ --output "${DATADIR}"/deltap/"${SCENARIO}"/"${CURVE}"/totals.csv

# Generate final map
python3 ./deltap/delta_p_scaled.py --input "${DATADIR}"/deltap_sum/"${SCENARIO}"/"${CURVE}"/ \
Expand Down
21 changes: 12 additions & 9 deletions utils/raster_sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import os
import queue
import resource
import sys
import tempfile
import time
Expand All @@ -26,8 +27,6 @@ def worker(
path: Path = input_queue.get_nowait()
except queue.Empty:
break
if compress:
print(path)

with RasterLayer.layer_from_file(path) as partial_raster:
if merged_result is None:
Expand All @@ -50,16 +49,26 @@ def raster_sum(
output_filename: Path,
processes_count: int
) -> None:
print(f"process count set to {processes_count}")

_, max_fd_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (max_fd_limit, max_fd_limit))
print(f"Set fd limit to {max_fd_limit}")

os.makedirs(output_filename.parent, exist_ok=True)

files = images_dir.glob("*.tif")
files = list(images_dir.glob("*.tif"))
if not files:
sys.exit(f"No files in {images_dir}, aborting")
print(f"Found {len(files)} images to process")

with tempfile.TemporaryDirectory() as tempdir:
with Manager() as manager:
source_queue = manager.Queue()

for file in files:
source_queue.put(file)

workers = [Process(target=worker, args=(
False,
f"{index}.tif",
Expand All @@ -69,11 +78,6 @@ def raster_sum(
for worker_process in workers:
worker_process.start()

for file in files:
source_queue.put(file)
# for _ in range(len(workers)):
# source_queue.put(None)

processes = workers
while processes:
candidates = [x for x in processes if not x.is_alive()]
Expand All @@ -86,7 +90,6 @@ def raster_sum(
processes.remove(candidate)
time.sleep(0.1)


# here we should have now a set of images in tempdir to merge
single_worker = Process(target=worker, args=(
True,
Expand Down