_id
stringlengths
2
7
title
stringlengths
1
88
partition
stringclasses
3 values
text
stringlengths
31
13.1k
language
stringclasses
1 value
meta_information
dict
q264400
MedicalImage.to_file
validation
def to_file(self, outpath): """Save this object instance in outpath. Parameters ---------- outpath: str Output file path """ if not self.has_mask() and not self.is_smoothed(): save_niigz(outpath, self.img)
python
{ "resource": "" }
q264401
setup_logging
validation
def setup_logging(log_config_file=op.join(op.dirname(__file__), 'logger.yml'), log_default_level=LOG_LEVEL, env_key=MODULE_NAME.upper() + '_LOG_CFG'): """Setup logging configuration.""" path = log_config_file value = os.getenv(env_key, None) if value: path = value if op.exists(path): log_cfg = yaml.load(read(path).format(MODULE_NAME)) logging.config.dictConfig(log_cfg)
python
{ "resource": "" }
q264402
get_3D_from_4D
validation
def get_3D_from_4D(filename, vol_idx=0): """Return a 3D volume from a 4D nifti image file Parameters ---------- filename: str Path to the 4D .mhd file vol_idx: int Index of the 3D volume to be extracted from the 4D volume. Returns ------- vol, hdr The data array and the new 3D image header. """ def remove_4th_element_from_hdr_string(hdr, fieldname): if fieldname in hdr: hdr[fieldname] = ' '.join(hdr[fieldname].split()[:3]) vol, hdr =
python
{ "resource": "" }
q264403
_safe_cache
validation
def _safe_cache(memory, func, **kwargs): """ A wrapper for mem.cache that flushes the cache if the version number of nibabel has changed. """ cachedir = memory.cachedir if cachedir is None or cachedir in __CACHE_CHECKED: return memory.cache(func, **kwargs) version_file = os.path.join(cachedir, 'module_versions.json') versions = dict() if os.path.exists(version_file): with open(version_file, 'r') as _version_file: versions = json.load(_version_file) modules = (nibabel, ) # Keep only the major + minor version numbers my_versions = dict((m.__name__, LooseVersion(m.__version__).version[:2]) for m in modules) commons = set(versions.keys()).intersection(set(my_versions.keys())) collisions = [m for m in commons if versions[m] != my_versions[m]] # Flush cache if version collision if len(collisions) > 0: if nilearn.CHECK_CACHE_VERSION: warnings.warn("Incompatible cache in %s: " "different version of nibabel. Deleting " "the cache. Put nilearn.CHECK_CACHE_VERSION " "to false to avoid this behavior." % cachedir) try: tmp_dir = (os.path.split(cachedir)[:-1] + ('old_%i' % os.getpid(), )) tmp_dir = os.path.join(*tmp_dir) # We use rename + unlink to be more robust to race # conditions
python
{ "resource": "" }
q264404
spatialimg_to_hdfgroup
validation
def spatialimg_to_hdfgroup(h5group, spatial_img): """Saves a Nifti1Image into an HDF5 group. Parameters ---------- h5group: h5py Group Output HDF5 file path spatial_img: nibabel SpatialImage Image to be saved h5path: str HDF5 group path where the image data will be saved. Datasets will be created inside the given group path: 'data', 'extra', 'affine', the header information will be set as attributes of the 'data' dataset. """ try: h5group['data'] = spatial_img.get_data() h5group['affine'] = spatial_img.get_affine()
python
{ "resource": "" }
q264405
spatialimg_to_hdfpath
validation
def spatialimg_to_hdfpath(file_path, spatial_img, h5path=None, append=True): """Saves a Nifti1Image into an HDF5 file. Parameters ---------- file_path: string Output HDF5 file path spatial_img: nibabel SpatialImage Image to be saved h5path: string HDF5 group path where the image data will be saved. Datasets will be created inside the given group path: 'data', 'extra', 'affine', the header information will be set as attributes of the 'data' dataset. Default: '/img' append: bool True if you don't want to erase the content of the file if it already exists, False otherwise. Note ---- HDF5 open modes >>> 'r' Readonly, file must exist
python
{ "resource": "" }
q264406
get_nifti1hdr_from_h5attrs
validation
def get_nifti1hdr_from_h5attrs(h5attrs): """Transforms an H5py Attributes set to a dict. Converts unicode string keys into standard strings and each value into a numpy array. Parameters ---------- h5attrs: H5py Attributes Returns --------
python
{ "resource": "" }
q264407
all_childnodes_to_nifti1img
validation
def all_childnodes_to_nifti1img(h5group): """Returns in a list all images found under h5group. Parameters ---------- h5group: h5py.Group HDF group Returns ------- list of nifti1Image """ child_nodes = [] def append_parent_if_dataset(name, obj): if isinstance(obj, h5py.Dataset): if name.split('/')[-1] == 'data':
python
{ "resource": "" }
q264408
insert_volumes_in_one_dataset
validation
def insert_volumes_in_one_dataset(file_path, h5path, file_list, newshape=None, concat_axis=0, dtype=None, append=True): """Inserts all given nifti files from file_list into one dataset in fname. This will not check if the dimensionality of all files match. Parameters ---------- file_path: string HDF5 file path h5path: string file_list: list of strings newshape: tuple or lambda function If None, it will not reshape the images. If a lambda function, this lambda will receive only the shape array. e.g., newshape = lambda x: (np.prod(x[0:3]), x[3]) If a tuple, it will try to reshape all the images with the same shape. It must work for all the images in file_list. concat_axis: int Axis of concatenation after reshaping dtype: data type Dataset data type If not set, will use the type of the first file. append: bool Raises ------ ValueError if concat_axis is bigger than data dimensionality. Note ---- For now, this only works if the dataset ends up being a 2D matrix. I haven't tested for multi-dimensionality concatenations. """ def isalambda(v): return isinstance(v, type(lambda: None)) and v.__name__ == '<lambda>' mode = 'w' if os.path.exists(file_path): if append: mode = 'a' #loading the metadata into spatialimages imgs = [nib.load(vol) for vol in file_list] #getting the shapes of all volumes shapes = [np.array(img.get_shape()) for img in imgs] #getting the reshaped shapes if newshape is not None: if isalambda(newshape): nushapes = np.array([newshape(shape) for shape in shapes]) else: nushapes = np.array([shape for shape in shapes]) #checking if concat_axis is available in this new shapes for nushape in nushapes: assert(len(nushape) - 1 < concat_axis) #calculate the shape of the new dataset n_dims = nushapes.shape[1] ds_shape = np.zeros(n_dims, dtype=np.int) for a in list(range(n_dims)): if a == concat_axis: ds_shape[a] = np.sum(nushapes[:, concat_axis]) else: ds_shape[a] = np.max(nushapes[:, a]) #get the type of the new dataset #dtypes = [img.get_data_dtype() for img in imgs] if dtype is None: dtype = imgs[0].get_data_dtype() with h5py.File(file_path, mode) as f: try: ic = 0 h5grp = f.create_group(os.path.dirname(h5path)) h5ds = h5grp.create_dataset(os.path.basename(h5path),
python
{ "resource": "" }
q264409
treefall
validation
def treefall(iterable): """ Generate all combinations of the elements of iterable and its subsets. Parameters ---------- iterable: list, set or dict or any iterable object Returns ------- A generator of all possible combinations of the iterable. Example: ------- >>> for i in treefall([1, 2, 3, 4, 5]): print(i) >>> (1, 2,
python
{ "resource": "" }
q264410
get_reliabledictionary_list
validation
def get_reliabledictionary_list(client, application_name, service_name): """List existing reliable dictionaries. List existing reliable dictionaries and respective schema for given application and service. :param application_name: Name of the application. :type application_name: str :param service_name: Name of the service. :type service_name: str """
python
{ "resource": "" }
q264411
get_reliabledictionary_schema
validation
def get_reliabledictionary_schema(client, application_name, service_name, dictionary_name, output_file=None): """Query Schema information for existing reliable dictionaries. Query Schema information existing reliable dictionaries for given application and service. :param application_name: Name of the application. :type application_name: str :param service_name: Name of the service. :type service_name: str :param dictionary: Name of the reliable dictionary. :type dictionary: str :param output_file: Optional file to save the schema. """ cluster =
python
{ "resource": "" }
q264412
query_reliabledictionary
validation
def query_reliabledictionary(client, application_name, service_name, dictionary_name, query_string, partition_key=None, partition_id=None, output_file=None): """Query existing reliable dictionary. Query existing reliable dictionaries for given application and service. :param application_name: Name of the application. :type application_name: str :param service_name: Name of the service. :type service_name: str :param dictionary_name: Name of the reliable dictionary. :type dictionary_name: str :param query_string: An OData query string. For example $top=10. Check https://www.odata.org/documentation/ for more information. :type query_string: str :param partition_key: Optional partition key of the desired partition, either a string if named schema or int if Int64 schema :type partition_id: str :param partition_id: Optional partition GUID of the owning reliable dictionary. :type partition_id: str :param output_file: Optional file to save the schema. """ cluster = Cluster.from_sfclient(client) dictionary = cluster.get_application(application_name).get_service(service_name).get_dictionary(dictionary_name)
python
{ "resource": "" }
q264413
execute_reliabledictionary
validation
def execute_reliabledictionary(client, application_name, service_name, input_file): """Execute create, update, delete operations on existing reliable dictionaries. carry out create, update and delete operations on existing reliable dictionaries for given application and service. :param application_name: Name of the application. :type application_name: str :param service_name: Name of the service. :type service_name: str :param output_file: input file with list of json to provide the operation information
python
{ "resource": "" }
q264414
select_arg_verify
validation
def select_arg_verify(endpoint, cert, key, pem, ca, aad, no_verify): #pylint: disable=invalid-name,too-many-arguments """Verify arguments for select command""" if not (endpoint.lower().startswith('http') or endpoint.lower().startswith('https')): raise CLIError('Endpoint must be HTTP or HTTPS') usage = ('Valid syntax : --endpoint [ [ --key --cert | --pem | --aad] ' '[ --ca | --no-verify ] ]') if ca and not (pem or all([key, cert])): raise CLIError(usage) if no_verify and not (pem or all([key, cert]) or aad):
python
{ "resource": "" }
q264415
get_aad_token
validation
def get_aad_token(endpoint, no_verify): #pylint: disable-msg=too-many-locals """Get AAD token""" from azure.servicefabric.service_fabric_client_ap_is import ( ServiceFabricClientAPIs ) from sfctl.auth import ClientCertAuthentication from sfctl.config import set_aad_metadata auth = ClientCertAuthentication(None, None, no_verify) client = ServiceFabricClientAPIs(auth, base_url=endpoint) aad_metadata = client.get_aad_metadata() if aad_metadata.type != "aad": raise CLIError("Not AAD cluster") aad_resource = aad_metadata.metadata tenant_id = aad_resource.tenant authority_uri = aad_resource.login + '/' + tenant_id context = adal.AuthenticationContext(authority_uri,
python
{ "resource": "" }
q264416
_openpyxl_read_xl
validation
def _openpyxl_read_xl(xl_path: str): """ Use openpyxl to read an Excel file. """
python
{ "resource": "" }
q264417
_check_xl_path
validation
def _check_xl_path(xl_path: str): """ Return the expanded absolute path of `xl_path` if if exists and 'xlrd' or 'openpyxl' depending on which module should be used for the Excel file in `xl_path`. Parameters ---------- xl_path: str Path to an Excel file Returns ------- xl_path: str User expanded and absolute path to `xl_path` module: str The name of the module you should use to process the Excel file. Choices: 'xlrd', 'pyopenxl' Raises ------ IOError
python
{ "resource": "" }
q264418
read_xl
validation
def read_xl(xl_path: str): """ Return the workbook from the Excel file in `xl_path`."""
python
{ "resource": "" }
q264419
get_sheet_list
validation
def get_sheet_list(xl_path: str) -> List: """Return a list with the name of the sheets in the Excel file in `xl_path`. """ wb = read_xl(xl_path)
python
{ "resource": "" }
q264420
concat_sheets
validation
def concat_sheets(xl_path: str, sheetnames=None, add_tab_names=False): """ Return a pandas DataFrame with the concat'ed content of the `sheetnames` from the Excel file in `xl_path`. Parameters ---------- xl_path: str Path to the Excel file sheetnames: list of str List of existing sheet names of `xl_path`. If None, will use all sheets from `xl_path`. add_tab_names: bool If True will add a 'Tab' column which says from which tab the row comes from. Returns ------- df: pandas.DataFrame """ xl_path, choice
python
{ "resource": "" }
q264421
_check_cols
validation
def _check_cols(df, col_names): """ Raise an AttributeError if `df` does not have a column named as an item of the list of strings `col_names`. """ for col in
python
{ "resource": "" }
q264422
col_values
validation
def col_values(df, col_name): """ Return a list of not null values from the `col_name` column of `df`.""" _check_cols(df, [col_name]) if 'O' in df[col_name] or pd.np.issubdtype(df[col_name].dtype, str): # if the column is of strings return
python
{ "resource": "" }
q264423
duplicated_rows
validation
def duplicated_rows(df, col_name): """ Return a DataFrame with the duplicated values of the column `col_name` in `df`.""" _check_cols(df, [col_name])
python
{ "resource": "" }
q264424
duplicated
validation
def duplicated(values: Sequence): """ Return the duplicated items in `values`""" vals =
python
{ "resource": "" }
q264425
_to_string
validation
def _to_string(data): """ Convert to string all values in `data`. Parameters ---------- data: dict[str]->object Returns ------- string_data: dict[str]->str """ sdata = data.copy() for k, v in data.items(): if isinstance(v, datetime):
python
{ "resource": "" }
q264426
search_unique
validation
def search_unique(table, sample, unique_fields=None): """ Search for items in `table` that have the same field sub-set values as in `sample`. Expecting it to be unique, otherwise will raise an exception. Parameters ---------- table: tinydb.table sample: dict Sample data Returns ------- search_result: tinydb.database.Element Unique item result of the search. Raises ------ KeyError: If the search returns for more than one entry. """ if unique_fields is None: unique_fields = list(sample.keys()) query
python
{ "resource": "" }
q264427
find_unique
validation
def find_unique(table, sample, unique_fields=None): """Search in `table` an item with the value of the `unique_fields` in the `sample` sample. Check if the the obtained result is unique. If nothing is found will return an empty list, if there is more than one item found, will raise an IndexError. Parameters ---------- table: tinydb.table sample: dict Sample data unique_fields: list of str Name of fields (keys) from `data` which are going to be used to build a sample to look for exactly the same values in the database. If None, will use every key in `data`.
python
{ "resource": "" }
q264428
_query_sample
validation
def _query_sample(sample, operators='__eq__'): """Create a TinyDB query that looks for items that have each field in `sample` with a value compared with the correspondent operation in `operators`. Parameters ---------- sample: dict The sample data operators: str or list of str A list of comparison operations for each field value in `sample`. If this is a str, will use the same operator for all `sample` fields. If you want different operators for each field, remember to use an OrderedDict for `sample`. Check TinyDB.Query class for possible choices. Returns ------- query: tinydb.database.Query """ if isinstance(operators, str): operators = [operators] * len(sample) if len(sample) != len(operators):
python
{ "resource": "" }
q264429
_query_data
validation
def _query_data(data, field_names=None, operators='__eq__'): """Create a tinyDB Query object that looks for items that confirms the correspondent operator from `operators` for each `field_names` field values from `data`. Parameters ---------- data: dict The data sample field_names: str or list of str The name of the fields in `data` that will be used for the query. operators: str or list of str A list of comparison operations for each field value in `field_names`. If this is a str, will use the same operator for all `field_names`. If you want different operators for each field, remember to use an OrderedDict for `data`. Check TinyDB.Query class for possible choices. Returns
python
{ "resource": "" }
q264430
_concat_queries
validation
def _concat_queries(queries, operators='__and__'): """Create a tinyDB Query object that is the concatenation of each query in `queries`. The concatenation operator is taken from `operators`. Parameters ---------- queries: list of tinydb.Query The list of tinydb.Query to be joined. operators: str or list of str List of binary operators to join `queries` into one query. Check TinyDB.Query class for possible choices. Returns ------- query: tinydb.database.Query """ # checks first if not queries: raise ValueError('Expected some `queries`, got {}.'.format(queries)) if len(queries) == 1: return queries[0] if isinstance(operators, str): operators = [operators] * (len(queries) - 1) if
python
{ "resource": "" }
q264431
PetitDB.search_by_eid
validation
def search_by_eid(self, table_name, eid): """Return the element in `table_name` with Object ID `eid`. If None is found will raise a KeyError exception. Parameters ---------- table_name: str The name of the table to look in. eid: int The Object ID of the element to look for. Returns ------- elem: tinydb.database.Element Raises ------
python
{ "resource": "" }
q264432
PetitDB.search_unique
validation
def search_unique(self, table_name, sample, unique_fields=None): """ Search in `table` an item with the value of the `unique_fields` in the `data` sample. Check if the the obtained result is unique. If nothing is found will return an empty list, if there is more than one item found, will raise an IndexError. Parameters ---------- table_name: str sample: dict Sample data unique_fields:
python
{ "resource": "" }
q264433
PetitDB.is_unique
validation
def is_unique(self, table_name, sample, unique_fields=None): """Return True if an item with the value of `unique_fields` from `data` is unique in the table with `table_name`. False if no sample is found or more than one is found. See function `find_unique` for more details. Parameters ---------- table_name: str sample: dict Sample data for query unique_fields: str or list of str Returns
python
{ "resource": "" }
q264434
PetitDB.update_unique
validation
def update_unique(self, table_name, fields, data, cond=None, unique_fields=None, *, raise_if_not_found=False): """Update the unique matching element to have a given set of fields. Parameters ---------- table_name: str fields: dict or function[dict -> None] new data/values to insert into the unique element or a method that will update the elements. data: dict Sample data for query cond: tinydb.Query
python
{ "resource": "" }
q264435
PetitDB.count
validation
def count(self, table_name, sample): """Return the number of items that match the `sample` field values in table `table_name`.
python
{ "resource": "" }
q264436
is_img
validation
def is_img(obj): """ Check for get_data and get_affine method in an object Parameters ---------- obj: any object Tested object Returns ------- is_img: boolean True if get_data and get_affine methods are present and callable, False otherwise. """ try: get_data
python
{ "resource": "" }
q264437
get_data
validation
def get_data(img): """Get the data in the image without having a side effect on the Nifti1Image object Parameters ---------- img: Nifti1Image Returns ------- np.ndarray
python
{ "resource": "" }
q264438
get_shape
validation
def get_shape(img): """Return the shape of img. Paramerers ----------- img: Returns
python
{ "resource": "" }
q264439
check_img_compatibility
validation
def check_img_compatibility(one_img, another_img, only_check_3d=False): """Return true if one_img and another_img have the same shape. False otherwise. If both are nibabel.Nifti1Image will also check for affine matrices. Parameters ---------- one_img: nibabel.Nifti1Image or np.ndarray another_img: nibabel.Nifti1Image or np.ndarray only_check_3d: bool If True will check only the 3D part of the affine matrices when they have more dimensions. Raises ------ NiftiFilesNotCompatible """ nd_to_check = None if only_check_3d: nd_to_check = 3 if hasattr(one_img, 'shape') and hasattr(another_img, 'shape'): if not have_same_shape(one_img, another_img, nd_to_check=nd_to_check): msg = 'Shape of the first image: \n{}\n is different from second one: \n{}'.format(one_img.shape,
python
{ "resource": "" }
q264440
have_same_affine
validation
def have_same_affine(one_img, another_img, only_check_3d=False): """Return True if the affine matrix of one_img is close to the affine matrix of another_img. False otherwise. Parameters ---------- one_img: nibabel.Nifti1Image another_img: nibabel.Nifti1Image only_check_3d: bool If True will extract only the 3D part of the affine matrices when they have more dimensions. Returns ------- bool Raises ------ ValueError """ img1 = check_img(one_img) img2 = check_img(another_img) ndim1 = len(img1.shape) ndim2 = len(img2.shape) if ndim1 < 3: raise ValueError('Image {} has only {} dimensions, at least 3 dimensions is expected.'.format(repr_imgs(img1), ndim1)) if ndim2
python
{ "resource": "" }
q264441
repr_imgs
validation
def repr_imgs(imgs): """Printing of img or imgs""" if isinstance(imgs, string_types): return imgs if isinstance(imgs, collections.Iterable): return '[{}]'.format(', '.join(repr_imgs(img) for img in imgs)) # try get_filename try: filename = imgs.get_filename() if filename is not None: img_str = "{}('{}')".format(imgs.__class__.__name__, filename) else: img_str = "{}(shape={}, affine={})".format(imgs.__class__.__name__,
python
{ "resource": "" }
q264442
have_same_shape
validation
def have_same_shape(array1, array2, nd_to_check=None): """ Returns true if array1 and array2 have the same shapes, false otherwise. Parameters ---------- array1: numpy.ndarray array2: numpy.ndarray nd_to_check: int Number of the dimensions to check, i.e., if == 3 then will check only the 3 first numbers of array.shape. Returns ------- bool """ shape1 = array1.shape shape2 = array2.shape if nd_to_check is not None: if len(shape1) < nd_to_check: msg = 'Number of dimensions to check {} is out of bounds for the shape of the first image: \n{}\n.'.format(shape1)
python
{ "resource": "" }
q264443
dir_match
validation
def dir_match(regex, wd=os.curdir): """Create a list of regex matches that result from the match_regex of all file names within wd. The list of files will have wd as path prefix. @param regex: string
python
{ "resource": "" }
q264444
recursive_dir_match
validation
def recursive_dir_match(folder_path, regex=''): """ Returns absolute paths of folders that match the regex within folder_path and all its children folders. Note: The regex matching is done using the match function
python
{ "resource": "" }
q264445
get_file_list
validation
def get_file_list(file_dir, regex=''): """ Creates a list of files that match the search_regex within file_dir. The list of files will have file_dir as path prefix. Parameters ---------- @param file_dir: @param search_regex: Returns: -------- List of paths to files that match the search_regex """
python
{ "resource": "" }
q264446
recursive_find_search
validation
def recursive_find_search(folder_path, regex=''): """ Returns absolute paths of files that match the regex within file_dir and all its children folders. Note: The regex matching is done using the search function of
python
{ "resource": "" }
q264447
iter_recursive_find
validation
def iter_recursive_find(folder_path, *regex): """ Returns absolute paths of files that match the regexs within folder_path and all its children folders. This is an iterator function that will use yield to return each set of file_paths in one iteration. Will only return value if all the strings in regex match a file name. Note: The regex matching is done using the search function of the re module. Parameters ---------- folder_path: string regex: strings Returns ------- A list of strings. """ for root, dirs, files in
python
{ "resource": "" }
q264448
get_all_files
validation
def get_all_files(folder): """ Generator that loops through all absolute paths of the files within folder Parameters ---------- folder: str Root folder start
python
{ "resource": "" }
q264449
recursive_glob
validation
def recursive_glob(base_directory, regex=''): """ Uses glob to find all files or folders that match the regex starting from the base_directory. Parameters ---------- base_directory: str regex: str Returns ------- files: list """ files
python
{ "resource": "" }
q264450
compose_err_msg
validation
def compose_err_msg(msg, **kwargs): """Append key-value pairs to msg, for display. Parameters ---------- msg: string arbitrary message kwargs: dict arbitrary dictionary Returns ------- updated_msg: string msg, with "key: value" appended. Only string values are appended. Example ------- >>> compose_err_msg('Error message with arguments...', arg_num=123, \ arg_str='filename.nii', arg_bool=True)
python
{ "resource": "" }
q264451
group_dicom_files
validation
def group_dicom_files(dicom_file_paths, header_fields): """ Gets a list of DICOM file absolute paths and returns a list of lists of DICOM file paths. Each group contains a set of DICOM files that have exactly the same headers. Parameters ---------- dicom_file_paths: list of str List or set of DICOM file paths header_fields: list of str List of header field names to check on the comparisons of the DICOM files. Returns ------- dict of DicomFileSets The key is one filepath representing the group (the first found). """ dist = SimpleDicomFileDistance(field_weights=header_fields) path_list = dicom_file_paths.copy() path_groups = DefaultOrderedDict(DicomFileSet) while len(path_list) > 0: file_path1 = path_list.pop()
python
{ "resource": "" }
q264452
copy_groups_to_folder
validation
def copy_groups_to_folder(dicom_groups, folder_path, groupby_field_name): """Copy the DICOM file groups to folder_path. Each group will be copied into a subfolder with named given by groupby_field. Parameters ---------- dicom_groups: boyle.dicom.sets.DicomFileSet folder_path: str Path to where copy the DICOM files. groupby_field_name: str DICOM field name. Will get the value of this field to name the group folder. """ if dicom_groups is None or not dicom_groups: raise ValueError('Expected a boyle.dicom.sets.DicomFileSet.') if not os.path.exists(folder_path):
python
{ "resource": "" }
q264453
calculate_file_distances
validation
def calculate_file_distances(dicom_files, field_weights=None, dist_method_cls=None, **kwargs): """ Calculates the DicomFileDistance between all files in dicom_files, using an weighted Levenshtein measure between all field names in field_weights and their corresponding weights. Parameters ---------- dicom_files: iterable of str Dicom file paths field_weights: dict of str to float A dict with header field names to float scalar values, that indicate a distance measure ratio for the levenshtein distance averaging of all the header field names in it. e.g., {'PatientID': 1} dist_method_cls: DicomFileDistance class Distance method object to compare the files. If None, the default DicomFileDistance method using Levenshtein distance between the field_wieghts will be used. kwargs: DicomFileDistance instantiation named arguments Apart from the field_weitghts argument. Returns ------- file_dists: np.ndarray or scipy.sparse.lil_matrix of shape NxN Levenshtein distances between each of the N items in dicom_files. """ if dist_method_cls is None: dist_method = LevenshteinDicomFileDistance(field_weights) else: try: dist_method = dist_method_cls(field_weights=field_weights, **kwargs) except: log.exception('Could not instantiate {} object
python
{ "resource": "" }
q264454
SimpleDicomFileDistance.transform
validation
def transform(self): """Check the field values in self.dcmf1 and self.dcmf2 and returns True if all the field values are the same, False otherwise. Returns ------- bool
python
{ "resource": "" }
q264455
DicomFilesClustering.levenshtein_analysis
validation
def levenshtein_analysis(self, field_weights=None): """ Updates the status of the file clusters comparing the cluster key files with a levenshtein weighted measure using either the header_fields or self.header_fields. Parameters ---------- field_weights: dict of strings with floats A dict with header field names to float scalar values, that indicate a distance measure ratio for the levenshtein distance averaging of all the header field names in it. e.g., {'PatientID': 1} """ if field_weights is None:
python
{ "resource": "" }
q264456
DicomFilesClustering.dist_percentile_threshold
validation
def dist_percentile_threshold(dist_matrix, perc_thr=0.05, k=1): """Thresholds a distance matrix and returns the result. Parameters ---------- dist_matrix: array_like Input array or object that can be converted to an array. perc_thr: float in range of [0,100] Percentile to compute which must be between 0 and 100 inclusive. k: int, optional Diagonal above which to zero elements. k = 0 (the default) is the main diagonal, k < 0 is below it and k > 0 is above.
python
{ "resource": "" }
q264457
DicomFilesClustering.get_groups_in_same_folder
validation
def get_groups_in_same_folder(self, folder_depth=3): """ Returns a list of 2-tuples with pairs of dicom groups that are in the same folder within given depth. Parameters ---------- folder_depth: int Path depth to check for folder equality. Returns ------- list of tuples of str """ group_pairs = [] key_dicoms = list(self.dicom_groups.keys()) idx = len(key_dicoms) while idx > 0:
python
{ "resource": "" }
q264458
DicomFilesClustering.merge_groups
validation
def merge_groups(self, indices): """Extend the lists within the DICOM groups dictionary. The indices will indicate which list have to be extended by which other list. Parameters ---------- indices: list or tuple of 2 iterables of int, bot having the same len The indices of the lists that have to be merged, both iterables items will be read pair by pair, the first is the index to the list that will be extended with the list of the second index. The indices can be constructed with Numpy e.g.,
python
{ "resource": "" }
q264459
DicomFilesClustering.move_to_folder
validation
def move_to_folder(self, folder_path, groupby_field_name=None): """Copy the file groups to folder_path. Each group will be copied into a subfolder with named given by groupby_field. Parameters ---------- folder_path: str Path to where copy the DICOM files.
python
{ "resource": "" }
q264460
DicomFilesClustering.get_unique_field_values_per_group
validation
def get_unique_field_values_per_group(self, field_name, field_to_use_as_key=None): """Return a dictionary where the key is the group key file path and the values are sets of unique values of the field name of all DICOM files in the group. Parameters ---------- field_name: str Name of the field to read from all files field_to_use_as_key: str Name of the field to get the value and use as key. If None, will use the same key as the dicom_groups. Returns ------- Dict of sets """ unique_vals = DefaultOrderedDict(set) for dcmg in self.dicom_groups: for f in self.dicom_groups[dcmg]: field_val = DicomFile(f).get_attributes(field_name) key_val = dcmg if field_to_use_as_key is not None:
python
{ "resource": "" }
q264461
get_config_value
validation
def get_config_value(name, fallback=None): """Gets a config by name. In the case where the config name is not found, will use fallback value."""
python
{ "resource": "" }
q264462
get_config_bool
validation
def get_config_bool(name): """Checks if a config value is set to a valid bool value."""
python
{ "resource": "" }
q264463
set_config_value
validation
def set_config_value(name, value): """Set a config by name to a value.""" cli_config =
python
{ "resource": "" }
q264464
cert_info
validation
def cert_info(): """Path to certificate related files, either a single file path or a tuple. In the case of no security, returns None.""" sec_type = security_type() if sec_type == 'pem':
python
{ "resource": "" }
q264465
set_aad_cache
validation
def set_aad_cache(token, cache): """Set AAD token cache.""" set_config_value('aad_token', jsonpickle.encode(token))
python
{ "resource": "" }
q264466
set_aad_metadata
validation
def set_aad_metadata(uri, resource, client): """Set AAD metadata.""" set_config_value('authority_uri', uri)
python
{ "resource": "" }
q264467
set_auth
validation
def set_auth(pem=None, cert=None, key=None, aad=False): """Set certificate usage paths""" if any([cert, key]) and pem: raise ValueError('Cannot specify both pem and cert or key') if any([cert, key]) and not all([cert, key]): raise ValueError('Must specify both cert and key') if pem: set_config_value('security', 'pem') set_config_value('pem_path', pem)
python
{ "resource": "" }
q264468
filter_objlist
validation
def filter_objlist(olist, fieldname, fieldval): """ Returns a list with of the objects in olist that have a fieldname valued as fieldval
python
{ "resource": "" }
q264469
is_valid_regex
validation
def is_valid_regex(string): """ Checks whether the re module can compile the given regular expression. Parameters ---------- string: str Returns ------- boolean """ try:
python
{ "resource": "" }
q264470
is_fnmatch_regex
validation
def is_fnmatch_regex(string): """ Returns True if the given string is considered a fnmatch regular expression, False otherwise. It will look for :param string: str """ is_regex = False regex_chars = ['!', '*',
python
{ "resource": "" }
q264471
where_is
validation
def where_is(strings, pattern, n=1, lookup_func=re.match): """Return index of the nth match found of pattern in strings Parameters ---------- strings: list of str List of strings pattern: str Pattern to be matched nth: int Number of times the match must happen to return the item index.
python
{ "resource": "" }
q264472
generate_config
validation
def generate_config(output_directory): """ Generate a dcm2nii configuration file that disable the interactive mode. """ if not op.isdir(output_directory): os.makedirs(output_directory) config_file = op.join(output_directory, "config.ini")
python
{ "resource": "" }
q264473
call_dcm2nii
validation
def call_dcm2nii(work_dir, arguments=''): """Converts all DICOM files within `work_dir` into one or more NifTi files by calling dcm2nii on this folder. Parameters ---------- work_dir: str Path to the folder that contain the DICOM files arguments: str String containing all the flag arguments for `dcm2nii` CLI. Returns ------- sys_code: int
python
{ "resource": "" }
q264474
convert_dcm2nii
validation
def convert_dcm2nii(input_dir, output_dir, filename): """ Call MRICron's `dcm2nii` to convert the DICOM files inside `input_dir` to Nifti and save the Nifti file in `output_dir` with a `filename` prefix. Parameters ---------- input_dir: str Path to the folder that contains the DICOM files output_dir: str Path to the folder where to save the NifTI file filename: str Output file basename Returns ------- filepaths: list of str List of file paths created in `output_dir`. """ # a few checks before doing the job if not op.exists(input_dir): raise IOError('Expected an existing folder in {}.'.format(input_dir)) if not op.exists(output_dir): raise IOError('Expected an existing output folder in {}.'.format(output_dir)) # create a temporary folder for dcm2nii export tmpdir = tempfile.TemporaryDirectory(prefix='dcm2nii_') # call dcm2nii arguments = '-o "{}" -i y'.format(tmpdir.name) try: call_out = call_dcm2nii(input_dir, arguments) except: raise else: log.info('Converted "{}" to nifti.'.format(input_dir)) # get the filenames of the files that dcm2nii produced filenames = glob(op.join(tmpdir.name, '*.nii*')) # cleanup `filenames`, using only the post-processed (reoriented, cropped, etc.) images by dcm2nii cleaned_filenames = remove_dcm2nii_underprocessed(filenames) # copy files to the output_dir
python
{ "resource": "" }
q264475
remove_dcm2nii_underprocessed
validation
def remove_dcm2nii_underprocessed(filepaths): """ Return a subset of `filepaths`. Keep only the files that have a basename longer than the others with same suffix. This works based on that dcm2nii appends a preffix character for each processing step it does automatically in the DICOM to NifTI conversion. Parameters ---------- filepaths: iterable of str Returns ------- cleaned_paths: iterable of str """ cln_flist = [] # sort them by size len_sorted = sorted(filepaths, key=len) for idx, fpath in enumerate(len_sorted): remove = False # get the basename and the rest of the files
python
{ "resource": "" }
q264476
dictify
validation
def dictify(a_named_tuple): """Transform a named tuple into
python
{ "resource": "" }
q264477
merge_dict_of_lists
validation
def merge_dict_of_lists(adict, indices, pop_later=True, copy=True): """Extend the within a dict of lists. The indices will indicate which list have to be extended by which other list. Parameters ---------- adict: OrderedDict An ordered dictionary of lists indices: list or tuple of 2 iterables of int, bot having the same length The indices of the lists that have to be merged, both iterables items will be read pair by pair, the first is the index to the list that will be extended with the list of the second index. The indices can be constructed with Numpy e.g., indices = np.where(square_matrix) pop_later: bool If True will oop out the lists that are indicated in the second list of indices. copy: bool If True will perform a deep copy of the input adict before modifying it, hence not changing the original input. Returns ------- Dictionary of lists Raises ------ IndexError If the indices are out of range
python
{ "resource": "" }
q264478
append_dict_values
validation
def append_dict_values(list_of_dicts, keys=None): """ Return a dict of lists from a list of dicts with the same keys. For each dict in list_of_dicts with look for the values of the given keys and append it to the output dict. Parameters ---------- list_of_dicts: list of dicts keys: list of str List of keys to create in the output dict If None will use all keys in the first element of list_of_dicts Returns ------- DefaultOrderedDict of lists """
python
{ "resource": "" }
q264479
import_pyfile
validation
def import_pyfile(filepath, mod_name=None): """ Imports the contents of filepath as a Python module. :param filepath: string :param mod_name: string Name of the module when imported :return: module Imported module """ import sys if sys.version_info.major == 3: import importlib.machinery
python
{ "resource": "" }
q264480
copy
validation
def copy(configfile='', destpath='', overwrite=False, sub_node=''): """Copies the files in the built file tree map to despath. :param configfile: string Path to the FileTreeMap config file :param destpath: string Path to the files destination :param overwrite: bool Overwrite files if they already exist. :param sub_node: string Tree map configuration sub path. Will copy only the contents within this sub-node """ log.info('Running {0} {1} {2}'.format(os.path.basename(__file__), whoami(), locals())) assert(os.path.isfile(configfile)) if os.path.exists(destpath):
python
{ "resource": "" }
q264481
convert_sav
validation
def convert_sav(inputfile, outputfile=None, method='rpy2', otype='csv'): """ Transforms the input .sav SPSS file into other format. If you don't specify an outputfile, it will use the inputfile and change its extension to .csv """ assert(os.path.isfile(inputfile)) assert(method=='rpy2' or method=='savread') if method == 'rpy2': df = sav_to_pandas_rpy2(inputfile) elif method == 'savread': df = sav_to_pandas_savreader(inputfile) otype_exts = {'csv': '.csv', 'hdf': '.h5', 'stata': '.dta', 'json': '.json', 'pickle': '.pickle', 'excel': '.xls', 'html': '.html'} if outputfile is None: outputfile = inputfile.replace(path(inputfile).ext, '') outputfile = add_extension_if_needed(outputfile, otype_exts[otype]) if otype == 'csv':
python
{ "resource": "" }
q264482
load_mask
validation
def load_mask(image, allow_empty=True): """Load a Nifti mask volume. Parameters ---------- image: img-like object or boyle.nifti.NeuroImage or str Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image. If niimg is a string, consider it as a path to Nifti image and call nibabel.load on it. If it is an object, check if get_data() and get_affine() methods are present, raise TypeError otherwise. allow_empty: boolean, optional Allow loading an empty mask (full of 0 values) Returns ------- nibabel.Nifti1Image with boolean data. """ img = check_img(image, make_it_3d=True) values = np.unique(img.get_data()) if len(values) == 1: # We accept a single value if it is not 0 (full true mask). if values[0] == 0 and not allow_empty: raise ValueError('Given mask is invalid because it masks all data') elif len(values) == 2: # If there are 2 different values, one of them must be 0 (background) if 0 not in values:
python
{ "resource": "" }
q264483
load_mask_data
validation
def load_mask_data(image, allow_empty=True): """Load a Nifti mask volume and return its data matrix as boolean and affine. Parameters ---------- image: img-like object or boyle.nifti.NeuroImage or str Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
python
{ "resource": "" }
q264484
union_mask
validation
def union_mask(filelist): """ Creates a binarised mask with the union of the files in filelist. Parameters ---------- filelist: list of img-like object or boyle.nifti.NeuroImage or str List of paths to the volume files containing the ROIs. Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image. If niimg is a string, consider it as a path to Nifti image and call nibabel.load on it. If it is an object, check if get_data() and get_affine() methods are present, raise TypeError otherwise. Returns ------- ndarray of bools Mask volume Raises ------ ValueError
python
{ "resource": "" }
q264485
apply_mask
validation
def apply_mask(image, mask_img): """Read a Nifti file nii_file and a mask Nifti file. Returns the voxels in nii_file that are within the mask, the mask indices and the mask shape. Parameters ---------- image: img-like object or boyle.nifti.NeuroImage or str Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image. If niimg is a string, consider it as a path to Nifti image and call nibabel.load on it. If it is an object, check if get_data() and get_affine() methods are present, raise TypeError otherwise. mask_img: img-like object or boyle.nifti.NeuroImage or str 3D mask array: True where a voxel should be used.
python
{ "resource": "" }
q264486
apply_mask_4d
validation
def apply_mask_4d(image, mask_img): # , smooth_mm=None, remove_nans=True): """Read a Nifti file nii_file and a mask Nifti file. Extract the signals in nii_file that are within the mask, the mask indices and the mask shape. Parameters ---------- image: img-like object or boyle.nifti.NeuroImage or str Can either be: - a file path to a Nifti image - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image. If niimg is a string, consider it as a path to Nifti image and call nibabel.load on it. If it is an object, check if get_data() and get_affine() methods are present, raise TypeError otherwise. mask_img: img-like
python
{ "resource": "" }
q264487
vector_to_volume
validation
def vector_to_volume(arr, mask, order='C'): """Transform a given vector to a volume. This is a reshape function for 3D flattened and maybe masked vectors. Parameters ---------- arr: np.array 1-Dimensional array mask: numpy.ndarray Mask image. Must have 3 dimensions, bool dtype. Returns ------- np.ndarray """ if mask.dtype != np.bool: raise ValueError("mask must be a boolean array") if arr.ndim != 1: raise ValueError("vector must be a 1-dimensional array") if arr.ndim == 2 and any(v
python
{ "resource": "" }
q264488
matrix_to_4dvolume
validation
def matrix_to_4dvolume(arr, mask, order='C'): """Transform a given vector to a volume. This is a reshape function for 4D flattened masked matrices where the second dimension of the matrix corresponds to the original 4th dimension. Parameters ---------- arr: numpy.array 2D numpy.array mask: numpy.ndarray Mask image. Must have 3 dimensions, bool dtype. dtype: return type If None, will get the type from vector Returns ------- data: numpy.ndarray Unmasked data. Shape: (mask.shape[0], mask.shape[1], mask.shape[2], X.shape[1]) """ if mask.dtype != np.bool: raise ValueError("mask must be a boolean array")
python
{ "resource": "" }
q264489
niftilist_mask_to_array
validation
def niftilist_mask_to_array(img_filelist, mask_file=None, outdtype=None): """From the list of absolute paths to nifti files, creates a Numpy array with the masked data. Parameters ---------- img_filelist: list of str List of absolute file paths to nifti files. All nifti files must have the same shape. mask_file: str Path to a Nifti mask file. Should be the same shape as the files in nii_filelist. outdtype: dtype Type of the elements of the array, if not set will obtain the dtype from the first nifti file. Returns ------- outmat:
python
{ "resource": "" }
q264490
create
validation
def create(_): """Create a client for Service Fabric APIs.""" endpoint = client_endpoint() if not endpoint: raise CLIError("Connection endpoint not found. " "Before running sfctl commands, connect to a cluster using " "the 'sfctl cluster select' command.") no_verify
python
{ "resource": "" }
q264491
DataFrame.aggregate
validation
def aggregate(self, clazz, new_col, *args): """ Aggregate the rows of the DataFrame into a single value. :param clazz: name of a class that extends class Callable :type clazz: class :param new_col: name of the new column :type new_col:
python
{ "resource": "" }
q264492
group
validation
def group(*args): """ Pipeable grouping method. Takes either - a dataframe and a tuple of strings for grouping, - a tuple of strings if a dataframe has already been piped into. :Example: group(dataframe, "column") :Example: dataframe >> group("column") :param args: tuple of arguments :type args: tuple :return: returns a grouped
python
{ "resource": "" }
q264493
aggregate
validation
def aggregate(*args): """ Pipeable aggregation method. Takes either - a dataframe and a tuple of arguments required for aggregation, - a tuple of arguments if a dataframe has already been piped into. In any case one argument has to be a class that extends callable. :Example: aggregate(dataframe, Function, "new_col_name", "old_col_name") :Example: dataframe >> aggregate(Function, "new_col_name", "old_col_name") :param args: tuple of arguments :type args: tuple
python
{ "resource": "" }
q264494
subset
validation
def subset(*args): """ Pipeable subsetting method. Takes either - a dataframe and a tuple of arguments required for subsetting, - a tuple of arguments if a dataframe has already been piped into. :Example: subset(dataframe, "column") :Example: dataframe >> subset("column") :param args: tuple of arguments :type args: tuple :return: returns a
python
{ "resource": "" }
q264495
modify
validation
def modify(*args): """ Pipeable modification method Takes either - a dataframe and a tuple of arguments required for modification, - a tuple of arguments if a dataframe has already been piped into. In any case one argument has to be a class that extends callable. :Example: modify(dataframe, Function, "new_col_name", "old_col_name")
python
{ "resource": "" }
q264496
_escape_char
validation
def _escape_char(c, escape_char=ESCAPE_CHAR): """Escape a single character""" buf = [] for byte in c.encode('utf8'):
python
{ "resource": "" }
q264497
escape
validation
def escape(to_escape, safe=SAFE, escape_char=ESCAPE_CHAR, allow_collisions=False): """Escape a string so that it only contains characters in a safe set. Characters outside the safe list will be escaped with _%x_, where %x is the hex value of the character. If `allow_collisions` is True, occurrences of `escape_char` in the input will not be escaped. In this case, `unescape` cannot be used to reverse the transform because occurrences of the escape char in the resulting string are ambiguous. Only use this mode when: 1. collisions cannot occur or do not matter, and 2. unescape will never be called. .. versionadded: 1.0 allow_collisions argument. Prior to 1.0, behavior was the same as allow_collisions=False (default). """
python
{ "resource": "" }
q264498
unescape
validation
def unescape(escaped, escape_char=ESCAPE_CHAR): """Unescape a string escaped with `escape` escape_char must be the same as that used in the call to escape. """ if isinstance(escaped, bytes): # always work on text escaped = escaped.decode('utf8') escape_pat =
python
{ "resource": "" }
q264499
BaseBackend.can_send
validation
def can_send(self, user, notice_type): """ Determines whether this backend is allowed to send a notification to
python
{ "resource": "" }