def _init_client(spec, require_token=False): if 'api_url' in spec: client = girder_client.GirderClient( apiUrl=spec['api_url'], cacheSettings=_get_cache_settings(spec)) elif 'host' in spec: scheme = spec.get('scheme', 'http') port = spec.get('port', { 'http': 80, 'https': 443 }[scheme]) api_root = spec.get('api_root', '/api/v1') client = girder_client.GirderClient( host=spec['host'], scheme=scheme, apiRoot=api_root, port=port, cacheSettings=_get_cache_settings(spec)) else: raise Exception('You must pass either an api_url or host key for ' 'Girder input and output bindings.') if 'token' in spec: client.setToken(spec['token']) elif require_token: raise Exception('You must pass a token for Girder authentication.') return client
def _gc(self): gc = girder_client.GirderClient(apiUrl=self.api_url) if self.api_key is not None: gc = girder_client.GirderClient(apiUrl=self.api_url) gc.authenticate(apiKey=self.api_key) elif self.token is not None: gc.token = self.token return gc
def get_gc(self): connection_info = 'https://data.kitware.com/api/v1' try: gc = girder_client.GirderClient(apiUrl=connection_info) gc.authenticate(**self.credentials) return gc except: try: gc = girder_client.GirderClient(apiUrl=connection_info) except: raise ValueError("Cannot connect to the Girder.") return gc
def connect_to_api(apiurl, apikey=None, interactive=True): """Connect to a specific girder API. Parameters ---------- apiurl : str URL to the AP to connect to. apikey : str API authentication token key interactive : bool Whether to use interactive mode instead. Returns ------- girder_client.GirderClient Authenticated girder client. """ assert interactive or (apikey is not None) gc = girder_client.GirderClient(apiUrl=apiurl) if apikey is not None: interactive = False if interactive: gc.authenticate(interactive=True) else: gc.authenticate(apiKey=apikey) return gc
def authenticate_me(self, **kwargs): connection_info = 'https://data.kitware.com/api/v1' username = kwargs['username'] password = kwargs['password'] try: gc = girder_client.GirderClient(apiUrl=connection_info) gc.authenticate(username, password) db = get_db() with db_session: p = db.Providers.select().filter(provider=self.name).first() provider_metadata = { 'provider': self.name, 'username': username, 'password': password, } if p is None: db.Providers(**provider_metadata) else: p.set(**provider_metadata) return True except: log.error( "Either credentials invalid or unable to connect to the Girder live server." ) return False
def __init__(self, item_id=None, token=None, name='gaia_result.json', uri='', **kwargs): """ Read and write GeoJSON data to/from Girder :param item_id: Item id to read/write from/to :param uri: location of temporary file :param kwargs: Other keyword arguments """ self.id = item_id self.token = token if uri: self.uri = uri else: tmpdir = tempfile.mkdtemp() self.uri = tempfile.mkstemp(suffix='.json', dir=tmpdir)[1] self.filename = name girderPort = config.getConfig()['server.socket_port'] client = girder_client.GirderClient(port=girderPort) client.token = token self.client = client self.meta = self.client.getItem(item_id) super(MinervaVectorIO, self).__init__(uri=self.uri, **kwargs)
def setUp(self): base.TestCase.setUp(self) admin = { 'email': '*****@*****.**', 'login': '******', 'firstName': 'Admin', 'lastName': 'Last', 'password': '******', 'admin': True } self.admin = User().createUser(**admin) self.gc = girder_client.GirderClient( apiUrl='http://127.0.0.1:%d/api/v1' % (int(os.environ['GIRDER_PORT']))) self.gc.authenticate(username='******', password='******') self.publicFolder = self.gc.get( 'resource/lookup', parameters={'path': 'user/%s/Public' % self.admin['login']}) wsi_path = os.path.join( TEST_DATA_DIR, 'TCGA-A2-A0YE-01Z-00-DX1.8A2E3094-5755-42BC-969D-7F0A2ECA0F39.svs') self.wsiFile = self.gc.uploadFile(self.publicFolder['_id'], stream=open(wsi_path, 'rb'), name=os.path.basename(wsi_path), size=os.stat(wsi_path).st_size, parentType='folder') annotations_path = wsi_path + '_annotations.json' self.gc.post('annotation/item/%s' % self.wsiFile['itemId'], data=open(annotations_path, 'rb').read(), headers={'Content-Type': 'application/json'})
def get_test_data(client, opts): """ Make sure we have a test collection with a folder with test data. :param client: girder client. :param opts: command line options. """ collName = 'HistomicsTK Tests' collection = client.resourceLookup('/collection/' + collName, True) if not collection: collection = client.createCollection(collName, public=True) folderName = 'Images' folder = client.resourceLookup( '/collection/%s/%s' % (collName, folderName), True) if not folder: folder = client.createFolder(collection['_id'], folderName, parentType='collection') if opts.get('test') != 'local': remote = girder_client.GirderClient( apiUrl='https://data.kitware.com/api/v1') remoteFolder = remote.resourceLookup( '/collection/HistomicsTK/Deployment test images') for item in remote.listItem(remoteFolder['_id']): localPath = '/collection/%s/%s/%s' % (collName, folderName, item['name']) localItem = client.resourceLookup(localPath, True) if localItem: client.delete('item/%s' % localItem['_id']) localItem = client.createItem(folder['_id'], item['name']) for remoteFile in remote.listFile(item['_id']): with tempfile.NamedTemporaryFile() as tf: fileName = tf.name tf.close() sys.stdout.write('Downloading %s' % remoteFile['name']) sys.stdout.flush() remote.downloadFile(remoteFile['_id'], fileName) sys.stdout.write(' .') sys.stdout.flush() client.uploadFileToItem(localItem['_id'], fileName, filename=remoteFile['name'], mimeType=remoteFile['mimeType']) sys.stdout.write('.\n') sys.stdout.flush() for item in list(client.listItem(folder['_id'])): if item['name'].endswith('.anot'): sys.stdout.write('Deleting %s\n' % item['name']) sys.stdout.flush() client.delete('item/%s' % item['_id']) continue if 'largeImage' not in item: sys.stdout.write('Making large_item %s ' % item['name']) sys.stdout.flush() job = client.post('item/%s/tiles' % item['_id']) if job is not None: job = wait_for_job(client, job) else: print('done') return folder
def saveAndUploadScene(self, username, api_key, api_url): sceneName = "Scene-" + strftime("%Y%m%d-%H%M%S") + ".mrb" from os.path import expanduser home = expanduser("~") sceneSaveFilename = os.path.join(home, sceneName) print 'saving scene to file: ' + sceneSaveFilename slicer.util.saveScene(sceneSaveFilename) print 'saving file done' print 'saving to Girder' patient_folder = '5a54e3b6210e501a6c95f73b' import girder_client gc = girder_client.GirderClient(apiUrl=api_url) gc.authenticate(username=username, apiKey=api_key) # create an item item = gc.createItem(parentFolderId=patient_folder, name=sceneName) # set some metadata on the item metadata = { 'patient_id': '0', 'acquisition_date': '2018-01-12' } gc.addMetadataToItem(item['_id'], metadata) # upload the file itself # 'example.zip' is a local file gc.uploadFileToItem(itemId=item['_id'], filepath=sceneSaveFilename)
def argvToList(x=sys.argv): """ Return a Full List of Items """ # Get commandLine to get full annotation list commandLen = len(x) if commandLen > 4: sys.exit('Command Line Argvs Error: { Url, User, Password }') else: url = x[1] user = x[2] password = x[3] url = url + '/api/v1' # Initialize Girder CLient instance gc = girder_client.GirderClient(apiUrl=url) # Authentication try: gc.authenticate(user, password) except requests.ConnectionError: sys.exit( 'Authentication Failed! Your url, username, and password might be wrong.' ) fullList = getFullList(gc) return gc, fullList
def testPatch(self): patchUrl = 'patch' patchRequest = { 'valid': False } # Test json request jsonBody = { 'foo': 'bar' } def _patchJson(url, request): body = request.body if isinstance(body, bytes): body = body.decode('utf8') patchRequest['valid'] = json.loads(body) == jsonBody return httmock.response(200, {}, {}, request=request) patch = httmock.urlmatch( path=r'^.*%s$' % patchUrl, method='PUT')(_patchJson) with httmock.HTTMock(patch): client = girder_client.GirderClient() client.put(patchUrl, json=jsonBody) # Check we got the right request self.assertTrue(patchRequest['valid']) # Now try raw message body patchRequest['valid'] = False rawBody = 'raw' def _patchRaw(url, request): patchRequest['valid'] = request.body == rawBody return httmock.response(200, {}, {}, request=request) patch = httmock.urlmatch( path=r'^.*%s$' % patchUrl, method='PUT')(_patchRaw) with httmock.HTTMock(patch): client = girder_client.GirderClient() client.put(patchUrl, data=rawBody) # Check we got the right request self.assertTrue(patchRequest['valid'])
def run_girder_init(apiUrl): createInitialUser(apiUrl) gc = girder_client.GirderClient(apiUrl=apiUrl) gc.authenticate("admin", "letmein") setGirderSetting("core.cors.allow_origin", "http://localhost:8080, http://localhost:9001", gc) createAssetstore(gc) createIngestFolders(gc)
def uploadToGirder(folderPath, girder_api_key, girder_parent_id): gc = girder_client.GirderClient(apiUrl=config.GIRDER_API) gc.authenticate(apiKey=girder_api_key) gc.upload(folderPath, girder_parent_id) ls = gc.listFolder(girder_parent_id, name=folderPath.split("/")[-1]) return list(ls)[0]
def login(): global token, mc, gc # TODO - not global token = pydas.login(email=MIDAS_LOGIN, api_key=MIDAS_API_KEY, url=MIDAS_URL) mc = pydas.session.communicator gc = girder_client.GirderClient(apiUrl=GIRDER_URL) gc.authenticate(username=GIRDER_LOGIN, apiKey=GIRDER_API_KEY)
def get_sample_data(adminUser, collName='Sample Images', folderName='Images'): """ As needed, download sample data. :param adminUser: a user to create and modify collections and folders. :param collName: the collection name where the data will be added. :param folderName: the folder name where the data will bed aded. :returns: the folder where the sample data is located. """ try: import girder_client except ImportError: logger.error('girder_client is unavailable. Cannot get sample data.') return from girder.models.item import Item from girder.models.upload import Upload from girder_large_image.models.image_item import ImageItem folder = get_collection_folder(adminUser, collName, folderName) remote = girder_client.GirderClient( apiUrl='https://data.kitware.com/api/v1') remoteFolder = remote.resourceLookup( '/collection/HistomicsTK/Deployment test images') sampleItems = [] for remoteItem in remote.listItem(remoteFolder['_id']): item = Item().findOne({ 'folderId': folder['_id'], 'name': remoteItem['name'] }) if item and len(list(Item().childFiles(item, limit=1))): sampleItems.append(item) continue if not item: item = Item().createItem(remoteItem['name'], creator=adminUser, folder=folder) for remoteFile in remote.listFile(remoteItem['_id']): with tempfile.NamedTemporaryFile() as tf: fileName = tf.name tf.close() logger.info('Downloading %s', remoteFile['name']) remote.downloadFile(remoteFile['_id'], fileName) Upload().uploadFromFile(open(fileName, 'rb'), os.path.getsize(fileName), name=remoteItem['name'], parentType='item', parent=item, user=adminUser) sampleItems.append(item) for item in sampleItems: if 'largeImage' not in item: logger.info('Making large_item %s', item['name']) try: ImageItem().createImageItem(item, createJob=False) except Exception: pass logger.info('done') return folder
def createGirderClient(api_url, api_key=None, username=None, password=None): gc = girder_client.GirderClient(apiUrl=api_url) if username and password: gc.authenticate(username=username, password=password) elif api_key: gc.authenticate(apiKey=api_key) return gc
def check_admin_login(apiUrl): gc = girder_client.GirderClient(apiUrl=apiUrl) try: gc.authenticate(ADMIN_USER, ADMIN_PASS) except girder_client.AuthenticationError: print('Cannot login admin, it probably does not exist') return False return True
def loadData(self, fold, set): entries = self.dataCSVFile.loc[(self.dataCSVFile["Fold"] == fold) & (self.dataCSVFile["Set"] == set)] images = [] imageLabels = [] numFilesWritten = 0 if "GirderID" in self.dataCSVFile.columns: LabelNames = self.dataCSVFile.columns[8:] for labelName in LabelNames: imageLabels[labelName] = [] if self.gClient == None: self.gClient = girder_client.GirderClient( apiUrl=entries["Girder_URL"][0]) userName = input("Username: "******"Password: "******"GirderID"][i] fileName = entries["FileName"][i] if not os.path.isfile(os.path.join(tempFileDir, fileName)): self.gClient.downloadItem(fileID, tempFileDir) numFilesWritten += 1 print(numFilesWritten) image = cv2.imread(os.path.join(tempFileDir, fileName), 0) resized = cv2.resize(image, (224, 224)) images.append(numpy.array(resized)) for labelName in LabelNames: imageLabels[labelName].append(entries[labelName][i]) else: for i in entries.index: #Read in the ultrasound image filePath = entries["Folder"][i] fileName = entries["FileName"][i] image = cv2.imread(os.path.join(filePath, fileName), 0) # print(os.path.join(filePath,fileName)) processed = self.process_ultrasound(image) images.append(numpy.array(processed)) #Read in the segmentation image fileName_seg = entries["VesselsCombined"][i] segmentation = cv2.imread(os.path.join(filePath, fileName_seg), 0) processed_seg = self.process_seg(segmentation) imageLabels.append(numpy.array(processed_seg)) return numpy.array(images), numpy.array(imageLabels)
def main(): # Use the API key to authenticate. key = os.environ.get('GIRDER_API_KEY') if key is None: print( 'Environment variable GIRDER_API_KEY is blank. Cannot upload files.', file=sys.stderr) return 1 gc = girder_client.GirderClient(host='data.kitware.com', scheme='https') # The api key needs scopes of at least # ["core.data.write","core.data.read","core.user_info.read"] gc.authenticate(apiKey=key) # Retrieve the target folder, which should be at ~/Public/Travis\ GeoJS user = gc.get('user/me') if user is None: print('No user logged in; API key may be bad.', file=sys.stderr) return 1 folder = gc.get('resource/lookup', parameters={'path': 'collection/GeoJS/Public'}) folder = gc.loadOrCreateFolder('Travis GeoJS', folder['_id'], 'folder') travis_build_number = os.environ.get('TRAVIS_BUILD_NUMBER') folderName = travis_build_number if os.environ.get('TRAVIS_EVENT_TYPE') == 'pull_request': folderName += ' PR #%s %s' % ( os.environ.get('TRAVIS_PULL_REQUEST'), os.environ.get('TRAVIS_PULL_REQUEST_BRANCH')) elif os.environ.get('TRAVIS_BRANCH'): folderName += ' ' + os.environ.get('TRAVIS_BRANCH') # folder = gc.loadOrCreateFolder(folderName, folder['_id'], 'folder') try: folder = six.next( gc.listFolder(folder['_id'], 'folder', name=folderName)) except StopIteration: folder = gc.createFolder(folder['_id'], folderName, os.environ.get('TRAVIS_COMMIT_MESSAGE', '')) # If we have a build matrix, this could be of use # travis_job_number = os.environ.get('TRAVIS_JOB_NUMBER') # folder = gc.loadOrCreateFolder(travis_job_number, folder['_id'], 'folder') # Upload the files specified on the command line, creating an item for each for fileName in sys.argv[1:]: (dirname, filename) = os.path.split(fileName) size = os.stat(fileName).st_size print(dirname, filename) with open(fileName, 'rb') as fd: gc.uploadFile(parentId=folder['_id'], stream=fd, name=filename, size=size, parentType='folder') print('uploaded')
def submit_results(user, csv_results, description=None): client = girder_client.GirderClient(apiUrl=GIRDER_URL) upload_challenge_data(client, user, 'prostatex', csv_results, metadata=description) print( "Done. The results of your submission will appear shortly on the leaderboard at http://ismi17.diagnijmegen.nl/" )
def __init__(self, config, aws_section='aws', girder_section='girder'): self.verbose = 0 self.config = config self.aws_section = aws_section self.girder_section = girder_section self.client = girder_client.GirderClient(apiUrl=self.girder_api_url) self.client.authenticate(self.girder_user, self.girder_password)
def _get_girder_client(self): # @note We import girder_client here, as only uploading requires it at present. # If `girder_client` can be imported via Bazel with minimal pain, then we can bubble # this up to the top-level. import girder_client if self._girder_client is None: self._girder_client = girder_client.GirderClient( apiUrl=self._api_url) self._girder_client.authenticate(apiKey=self._api_key) return self._girder_client
def connect_to_api(apiurl, apikey=None, interactive=True): """Connect to a specific girder API""" assert interactive or (apikey is not None) gc = girder_client.GirderClient(apiUrl=apiurl) if apikey is not None: interactive = False if interactive: gc.authenticate(interactive=True) else: gc.authenticate(apiKey=apikey) return gc
def __init__( self, api_key, root_id, root_type='collection', host='0.0.0.0', port=8080, client=None, ): # type: (str, str, str, str, int, Any) -> None ''' Constructs a GirderExporter instances and creates a Girder client. Args: api_key (str): Girder API key. root_id (str): ID of folder or collection under which all data will be exported. root_type (str, optional): Root entity type. Default: collection. Options: folder, collection host (str, optional): Docker host IP address. Default: 0.0.0.0. port (int, optional): Docker host port. Default: 8080. client (object, optional): Client instance, for testing. Default: None. Raises: DataError: If config is invalid. ''' # sudo ip addr show docker0 | grep inet | grep docker0 | awk '{print $2}' | sed 's/\/.*//' # will give you the ip address of the docker network which binds to # localhost config = dict( api_key=api_key, root_id=root_id, root_type=root_type, host=host, port=port, ) GirderConfig(config).validate() self._url = f'http://{host}:{port}/api/v1' # type: str if client is None: client = girder_client.GirderClient( apiUrl=self._url) # pragma: no cover client.authenticate(apiKey=api_key) # pragma: no cover self._client = client # type: Any self._root_id = root_id # type: str self._root_type = root_type # type: str
def testUploadCallbacks(self): callbackUser = self.model('user').createUser( firstName='Callback', lastName='Last', login='******', password='******', email='*****@*****.**') callbackPublicFolder = six.next( self.model('folder').childFolders(parentType='user', parent=callbackUser, user=None, limit=1)) callback_counts = {'folder': 0, 'item': 0} folders = {} items = {} folders[self.libTestDir] = False folder_count = 1 # 1 for self.libTestDir item_count = 0 for root, dirs, files in os.walk(self.libTestDir): for name in files: items[os.path.join(root, name)] = False item_count += 1 for name in dirs: folders[os.path.join(root, name)] = False folder_count += 1 def folder_callback(folder, filepath): self.assertIn(filepath, folders.keys()) folders[filepath] = True callback_counts['folder'] += 1 def item_callback(item, filepath): self.assertIn(filepath, items.keys()) items[filepath] = True callback_counts['item'] += 1 client = girder_client.GirderClient(port=os.environ['GIRDER_PORT']) client.authenticate('callback', 'password') client.add_folder_upload_callback(folder_callback) client.add_item_upload_callback(item_callback) client.upload(self.libTestDir, callbackPublicFolder['_id']) # make sure counts are the same (callbacks not called more than once) # and that all folders and files have callbacks called on them self.assertEqual(folder_count, callback_counts['folder']) self.assertEqual(item_count, callback_counts['item']) self.assertTrue(all(items.values())) self.assertTrue(all(folders.values()))
def parse_request_body(data): gc = girder_client.GirderClient(apiUrl=GIRDER_API_URL) gc.token = data['girder_token'] user = gc.get("/user/me") if user is None: logging.warn("Bad gider token") raise tornado.web.HTTPError(401, 'Failed to authenticate with girder') if data.get('taleId'): obj = gc.get('/tale/%s' % data['taleId']) elif data.get('instanceId'): obj = gc.get('/instance/%s' % data['instanceId']) else: obj = None # TODO: catch possible errors return gc, user, obj
def get_gc(gc=None, server="lemon"): global GIRDER_CLIENT global GIRDER_URL global GIRDER_KEY if server == "images": GIRDER_URL = 'https://images.slide-atlas.org' GIRDER_KEY = 'MJyaKIJxIkAb8l7OS7mffPE7QvB8H1WxDpyllzcG' if gc is None: if GIRDER_CLIENT is None: GIRDER_CLIENT = girder_client.GirderClient(apiUrl=GIRDER_URL + '/api/v1') GIRDER_CLIENT.authenticate(GIRDER_USERNAME, apiKey=GIRDER_KEY) gc = GIRDER_CLIENT return gc
def syncPatient(apiKey, destinationLocalFolder): # api Rest url of the warehouse url = 'http://humanheart-project.creatis.insa-lyon.fr/database/api/v1' # Generate the warehouse client gc = girder_client.GirderClient(apiUrl=url) # Authentication to the warehouse gc.authenticate(apiKey=apiKey) # download the delpel collection in local gc.downloadResource('5ef3090773e9f0055751d55d', destinationLocalFolder, resourceType='collection', sync=True)
def __init__(self, girder_api_url, username, password): """Autheticate with girder instance, prepare for interaction. param: girder_api_url: the url to the girder instance. param: username: the user name of the owner of the specific instance param: password: the password for the user name. """ self._client = girder_client.GirderClient(apiUrl=girder_api_url) self._client.authenticate(username, password) self._metadata = dict() self._local_path = None self._isfolder = False self._girder_dest_path = None self._client.addFolderUploadCallback(self.__upload_folder_callback) self._client.addItemUploadCallback(self.__upload_item_callback)
def upload_baselines(args): """ Upload the baseline image tarball to a girder instance. :param args: a dictionary of arguments, including: build: the build directory where the tarball is located. dest: the root url of the Girder instance. folder: the Girder folder ID to upload to. apikey: a Girder authentication token. Optional. username: a Girder username. Optional. password: a Girder username. Optional. verbose: the verbosity level. """ buildPath = os.path.abspath(os.path.expanduser(args.get('build'))) tarPath = os.path.join(buildPath, 'base-images.tgz') tarSize = os.path.getsize(tarPath) # Get the folder we want to upload to to ensure it exists apiRoot = args['dest'].rstrip('/') + '/api/v1/' gc = girder_client.GirderClient(apiUrl=apiRoot) if args.get('apikey'): gc.authenticate(apiKey=args.get('apikey')) elif args.get('username') and args.get('password'): gc.authenticate(username=args.get('username'), password=args.get('password')) else: gc.authenticate(username=args.get('username'), interactive=True) name = 'Baseline Images %s.tgz' % time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(tarPath))) uploadedFile = gc.uploadFile(parentId=args['folder'], parentType='folder', name=name, stream=open(tarPath, 'rb'), size=tarSize, mimeType='application/tar+gzip') if args['verbose'] >= 1: print('Upload to file %s' % uploadedFile['_id']) testDataPath = os.path.abspath('scripts/datastore.js') if not os.path.isfile(testDataPath): raise Exception('Cannot update test-data information.') sha512 = gc.getFile(uploadedFile['_id'])['sha512'] ds = open(testDataPath).read() start, rest = ds.split("'base-images.tgz': ", 1) rest, end = rest.split(',', 1) ds = start + ("'base-images.tgz': '%s'," % sha512) + end open(testDataPath, 'w').write(ds) if args['verbose'] >= 1: print('test-data references updated')