123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import os
- import dm
- import datetime
- import math
- import multiprocessing
- import tofu
- import tofu.config
- import tofu.reco
- import logging
- import logging.config
- import xdg.BaseDirectory
- APP_NAME = 'recofoo'
- LOG_INI = 'log.ini'
- DATA_PATH = xdg.BaseDirectory.save_data_path(APP_NAME)
- CONFIG_PATH = xdg.BaseDirectory.save_config_path(APP_NAME)
- TIMESTAMP_PATH = os.path.join(DATA_PATH, 'timestamp')
- class RecoProcess(multiprocessing.Process):
- def __init__(self, params):
- super(RecoProcess, self).__init__()
- self.params = params
- def run(self):
- time = tofu.reco.tomo(self.params)
- def get_last_timestamp():
- if not os.path.exists(TIMESTAMP_PATH):
- return datetime.datetime.min
- with open(TIMESTAMP_PATH) as f:
- time = f.read()
- return datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S')
- def update_timestamp(timestamp):
- with open(TIMESTAMP_PATH, 'w') as f:
- f.write(timestamp.strftime('%Y-%m-%dT%H:%M:%S'))
- def configure_logging():
- config_path = os.path.join(CONFIG_PATH, LOG_INI)
- log_path = LOG_INI if os.path.exists(LOG_INI) else None
- if not log_path:
- log_path = config_path if os.path.exists(config_path) else None
- if log_path:
- logging.config.fileConfig(log_path)
- else:
- logging.basicConfig(level=logging.INFO)
- def reconstruct_scan(input_path, output_path):
- log = logging.getLogger(APP_NAME)
- params = tofu.config.TomoParams().get_defaults()
- params.input = os.path.join(input_path, 'radios')
- params.from_projections = True
- params.output = os.path.join(output_path, 'data', 'slices', 'slice-%05i.tif')
- params.number = 1698
- params.axis = 500
- params.angle_step = math.radians(0.105945)
- log.info(" Reconstructing ... writing to {}".format(params.output))
- proc = RecoProcess(params)
- proc.start()
- proc.join()
- log.info(" Done.")
- class Application(object):
- def __init__(self):
- self.client = dm.Client(token="TjMgcEtjGyL2qXZ4", token_secret="xBJS0Oq9j5Hjc8Kq")
- self.log = logging.getLogger(APP_NAME)
- def run(self):
- last_update = get_last_timestamp()
- self.objects = {o.uuid: o for o in self.client.get_objects() if o.note == 'raw'}
- self.accesspoint = self.client.get_accesspoints('file://')[0]
- downloads = {d.object_uuid: d for d in self.client.get_downloads()}
- for uuid, obj in self.objects.items():
- self.log.info("Checking downloads for {}".format(obj))
- if uuid in downloads:
- download = downloads[uuid]
- self.log.info("Using download={}".format(download))
- self.reconstruct(obj, download)
- else:
- download = obj.create_download(ap)
- self.log.warn("No download found, created {}".format(download))
- now = datetime.datetime.now()
- update_timestamp(now)
- def reconstruct(self, obj, download):
- _, path = download.url.split('file:', 1)
- data_path = os.path.join(path, 'data')
- if os.path.isdir(data_path) and 'radios' in os.listdir(data_path):
- user_client, new_ingest = self.create_ingest(obj)
- if new_ingest:
- _, output_path = new_ingest.staging_url.split('file:', 1)
- reconstruct_scan(data_path, output_path)
- new_ingest.status = dm.models.IngestStatus.PRE_INGEST_FINISHED
- else:
- self.log.error("No raw data found in {}".format(data_path))
- def create_ingest(self, obj):
- try:
- user = self.client.get_user(obj.uploader)
- user_client = dm.Client(token=user.credentials.key, token_secret=user.credentials.secret)
- new_obj = user_client.create_object(obj.investigation_id, user, label='reco')
- self.log.info(" Created new object={}".format(new_obj))
- new_ingest = new_obj.create_ingest(self.accesspoint)
- # get the ingest again to acccess the staging url ...
- new_ingest = dm.models.Ingest(user_client, new_ingest.id)
- self.log.info(" Created new ingest={}".format(new_ingest))
- return (user_client, new_ingest)
- except dm.exceptions.NotFoundError:
- self.log.warn(" No credentials found for user={}".format(obj.uploader))
- except dm.exceptions.ArbitraryError as e:
- self.log.error(" Could not create object [{}]".format(e))
- if __name__ == '__main__':
- configure_logging()
- Application().run()
|