def create_paths(toyz_settings, tid, params): """ Creates a new path on the server (if it does not already exist). Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): path to create on the server Response id: 'create_folder', status (*string* ): 'success', path (*string* ): path created on the server """ core.check4keys(params, ['path', 'new_folder']) path = os.path.join(params['path'], params['new_folder']) permissions = file_access.get_parent_permissions(toyz_settings.db, path, user_id=tid['user_id']) if 'w' not in permissions and 'x' not in permissions: raise ToyzJobError("You do not have permission to create path {0}".format(path)) core.create_paths(path) response = { 'id': 'notification', 'msg': 'Created path'.format(path), 'func': 'create_dir' } return response
def create_tile(file_info, img_info, tile_info): try: from PIL import Image except ImportError: raise ToyzJobError( "You must have PIL (Python Imaging Library) installed to " "open files of this type" ) if file_info['ext']=='fits': try: from matplotlib import cm as cmap from matplotlib.colors import Normalize, LinearSegmentedColormap except ImportError: raise ToyzJobError("You must have matplotlib installed to load FITS images") hdulist = get_file(file_info) data = hdulist[int(img_info['frame'])].data # If no advanced resampling algorithm is used, scale the data as quickly as possible. # Otherwise crop the data. if file_info['resampling'] == 'NEAREST': data = scale_data(file_info, img_info, tile_info, data) else: data = data[ tile_info['y0_idx']:tile_info['yf_idx'], tile_info['x0_idx']:tile_info['xf_idx']] # FITS images have a flipped y-axis from what browsers and other image formats expect if img_info['invert_y']: data = np.flipud(data) if img_info['invert_x']: data = np.fliplr(data) norm = Normalize(img_info['colormap']['px_min'], img_info['colormap']['px_max'], True) colormap_name = img_info['colormap']['name'] if img_info['colormap']['invert_color']: colormap_name = colormap_name + '_r' colormap = getattr(cmap, colormap_name) cm = cmap.ScalarMappable(norm, colormap) img = np.uint8(cm.to_rgba(data)*255) img = Image.fromarray(img) if file_info['resampling'] != 'NEAREST': img = img.resize( (tile_info['width'], tile_info['height']), getattr(Image, file_info['resampling'])) else: img = get_file(file_info) img = img.crop(( tile_info['x0_idx'], tile_info['y0_idx'], tile_info['xf_idx'], tile_info['yf_idx'])) img = img.resize( (tile_info['width'], tile_info['height']), getattr(Image, file_info['resampling'])) width, height = img.size if width>0 and height>0: path = os.path.dirname(tile_info['new_filepath']) core.create_paths([path]) img.save(tile_info['new_filepath'], format=img_formats[file_info['tile_format']]) else: return False, '' return True, tile_info
def run_sextractor(toyz_settings, tid, params): """ Run sextractor using parameters defined in the client """ import astrotoyz.astromatic.sex as sex import toyz.utils.db import shutil core.check4keys(params, ['config', 'params', 'filename']) shortcuts = toyz.utils.db.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) # Just in case the user did something dumb, like remove his/her temp path if 'temp' not in shortcuts: raise ToyJobError("You removed your 'temp' shortcut, DON'T DO THAT!" + "Refresh your browser and it will be restored") # Create a path for temporary files that will be erased once this has been completed temp_path = os.path.join(shortcuts['temp'], tid['session_id'], str(tid['request_id'])) core.create_paths(temp_path) # Run SExtractor params['temp_path'] = temp_path result = sex.run_sextractor(**params) # Remove the temporary path form the server #shutil.rmtree(temp_path) if result!='success': response = { 'id': 'ERROR', 'error': result } else: response = { 'id': 'run_sextractor', 'result': result } return response
def create_paths(toyz_settings, tid, params): """ Creates a new path on the server (if it does not already exist). Parameters - toyz_settings ( :py:class:`toyz.utils.core.ToyzSettings`): Settings for the toyz application - tid (*string* ): Task ID of the client user running the task - params (*dict* ): Any parameters sent by the client (see *params* below) Params - path (*string* ): path to create on the server Response id: 'create_folder', status (*string* ): 'success', path (*string* ): path created on the server """ core.check4keys(params, ['path', 'new_folder']) path = os.path.join(params['path'], params['new_folder']) permissions = file_access.get_parent_permissions(toyz_settings.db, path, user_id=tid['user_id']) if 'w' not in permissions and 'x' not in permissions: raise ToyzJobError( "You do not have permission to create path {0}".format(path)) core.create_paths(path) response = { 'id': 'notification', 'msg': 'Created path'.format(path), 'func': 'create_dir' } return response
def run_sextractor(toyz_settings, tid, params): """ Run sextractor using parameters defined in the client """ import astrotoyz.astromatic.sex as sex import toyz.utils.db import shutil core.check4keys(params, ['config', 'params', 'filename']) shortcuts = toyz.utils.db.get_param(toyz_settings.db, 'shortcuts', user_id=tid['user_id']) # Just in case the user did something dumb, like remove his/her temp path if 'temp' not in shortcuts: raise ToyJobError("You removed your 'temp' shortcut, DON'T DO THAT!" + "Refresh your browser and it will be restored") # Create a path for temporary files that will be erased once this has been completed temp_path = os.path.join(shortcuts['temp'], tid['session_id'], str(tid['request_id'])) core.create_paths(temp_path) # Run SExtractor params['temp_path'] = temp_path result = sex.run_sextractor(**params) # Remove the temporary path form the server #shutil.rmtree(temp_path) if result != 'success': response = {'id': 'ERROR', 'error': result} else: response = {'id': 'run_sextractor', 'result': result} return response
def create_tile(file_info, img_info, tile_info): try: from PIL import Image except ImportError: raise ToyzJobError( "You must have PIL (Python Imaging Library) installed to " "open files of this type") if file_info['ext'] == 'fits': try: from matplotlib import cm as cmap from matplotlib.colors import Normalize, LinearSegmentedColormap except ImportError: raise ToyzJobError( "You must have matplotlib installed to load FITS images") hdulist = get_file(file_info) data = hdulist[int(img_info['frame'])].data # If no advanced resampling algorithm is used, scale the data as quickly as possible. # Otherwise crop the data. if file_info['resampling'] == 'NEAREST': data = scale_data(file_info, img_info, tile_info, data) else: data = data[tile_info['y0_idx']:tile_info['yf_idx'], tile_info['x0_idx']:tile_info['xf_idx']] # FITS images have a flipped y-axis from what browsers and other image formats expect if img_info['invert_y']: data = np.flipud(data) if img_info['invert_x']: data = np.fliplr(data) norm = Normalize(img_info['colormap']['px_min'], img_info['colormap']['px_max'], True) colormap_name = img_info['colormap']['name'] if img_info['colormap']['invert_color']: colormap_name = colormap_name + '_r' colormap = getattr(cmap, colormap_name) cm = cmap.ScalarMappable(norm, colormap) img = np.uint8(cm.to_rgba(data) * 255) img = Image.fromarray(img) if file_info['resampling'] != 'NEAREST': img = img.resize((tile_info['width'], tile_info['height']), getattr(Image, file_info['resampling'])) else: img = get_file(file_info) img = img.crop((tile_info['x0_idx'], tile_info['y0_idx'], tile_info['xf_idx'], tile_info['yf_idx'])) img = img.resize((tile_info['width'], tile_info['height']), getattr(Image, file_info['resampling'])) width, height = img.size if width > 0 and height > 0: path = os.path.dirname(tile_info['new_filepath']) core.create_paths([path]) img.save(tile_info['new_filepath'], format=img_formats[file_info['tile_format']]) else: return False, '' return True, tile_info
def new_session(self, user_id, websocket, session_id=None): """ Open a new websocket session for a given user Parameters user_id ( :py:class:`toyz.utils.core.ToyzUser` ): User id websocket (:py:class:`toyz.web.app.WebSocketHandler` ): new websocket opened """ import datetime if user_id not in self.user_sessions: self.user_sessions[user_id] = {} print("Users logged in:", self.user_sessions.keys()) if session_id is None: session_id = str(datetime.datetime.now()).replace( ' ', '__').replace('.', '-').replace(':', '_') self.user_sessions[user_id][session_id] = websocket shortcuts = db_utils.get_param(self.toyz_settings.db, 'shortcuts', user_id=user_id) shortcuts = core.check_user_shortcuts(self.toyz_settings, user_id, shortcuts) websocket.session = { 'user_id': user_id, 'session_id': session_id, 'path': os.path.join(shortcuts['temp'], session_id), } core.create_paths(websocket.session['path']) #initialize process for session jobs websocket.job_pipe, remote_pipe = multiprocessing.Pipe() websocket.process = multiprocessing.Process(target=job_process, args=(session_id, remote_pipe, websocket.job_pipe)) websocket.process.start() remote_pipe.close() process_events = (tornado.ioloop.IOLoop.READ | tornado.ioloop.IOLoop.ERROR) tornado.ioloop.IOLoop.current().add_handler(websocket.job_pipe, websocket.send_response, process_events) websocket.write_message({ 'id': 'initialize', 'user_id': user_id, 'session_id': session_id, })
def new_session(self, user_id, websocket, session_id=None): """ Open a new websocket session for a given user Parameters user_id ( :py:class:`toyz.utils.core.ToyzUser` ): User id websocket (:py:class:`toyz.web.app.WebSocketHandler` ): new websocket opened """ import datetime if user_id not in self.user_sessions: self.user_sessions[user_id] = {} print("Users logged in:", self.user_sessions.keys()) if session_id is None: session_id = str( datetime.datetime.now()).replace(' ','__').replace('.','-').replace(':','_') self.user_sessions[user_id][session_id] = websocket shortcuts = db_utils.get_param(self.toyz_settings.db, 'shortcuts', user_id=user_id) shortcuts = core.check_user_shortcuts(self.toyz_settings, user_id, shortcuts) websocket.session = { 'user_id': user_id, 'session_id': session_id, 'path': os.path.join(shortcuts['temp'], session_id), } core.create_paths(websocket.session['path']) #initialize process for session jobs websocket.job_pipe, remote_pipe = multiprocessing.Pipe() websocket.process = multiprocessing.Process( target = job_process, args=(session_id, remote_pipe, websocket.job_pipe)) websocket.process.start() remote_pipe.close() process_events = (tornado.ioloop.IOLoop.READ | tornado.ioloop.IOLoop.ERROR) tornado.ioloop.IOLoop.current().add_handler( websocket.job_pipe, websocket.send_response, process_events) websocket.write_message({ 'id': 'initialize', 'user_id': user_id, 'session_id': session_id, })