repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
nvictus/priority-queue-dictionary
pqdict/__init__.py
pqdict.pushpopitem
def pushpopitem(self, key, value, node_factory=_Node): """ Equivalent to inserting a new item followed by removing the top priority item, but faster. Raises ``KeyError`` if the new key is already in the pqdict. """ heap = self._heap position = self._position precedes = self._precedes prio = self._keyfn(value) if self._keyfn else value node = node_factory(key, value, prio) if key in self: raise KeyError('%s is already in the queue' % repr(key)) if heap and precedes(heap[0].prio, node.prio): node, heap[0] = heap[0], node position[key] = 0 del position[node.key] self._sink(0) return node.key, node.value
python
def pushpopitem(self, key, value, node_factory=_Node): """ Equivalent to inserting a new item followed by removing the top priority item, but faster. Raises ``KeyError`` if the new key is already in the pqdict. """ heap = self._heap position = self._position precedes = self._precedes prio = self._keyfn(value) if self._keyfn else value node = node_factory(key, value, prio) if key in self: raise KeyError('%s is already in the queue' % repr(key)) if heap and precedes(heap[0].prio, node.prio): node, heap[0] = heap[0], node position[key] = 0 del position[node.key] self._sink(0) return node.key, node.value
[ "def", "pushpopitem", "(", "self", ",", "key", ",", "value", ",", "node_factory", "=", "_Node", ")", ":", "heap", "=", "self", ".", "_heap", "position", "=", "self", ".", "_position", "precedes", "=", "self", ".", "_precedes", "prio", "=", "self", ".", "_keyfn", "(", "value", ")", "if", "self", ".", "_keyfn", "else", "value", "node", "=", "node_factory", "(", "key", ",", "value", ",", "prio", ")", "if", "key", "in", "self", ":", "raise", "KeyError", "(", "'%s is already in the queue'", "%", "repr", "(", "key", ")", ")", "if", "heap", "and", "precedes", "(", "heap", "[", "0", "]", ".", "prio", ",", "node", ".", "prio", ")", ":", "node", ",", "heap", "[", "0", "]", "=", "heap", "[", "0", "]", ",", "node", "position", "[", "key", "]", "=", "0", "del", "position", "[", "node", ".", "key", "]", "self", ".", "_sink", "(", "0", ")", "return", "node", ".", "key", ",", "node", ".", "value" ]
Equivalent to inserting a new item followed by removing the top priority item, but faster. Raises ``KeyError`` if the new key is already in the pqdict.
[ "Equivalent", "to", "inserting", "a", "new", "item", "followed", "by", "removing", "the", "top", "priority", "item", "but", "faster", ".", "Raises", "KeyError", "if", "the", "new", "key", "is", "already", "in", "the", "pqdict", "." ]
577f9d3086058bec0e49cc2050dd9454b788d93b
https://github.com/nvictus/priority-queue-dictionary/blob/577f9d3086058bec0e49cc2050dd9454b788d93b/pqdict/__init__.py#L305-L324
train
nvictus/priority-queue-dictionary
pqdict/__init__.py
pqdict.updateitem
def updateitem(self, key, new_val): """ Update the priority value of an existing item. Raises ``KeyError`` if key is not in the pqdict. """ if key not in self._position: raise KeyError(key) self[key] = new_val
python
def updateitem(self, key, new_val): """ Update the priority value of an existing item. Raises ``KeyError`` if key is not in the pqdict. """ if key not in self._position: raise KeyError(key) self[key] = new_val
[ "def", "updateitem", "(", "self", ",", "key", ",", "new_val", ")", ":", "if", "key", "not", "in", "self", ".", "_position", ":", "raise", "KeyError", "(", "key", ")", "self", "[", "key", "]", "=", "new_val" ]
Update the priority value of an existing item. Raises ``KeyError`` if key is not in the pqdict.
[ "Update", "the", "priority", "value", "of", "an", "existing", "item", ".", "Raises", "KeyError", "if", "key", "is", "not", "in", "the", "pqdict", "." ]
577f9d3086058bec0e49cc2050dd9454b788d93b
https://github.com/nvictus/priority-queue-dictionary/blob/577f9d3086058bec0e49cc2050dd9454b788d93b/pqdict/__init__.py#L326-L334
train
nvictus/priority-queue-dictionary
pqdict/__init__.py
pqdict.replace_key
def replace_key(self, key, new_key): """ Replace the key of an existing heap node in place. Raises ``KeyError`` if the key to replace does not exist or if the new key is already in the pqdict. """ heap = self._heap position = self._position if new_key in self: raise KeyError('%s is already in the queue' % repr(new_key)) pos = position.pop(key) # raises appropriate KeyError position[new_key] = pos heap[pos].key = new_key
python
def replace_key(self, key, new_key): """ Replace the key of an existing heap node in place. Raises ``KeyError`` if the key to replace does not exist or if the new key is already in the pqdict. """ heap = self._heap position = self._position if new_key in self: raise KeyError('%s is already in the queue' % repr(new_key)) pos = position.pop(key) # raises appropriate KeyError position[new_key] = pos heap[pos].key = new_key
[ "def", "replace_key", "(", "self", ",", "key", ",", "new_key", ")", ":", "heap", "=", "self", ".", "_heap", "position", "=", "self", ".", "_position", "if", "new_key", "in", "self", ":", "raise", "KeyError", "(", "'%s is already in the queue'", "%", "repr", "(", "new_key", ")", ")", "pos", "=", "position", ".", "pop", "(", "key", ")", "# raises appropriate KeyError", "position", "[", "new_key", "]", "=", "pos", "heap", "[", "pos", "]", ".", "key", "=", "new_key" ]
Replace the key of an existing heap node in place. Raises ``KeyError`` if the key to replace does not exist or if the new key is already in the pqdict.
[ "Replace", "the", "key", "of", "an", "existing", "heap", "node", "in", "place", ".", "Raises", "KeyError", "if", "the", "key", "to", "replace", "does", "not", "exist", "or", "if", "the", "new", "key", "is", "already", "in", "the", "pqdict", "." ]
577f9d3086058bec0e49cc2050dd9454b788d93b
https://github.com/nvictus/priority-queue-dictionary/blob/577f9d3086058bec0e49cc2050dd9454b788d93b/pqdict/__init__.py#L336-L349
train
nvictus/priority-queue-dictionary
pqdict/__init__.py
pqdict.swap_priority
def swap_priority(self, key1, key2): """ Fast way to swap the priority level of two items in the pqdict. Raises ``KeyError`` if either key does not exist. """ heap = self._heap position = self._position if key1 not in self or key2 not in self: raise KeyError pos1, pos2 = position[key1], position[key2] heap[pos1].key, heap[pos2].key = key2, key1 position[key1], position[key2] = pos2, pos1
python
def swap_priority(self, key1, key2): """ Fast way to swap the priority level of two items in the pqdict. Raises ``KeyError`` if either key does not exist. """ heap = self._heap position = self._position if key1 not in self or key2 not in self: raise KeyError pos1, pos2 = position[key1], position[key2] heap[pos1].key, heap[pos2].key = key2, key1 position[key1], position[key2] = pos2, pos1
[ "def", "swap_priority", "(", "self", ",", "key1", ",", "key2", ")", ":", "heap", "=", "self", ".", "_heap", "position", "=", "self", ".", "_position", "if", "key1", "not", "in", "self", "or", "key2", "not", "in", "self", ":", "raise", "KeyError", "pos1", ",", "pos2", "=", "position", "[", "key1", "]", ",", "position", "[", "key2", "]", "heap", "[", "pos1", "]", ".", "key", ",", "heap", "[", "pos2", "]", ".", "key", "=", "key2", ",", "key1", "position", "[", "key1", "]", ",", "position", "[", "key2", "]", "=", "pos2", ",", "pos1" ]
Fast way to swap the priority level of two items in the pqdict. Raises ``KeyError`` if either key does not exist.
[ "Fast", "way", "to", "swap", "the", "priority", "level", "of", "two", "items", "in", "the", "pqdict", ".", "Raises", "KeyError", "if", "either", "key", "does", "not", "exist", "." ]
577f9d3086058bec0e49cc2050dd9454b788d93b
https://github.com/nvictus/priority-queue-dictionary/blob/577f9d3086058bec0e49cc2050dd9454b788d93b/pqdict/__init__.py#L351-L363
train
nvictus/priority-queue-dictionary
pqdict/__init__.py
pqdict.heapify
def heapify(self, key=__marker): """ Repair a broken heap. If the state of an item's priority value changes you can re-sort the relevant item only by providing ``key``. """ if key is self.__marker: n = len(self._heap) for pos in reversed(range(n//2)): self._sink(pos) else: try: pos = self._position[key] except KeyError: raise KeyError(key) self._reheapify(pos)
python
def heapify(self, key=__marker): """ Repair a broken heap. If the state of an item's priority value changes you can re-sort the relevant item only by providing ``key``. """ if key is self.__marker: n = len(self._heap) for pos in reversed(range(n//2)): self._sink(pos) else: try: pos = self._position[key] except KeyError: raise KeyError(key) self._reheapify(pos)
[ "def", "heapify", "(", "self", ",", "key", "=", "__marker", ")", ":", "if", "key", "is", "self", ".", "__marker", ":", "n", "=", "len", "(", "self", ".", "_heap", ")", "for", "pos", "in", "reversed", "(", "range", "(", "n", "//", "2", ")", ")", ":", "self", ".", "_sink", "(", "pos", ")", "else", ":", "try", ":", "pos", "=", "self", ".", "_position", "[", "key", "]", "except", "KeyError", ":", "raise", "KeyError", "(", "key", ")", "self", ".", "_reheapify", "(", "pos", ")" ]
Repair a broken heap. If the state of an item's priority value changes you can re-sort the relevant item only by providing ``key``.
[ "Repair", "a", "broken", "heap", ".", "If", "the", "state", "of", "an", "item", "s", "priority", "value", "changes", "you", "can", "re", "-", "sort", "the", "relevant", "item", "only", "by", "providing", "key", "." ]
577f9d3086058bec0e49cc2050dd9454b788d93b
https://github.com/nvictus/priority-queue-dictionary/blob/577f9d3086058bec0e49cc2050dd9454b788d93b/pqdict/__init__.py#L398-L413
train
ajk8/hatchery
hatchery/project.py
package_has_version_file
def package_has_version_file(package_name): """ Check to make sure _version.py is contained in the package """ version_file_path = helpers.package_file_path('_version.py', package_name) return os.path.isfile(version_file_path)
python
def package_has_version_file(package_name): """ Check to make sure _version.py is contained in the package """ version_file_path = helpers.package_file_path('_version.py', package_name) return os.path.isfile(version_file_path)
[ "def", "package_has_version_file", "(", "package_name", ")", ":", "version_file_path", "=", "helpers", ".", "package_file_path", "(", "'_version.py'", ",", "package_name", ")", "return", "os", ".", "path", ".", "isfile", "(", "version_file_path", ")" ]
Check to make sure _version.py is contained in the package
[ "Check", "to", "make", "sure", "_version", ".", "py", "is", "contained", "in", "the", "package" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L45-L48
train
ajk8/hatchery
hatchery/project.py
get_project_name
def get_project_name(): """ Grab the project name out of setup.py """ setup_py_content = helpers.get_file_content('setup.py') ret = helpers.value_of_named_argument_in_function( 'name', 'setup', setup_py_content, resolve_varname=True ) if ret and ret[0] == ret[-1] in ('"', "'"): ret = ret[1:-1] return ret
python
def get_project_name(): """ Grab the project name out of setup.py """ setup_py_content = helpers.get_file_content('setup.py') ret = helpers.value_of_named_argument_in_function( 'name', 'setup', setup_py_content, resolve_varname=True ) if ret and ret[0] == ret[-1] in ('"', "'"): ret = ret[1:-1] return ret
[ "def", "get_project_name", "(", ")", ":", "setup_py_content", "=", "helpers", ".", "get_file_content", "(", "'setup.py'", ")", "ret", "=", "helpers", ".", "value_of_named_argument_in_function", "(", "'name'", ",", "'setup'", ",", "setup_py_content", ",", "resolve_varname", "=", "True", ")", "if", "ret", "and", "ret", "[", "0", "]", "==", "ret", "[", "-", "1", "]", "in", "(", "'\"'", ",", "\"'\"", ")", ":", "ret", "=", "ret", "[", "1", ":", "-", "1", "]", "return", "ret" ]
Grab the project name out of setup.py
[ "Grab", "the", "project", "name", "out", "of", "setup", ".", "py" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L78-L86
train
ajk8/hatchery
hatchery/project.py
get_version
def get_version(package_name, ignore_cache=False): """ Get the version which is currently configured by the package """ if ignore_cache: with microcache.temporarily_disabled(): found = helpers.regex_in_package_file( VERSION_SET_REGEX, '_version.py', package_name, return_match=True ) else: found = helpers.regex_in_package_file( VERSION_SET_REGEX, '_version.py', package_name, return_match=True ) if found is None: raise ProjectError('found {}, but __version__ is not defined') current_version = found['version'] return current_version
python
def get_version(package_name, ignore_cache=False): """ Get the version which is currently configured by the package """ if ignore_cache: with microcache.temporarily_disabled(): found = helpers.regex_in_package_file( VERSION_SET_REGEX, '_version.py', package_name, return_match=True ) else: found = helpers.regex_in_package_file( VERSION_SET_REGEX, '_version.py', package_name, return_match=True ) if found is None: raise ProjectError('found {}, but __version__ is not defined') current_version = found['version'] return current_version
[ "def", "get_version", "(", "package_name", ",", "ignore_cache", "=", "False", ")", ":", "if", "ignore_cache", ":", "with", "microcache", ".", "temporarily_disabled", "(", ")", ":", "found", "=", "helpers", ".", "regex_in_package_file", "(", "VERSION_SET_REGEX", ",", "'_version.py'", ",", "package_name", ",", "return_match", "=", "True", ")", "else", ":", "found", "=", "helpers", ".", "regex_in_package_file", "(", "VERSION_SET_REGEX", ",", "'_version.py'", ",", "package_name", ",", "return_match", "=", "True", ")", "if", "found", "is", "None", ":", "raise", "ProjectError", "(", "'found {}, but __version__ is not defined'", ")", "current_version", "=", "found", "[", "'version'", "]", "return", "current_version" ]
Get the version which is currently configured by the package
[ "Get", "the", "version", "which", "is", "currently", "configured", "by", "the", "package" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L89-L103
train
ajk8/hatchery
hatchery/project.py
set_version
def set_version(package_name, version_str): """ Set the version in _version.py to version_str """ current_version = get_version(package_name) version_file_path = helpers.package_file_path('_version.py', package_name) version_file_content = helpers.get_file_content(version_file_path) version_file_content = version_file_content.replace(current_version, version_str) with open(version_file_path, 'w') as version_file: version_file.write(version_file_content)
python
def set_version(package_name, version_str): """ Set the version in _version.py to version_str """ current_version = get_version(package_name) version_file_path = helpers.package_file_path('_version.py', package_name) version_file_content = helpers.get_file_content(version_file_path) version_file_content = version_file_content.replace(current_version, version_str) with open(version_file_path, 'w') as version_file: version_file.write(version_file_content)
[ "def", "set_version", "(", "package_name", ",", "version_str", ")", ":", "current_version", "=", "get_version", "(", "package_name", ")", "version_file_path", "=", "helpers", ".", "package_file_path", "(", "'_version.py'", ",", "package_name", ")", "version_file_content", "=", "helpers", ".", "get_file_content", "(", "version_file_path", ")", "version_file_content", "=", "version_file_content", ".", "replace", "(", "current_version", ",", "version_str", ")", "with", "open", "(", "version_file_path", ",", "'w'", ")", "as", "version_file", ":", "version_file", ".", "write", "(", "version_file_content", ")" ]
Set the version in _version.py to version_str
[ "Set", "the", "version", "in", "_version", ".", "py", "to", "version_str" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L106-L113
train
ajk8/hatchery
hatchery/project.py
version_is_valid
def version_is_valid(version_str): """ Check to see if the version specified is a valid as far as pkg_resources is concerned >>> version_is_valid('blah') False >>> version_is_valid('1.2.3') True """ try: packaging.version.Version(version_str) except packaging.version.InvalidVersion: return False return True
python
def version_is_valid(version_str): """ Check to see if the version specified is a valid as far as pkg_resources is concerned >>> version_is_valid('blah') False >>> version_is_valid('1.2.3') True """ try: packaging.version.Version(version_str) except packaging.version.InvalidVersion: return False return True
[ "def", "version_is_valid", "(", "version_str", ")", ":", "try", ":", "packaging", ".", "version", ".", "Version", "(", "version_str", ")", "except", "packaging", ".", "version", ".", "InvalidVersion", ":", "return", "False", "return", "True" ]
Check to see if the version specified is a valid as far as pkg_resources is concerned >>> version_is_valid('blah') False >>> version_is_valid('1.2.3') True
[ "Check", "to", "see", "if", "the", "version", "specified", "is", "a", "valid", "as", "far", "as", "pkg_resources", "is", "concerned" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L116-L128
train
ajk8/hatchery
hatchery/project.py
_get_uploaded_versions_warehouse
def _get_uploaded_versions_warehouse(project_name, index_url, requests_verify=True): """ Query the pypi index at index_url using warehouse api to find all of the "releases" """ url = '/'.join((index_url, project_name, 'json')) response = requests.get(url, verify=requests_verify) if response.status_code == 200: return response.json()['releases'].keys() return None
python
def _get_uploaded_versions_warehouse(project_name, index_url, requests_verify=True): """ Query the pypi index at index_url using warehouse api to find all of the "releases" """ url = '/'.join((index_url, project_name, 'json')) response = requests.get(url, verify=requests_verify) if response.status_code == 200: return response.json()['releases'].keys() return None
[ "def", "_get_uploaded_versions_warehouse", "(", "project_name", ",", "index_url", ",", "requests_verify", "=", "True", ")", ":", "url", "=", "'/'", ".", "join", "(", "(", "index_url", ",", "project_name", ",", "'json'", ")", ")", "response", "=", "requests", ".", "get", "(", "url", ",", "verify", "=", "requests_verify", ")", "if", "response", ".", "status_code", "==", "200", ":", "return", "response", ".", "json", "(", ")", "[", "'releases'", "]", ".", "keys", "(", ")", "return", "None" ]
Query the pypi index at index_url using warehouse api to find all of the "releases"
[ "Query", "the", "pypi", "index", "at", "index_url", "using", "warehouse", "api", "to", "find", "all", "of", "the", "releases" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L131-L137
train
ajk8/hatchery
hatchery/project.py
_get_uploaded_versions_pypicloud
def _get_uploaded_versions_pypicloud(project_name, index_url, requests_verify=True): """ Query the pypi index at index_url using pypicloud api to find all versions """ api_url = index_url for suffix in ('/pypi', '/pypi/', '/simple', '/simple/'): if api_url.endswith(suffix): api_url = api_url[:len(suffix) * -1] + '/api/package' break url = '/'.join((api_url, project_name)) response = requests.get(url, verify=requests_verify) if response.status_code == 200: return [p['version'] for p in response.json()['packages']] return None
python
def _get_uploaded_versions_pypicloud(project_name, index_url, requests_verify=True): """ Query the pypi index at index_url using pypicloud api to find all versions """ api_url = index_url for suffix in ('/pypi', '/pypi/', '/simple', '/simple/'): if api_url.endswith(suffix): api_url = api_url[:len(suffix) * -1] + '/api/package' break url = '/'.join((api_url, project_name)) response = requests.get(url, verify=requests_verify) if response.status_code == 200: return [p['version'] for p in response.json()['packages']] return None
[ "def", "_get_uploaded_versions_pypicloud", "(", "project_name", ",", "index_url", ",", "requests_verify", "=", "True", ")", ":", "api_url", "=", "index_url", "for", "suffix", "in", "(", "'/pypi'", ",", "'/pypi/'", ",", "'/simple'", ",", "'/simple/'", ")", ":", "if", "api_url", ".", "endswith", "(", "suffix", ")", ":", "api_url", "=", "api_url", "[", ":", "len", "(", "suffix", ")", "*", "-", "1", "]", "+", "'/api/package'", "break", "url", "=", "'/'", ".", "join", "(", "(", "api_url", ",", "project_name", ")", ")", "response", "=", "requests", ".", "get", "(", "url", ",", "verify", "=", "requests_verify", ")", "if", "response", ".", "status_code", "==", "200", ":", "return", "[", "p", "[", "'version'", "]", "for", "p", "in", "response", ".", "json", "(", ")", "[", "'packages'", "]", "]", "return", "None" ]
Query the pypi index at index_url using pypicloud api to find all versions
[ "Query", "the", "pypi", "index", "at", "index_url", "using", "pypicloud", "api", "to", "find", "all", "versions" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L140-L151
train
ajk8/hatchery
hatchery/project.py
version_already_uploaded
def version_already_uploaded(project_name, version_str, index_url, requests_verify=True): """ Check to see if the version specified has already been uploaded to the configured index """ all_versions = _get_uploaded_versions(project_name, index_url, requests_verify) return version_str in all_versions
python
def version_already_uploaded(project_name, version_str, index_url, requests_verify=True): """ Check to see if the version specified has already been uploaded to the configured index """ all_versions = _get_uploaded_versions(project_name, index_url, requests_verify) return version_str in all_versions
[ "def", "version_already_uploaded", "(", "project_name", ",", "version_str", ",", "index_url", ",", "requests_verify", "=", "True", ")", ":", "all_versions", "=", "_get_uploaded_versions", "(", "project_name", ",", "index_url", ",", "requests_verify", ")", "return", "version_str", "in", "all_versions" ]
Check to see if the version specified has already been uploaded to the configured index
[ "Check", "to", "see", "if", "the", "version", "specified", "has", "already", "been", "uploaded", "to", "the", "configured", "index" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L168-L172
train
ajk8/hatchery
hatchery/project.py
convert_readme_to_rst
def convert_readme_to_rst(): """ Attempt to convert a README.md file into README.rst """ project_files = os.listdir('.') for filename in project_files: if filename.lower() == 'readme': raise ProjectError( 'found {} in project directory...'.format(filename) + 'not sure what to do with it, refusing to convert' ) elif filename.lower() == 'readme.rst': raise ProjectError( 'found {} in project directory...'.format(filename) + 'refusing to overwrite' ) for filename in project_files: if filename.lower() == 'readme.md': rst_filename = 'README.rst' logger.info('converting {} to {}'.format(filename, rst_filename)) try: rst_content = pypandoc.convert(filename, 'rst') with open('README.rst', 'w') as rst_file: rst_file.write(rst_content) return except OSError as e: raise ProjectError( 'could not convert readme to rst due to pypandoc error:' + os.linesep + str(e) ) raise ProjectError('could not find any README.md file to convert')
python
def convert_readme_to_rst(): """ Attempt to convert a README.md file into README.rst """ project_files = os.listdir('.') for filename in project_files: if filename.lower() == 'readme': raise ProjectError( 'found {} in project directory...'.format(filename) + 'not sure what to do with it, refusing to convert' ) elif filename.lower() == 'readme.rst': raise ProjectError( 'found {} in project directory...'.format(filename) + 'refusing to overwrite' ) for filename in project_files: if filename.lower() == 'readme.md': rst_filename = 'README.rst' logger.info('converting {} to {}'.format(filename, rst_filename)) try: rst_content = pypandoc.convert(filename, 'rst') with open('README.rst', 'w') as rst_file: rst_file.write(rst_content) return except OSError as e: raise ProjectError( 'could not convert readme to rst due to pypandoc error:' + os.linesep + str(e) ) raise ProjectError('could not find any README.md file to convert')
[ "def", "convert_readme_to_rst", "(", ")", ":", "project_files", "=", "os", ".", "listdir", "(", "'.'", ")", "for", "filename", "in", "project_files", ":", "if", "filename", ".", "lower", "(", ")", "==", "'readme'", ":", "raise", "ProjectError", "(", "'found {} in project directory...'", ".", "format", "(", "filename", ")", "+", "'not sure what to do with it, refusing to convert'", ")", "elif", "filename", ".", "lower", "(", ")", "==", "'readme.rst'", ":", "raise", "ProjectError", "(", "'found {} in project directory...'", ".", "format", "(", "filename", ")", "+", "'refusing to overwrite'", ")", "for", "filename", "in", "project_files", ":", "if", "filename", ".", "lower", "(", ")", "==", "'readme.md'", ":", "rst_filename", "=", "'README.rst'", "logger", ".", "info", "(", "'converting {} to {}'", ".", "format", "(", "filename", ",", "rst_filename", ")", ")", "try", ":", "rst_content", "=", "pypandoc", ".", "convert", "(", "filename", ",", "'rst'", ")", "with", "open", "(", "'README.rst'", ",", "'w'", ")", "as", "rst_file", ":", "rst_file", ".", "write", "(", "rst_content", ")", "return", "except", "OSError", "as", "e", ":", "raise", "ProjectError", "(", "'could not convert readme to rst due to pypandoc error:'", "+", "os", ".", "linesep", "+", "str", "(", "e", ")", ")", "raise", "ProjectError", "(", "'could not find any README.md file to convert'", ")" ]
Attempt to convert a README.md file into README.rst
[ "Attempt", "to", "convert", "a", "README", ".", "md", "file", "into", "README", ".", "rst" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L208-L235
train
ajk8/hatchery
hatchery/project.py
get_packaged_files
def get_packaged_files(package_name): """ Collect relative paths to all files which have already been packaged """ if not os.path.isdir('dist'): return [] return [os.path.join('dist', filename) for filename in os.listdir('dist')]
python
def get_packaged_files(package_name): """ Collect relative paths to all files which have already been packaged """ if not os.path.isdir('dist'): return [] return [os.path.join('dist', filename) for filename in os.listdir('dist')]
[ "def", "get_packaged_files", "(", "package_name", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "'dist'", ")", ":", "return", "[", "]", "return", "[", "os", ".", "path", ".", "join", "(", "'dist'", ",", "filename", ")", "for", "filename", "in", "os", ".", "listdir", "(", "'dist'", ")", "]" ]
Collect relative paths to all files which have already been packaged
[ "Collect", "relative", "paths", "to", "all", "files", "which", "have", "already", "been", "packaged" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L238-L242
train
ajk8/hatchery
hatchery/project.py
multiple_packaged_versions
def multiple_packaged_versions(package_name): """ Look through built package directory and see if there are multiple versions there """ dist_files = os.listdir('dist') versions = set() for filename in dist_files: version = funcy.re_find(r'{}-(.+).tar.gz'.format(package_name), filename) if version: versions.add(version) return len(versions) > 1
python
def multiple_packaged_versions(package_name): """ Look through built package directory and see if there are multiple versions there """ dist_files = os.listdir('dist') versions = set() for filename in dist_files: version = funcy.re_find(r'{}-(.+).tar.gz'.format(package_name), filename) if version: versions.add(version) return len(versions) > 1
[ "def", "multiple_packaged_versions", "(", "package_name", ")", ":", "dist_files", "=", "os", ".", "listdir", "(", "'dist'", ")", "versions", "=", "set", "(", ")", "for", "filename", "in", "dist_files", ":", "version", "=", "funcy", ".", "re_find", "(", "r'{}-(.+).tar.gz'", ".", "format", "(", "package_name", ")", ",", "filename", ")", "if", "version", ":", "versions", ".", "add", "(", "version", ")", "return", "len", "(", "versions", ")", ">", "1" ]
Look through built package directory and see if there are multiple versions there
[ "Look", "through", "built", "package", "directory", "and", "see", "if", "there", "are", "multiple", "versions", "there" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/project.py#L245-L253
train
djgagne/hagelslag
hagelslag/data/HailForecastGrid.py
HailForecastGrid.period_neighborhood_probability
def period_neighborhood_probability(self, radius, smoothing, threshold, stride,start_time,end_time): """ Calculate the neighborhood probability over the full period of the forecast Args: radius: circular radius from each point in km smoothing: width of Gaussian smoother in km threshold: intensity of exceedance stride: number of grid points to skip for reduced neighborhood grid Returns: (neighborhood probabilities) """ neighbor_x = self.x[::stride, ::stride] neighbor_y = self.y[::stride, ::stride] neighbor_kd_tree = cKDTree(np.vstack((neighbor_x.ravel(), neighbor_y.ravel())).T) neighbor_prob = np.zeros((self.data.shape[0], neighbor_x.shape[0], neighbor_x.shape[1])) print('Forecast Hours: {0}-{1}'.format(start_time, end_time)) for m in range(len(self.members)): period_max = self.data[m,start_time:end_time,:,:].max(axis=0) valid_i, valid_j = np.where(period_max >= threshold) print(self.members[m], len(valid_i)) if len(valid_i) > 0: var_kd_tree = cKDTree(np.vstack((self.x[valid_i, valid_j], self.y[valid_i, valid_j])).T) exceed_points = np.unique(np.concatenate(var_kd_tree.query_ball_tree(neighbor_kd_tree, radius))).astype(int) exceed_i, exceed_j = np.unravel_index(exceed_points, neighbor_x.shape) neighbor_prob[m][exceed_i, exceed_j] = 1 if smoothing > 0: neighbor_prob[m] = gaussian_filter(neighbor_prob[m], smoothing,mode='constant') return neighbor_prob
python
def period_neighborhood_probability(self, radius, smoothing, threshold, stride,start_time,end_time): """ Calculate the neighborhood probability over the full period of the forecast Args: radius: circular radius from each point in km smoothing: width of Gaussian smoother in km threshold: intensity of exceedance stride: number of grid points to skip for reduced neighborhood grid Returns: (neighborhood probabilities) """ neighbor_x = self.x[::stride, ::stride] neighbor_y = self.y[::stride, ::stride] neighbor_kd_tree = cKDTree(np.vstack((neighbor_x.ravel(), neighbor_y.ravel())).T) neighbor_prob = np.zeros((self.data.shape[0], neighbor_x.shape[0], neighbor_x.shape[1])) print('Forecast Hours: {0}-{1}'.format(start_time, end_time)) for m in range(len(self.members)): period_max = self.data[m,start_time:end_time,:,:].max(axis=0) valid_i, valid_j = np.where(period_max >= threshold) print(self.members[m], len(valid_i)) if len(valid_i) > 0: var_kd_tree = cKDTree(np.vstack((self.x[valid_i, valid_j], self.y[valid_i, valid_j])).T) exceed_points = np.unique(np.concatenate(var_kd_tree.query_ball_tree(neighbor_kd_tree, radius))).astype(int) exceed_i, exceed_j = np.unravel_index(exceed_points, neighbor_x.shape) neighbor_prob[m][exceed_i, exceed_j] = 1 if smoothing > 0: neighbor_prob[m] = gaussian_filter(neighbor_prob[m], smoothing,mode='constant') return neighbor_prob
[ "def", "period_neighborhood_probability", "(", "self", ",", "radius", ",", "smoothing", ",", "threshold", ",", "stride", ",", "start_time", ",", "end_time", ")", ":", "neighbor_x", "=", "self", ".", "x", "[", ":", ":", "stride", ",", ":", ":", "stride", "]", "neighbor_y", "=", "self", ".", "y", "[", ":", ":", "stride", ",", ":", ":", "stride", "]", "neighbor_kd_tree", "=", "cKDTree", "(", "np", ".", "vstack", "(", "(", "neighbor_x", ".", "ravel", "(", ")", ",", "neighbor_y", ".", "ravel", "(", ")", ")", ")", ".", "T", ")", "neighbor_prob", "=", "np", ".", "zeros", "(", "(", "self", ".", "data", ".", "shape", "[", "0", "]", ",", "neighbor_x", ".", "shape", "[", "0", "]", ",", "neighbor_x", ".", "shape", "[", "1", "]", ")", ")", "print", "(", "'Forecast Hours: {0}-{1}'", ".", "format", "(", "start_time", ",", "end_time", ")", ")", "for", "m", "in", "range", "(", "len", "(", "self", ".", "members", ")", ")", ":", "period_max", "=", "self", ".", "data", "[", "m", ",", "start_time", ":", "end_time", ",", ":", ",", ":", "]", ".", "max", "(", "axis", "=", "0", ")", "valid_i", ",", "valid_j", "=", "np", ".", "where", "(", "period_max", ">=", "threshold", ")", "print", "(", "self", ".", "members", "[", "m", "]", ",", "len", "(", "valid_i", ")", ")", "if", "len", "(", "valid_i", ")", ">", "0", ":", "var_kd_tree", "=", "cKDTree", "(", "np", ".", "vstack", "(", "(", "self", ".", "x", "[", "valid_i", ",", "valid_j", "]", ",", "self", ".", "y", "[", "valid_i", ",", "valid_j", "]", ")", ")", ".", "T", ")", "exceed_points", "=", "np", ".", "unique", "(", "np", ".", "concatenate", "(", "var_kd_tree", ".", "query_ball_tree", "(", "neighbor_kd_tree", ",", "radius", ")", ")", ")", ".", "astype", "(", "int", ")", "exceed_i", ",", "exceed_j", "=", "np", ".", "unravel_index", "(", "exceed_points", ",", "neighbor_x", ".", "shape", ")", "neighbor_prob", "[", "m", "]", "[", "exceed_i", ",", "exceed_j", "]", "=", "1", "if", "smoothing", ">", "0", ":", "neighbor_prob", "[", "m", "]", "=", "gaussian_filter", "(", "neighbor_prob", "[", "m", "]", ",", "smoothing", ",", "mode", "=", "'constant'", ")", "return", "neighbor_prob" ]
Calculate the neighborhood probability over the full period of the forecast Args: radius: circular radius from each point in km smoothing: width of Gaussian smoother in km threshold: intensity of exceedance stride: number of grid points to skip for reduced neighborhood grid Returns: (neighborhood probabilities)
[ "Calculate", "the", "neighborhood", "probability", "over", "the", "full", "period", "of", "the", "forecast" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/data/HailForecastGrid.py#L94-L123
train
base4sistemas/satcfe
satcfe/resposta/consultarnumerosessao.py
RespostaConsultarNumeroSessao.analisar
def analisar(retorno): """Constrói uma :class:`RespostaSAT` ou especialização dependendo da função SAT encontrada na sessão consultada. :param unicode retorno: Retorno da função ``ConsultarNumeroSessao``. """ if '|' not in retorno: raise ErroRespostaSATInvalida('Resposta nao possui pipes ' 'separando os campos: {!r}'.format(retorno)) resposta = _RespostaParcial(*(retorno.split('|')[:2])) for faixa, construtor in _RESPOSTAS_POSSIVEIS: if int(resposta.EEEEE) in xrange(faixa, faixa+1000): return construtor(retorno) return RespostaConsultarNumeroSessao._pos_analise(retorno)
python
def analisar(retorno): """Constrói uma :class:`RespostaSAT` ou especialização dependendo da função SAT encontrada na sessão consultada. :param unicode retorno: Retorno da função ``ConsultarNumeroSessao``. """ if '|' not in retorno: raise ErroRespostaSATInvalida('Resposta nao possui pipes ' 'separando os campos: {!r}'.format(retorno)) resposta = _RespostaParcial(*(retorno.split('|')[:2])) for faixa, construtor in _RESPOSTAS_POSSIVEIS: if int(resposta.EEEEE) in xrange(faixa, faixa+1000): return construtor(retorno) return RespostaConsultarNumeroSessao._pos_analise(retorno)
[ "def", "analisar", "(", "retorno", ")", ":", "if", "'|'", "not", "in", "retorno", ":", "raise", "ErroRespostaSATInvalida", "(", "'Resposta nao possui pipes '", "'separando os campos: {!r}'", ".", "format", "(", "retorno", ")", ")", "resposta", "=", "_RespostaParcial", "(", "*", "(", "retorno", ".", "split", "(", "'|'", ")", "[", ":", "2", "]", ")", ")", "for", "faixa", ",", "construtor", "in", "_RESPOSTAS_POSSIVEIS", ":", "if", "int", "(", "resposta", ".", "EEEEE", ")", "in", "xrange", "(", "faixa", ",", "faixa", "+", "1000", ")", ":", "return", "construtor", "(", "retorno", ")", "return", "RespostaConsultarNumeroSessao", ".", "_pos_analise", "(", "retorno", ")" ]
Constrói uma :class:`RespostaSAT` ou especialização dependendo da função SAT encontrada na sessão consultada. :param unicode retorno: Retorno da função ``ConsultarNumeroSessao``.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "ou", "especialização", "dependendo", "da", "função", "SAT", "encontrada", "na", "sessão", "consultada", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/consultarnumerosessao.py#L65-L81
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
analisar_retorno
def analisar_retorno(retorno, classe_resposta=RespostaSAT, campos=RespostaSAT.CAMPOS, campos_alternativos=[], funcao=None, manter_verbatim=True): """Analisa o retorno (supostamente um retorno de uma função do SAT) conforme o padrão e campos esperados. O retorno deverá possuir dados separados entre si através de pipes e o número de campos deverá coincidir com os campos especificados. O campos devem ser especificados como uma tupla onde cada elemento da tupla deverá ser uma tupla contendo dois elementos: o nome do campo e uma função de conversão a partir de uma string unicode. Por exemplo: .. sourcecode:: python >>> retorno = '123456|08000|SAT em operacao||' >>> resposta = analisar_retorno(retorno, funcao='ConsultarSAT') >>> resposta.numeroSessao 123456 >>> resposta.EEEEE u'08000' >>> resposta.mensagem u'SAT em operacao' >>> resposta.cod u'' >>> resposta.mensagemSEFAZ u'' >>> resposta.atributos.funcao 'ConsultarSAT' >>> resposta.atributos.verbatim '123456|08000|SAT em operacao||' :param unicode retorno: O conteúdo **unicode** da resposta retornada pela função da DLL SAT. :param type classe_resposta: O tipo :class:`RespostaSAT` ou especialização que irá representar o retorno, após sua decomposição em campos. :param tuple campos: Especificação dos campos (nomes) e seus conversores a a partir do tipo ``unicode``. :param list campos_alternativos: Especifica conjuntos de campos alternativos que serão considerados caso o número de campos encontrados na resposta não coincida com o número de campos especificados em ``campos``. Para que a relação alternativa de campos funcione, é importante que cada relação de campos alternativos tenha um número diferente de campos. :param str funcao: Nome da função da DLL SAT que gerou o retorno, que estará disponível nos atributos adicionais à resposta. :param bool manter_verbatim: Se uma cópia verbatim da resposta deverá ser mantida nos atributos adicionais à resposta. :raises ErroRespostaSATInvalida: Se o retorno não estiver em conformidade com o padrão esperado ou se não possuir os campos especificados. :return: Uma instância de :class:`RespostaSAT` ou especialização. :rtype: satcfe.resposta.padrao.RespostaSAT """ if '|' not in retorno: raise ErroRespostaSATInvalida('Resposta nao possui pipes separando os ' 'campos: "%s"' % as_ascii(retorno)) partes = retorno.split('|') if len(partes) != len(campos): # procura por uma relação alternativa de campos do retorno for relacao_alternativa in campos_alternativos: if len(partes) == len(relacao_alternativa): relacao_campos = relacao_alternativa break else: raise ErroRespostaSATInvalida('Resposta nao possui o numero ' 'esperado de campos. Esperados %d campos, mas ' 'contem %d: "%s"' % ( len(campos), len(partes), as_ascii(retorno),)) else: relacao_campos = campos resultado = {} def _enumerate(sequence): for index, value in enumerate(sequence): yield index, value[0], value[1] for indice, campo, conversor in _enumerate(relacao_campos): resultado[campo] = conversor(partes[indice]) resposta = classe_resposta(**resultado) resposta.atributos.funcao = funcao resposta.atributos.verbatim = retorno if manter_verbatim else None return resposta
python
def analisar_retorno(retorno, classe_resposta=RespostaSAT, campos=RespostaSAT.CAMPOS, campos_alternativos=[], funcao=None, manter_verbatim=True): """Analisa o retorno (supostamente um retorno de uma função do SAT) conforme o padrão e campos esperados. O retorno deverá possuir dados separados entre si através de pipes e o número de campos deverá coincidir com os campos especificados. O campos devem ser especificados como uma tupla onde cada elemento da tupla deverá ser uma tupla contendo dois elementos: o nome do campo e uma função de conversão a partir de uma string unicode. Por exemplo: .. sourcecode:: python >>> retorno = '123456|08000|SAT em operacao||' >>> resposta = analisar_retorno(retorno, funcao='ConsultarSAT') >>> resposta.numeroSessao 123456 >>> resposta.EEEEE u'08000' >>> resposta.mensagem u'SAT em operacao' >>> resposta.cod u'' >>> resposta.mensagemSEFAZ u'' >>> resposta.atributos.funcao 'ConsultarSAT' >>> resposta.atributos.verbatim '123456|08000|SAT em operacao||' :param unicode retorno: O conteúdo **unicode** da resposta retornada pela função da DLL SAT. :param type classe_resposta: O tipo :class:`RespostaSAT` ou especialização que irá representar o retorno, após sua decomposição em campos. :param tuple campos: Especificação dos campos (nomes) e seus conversores a a partir do tipo ``unicode``. :param list campos_alternativos: Especifica conjuntos de campos alternativos que serão considerados caso o número de campos encontrados na resposta não coincida com o número de campos especificados em ``campos``. Para que a relação alternativa de campos funcione, é importante que cada relação de campos alternativos tenha um número diferente de campos. :param str funcao: Nome da função da DLL SAT que gerou o retorno, que estará disponível nos atributos adicionais à resposta. :param bool manter_verbatim: Se uma cópia verbatim da resposta deverá ser mantida nos atributos adicionais à resposta. :raises ErroRespostaSATInvalida: Se o retorno não estiver em conformidade com o padrão esperado ou se não possuir os campos especificados. :return: Uma instância de :class:`RespostaSAT` ou especialização. :rtype: satcfe.resposta.padrao.RespostaSAT """ if '|' not in retorno: raise ErroRespostaSATInvalida('Resposta nao possui pipes separando os ' 'campos: "%s"' % as_ascii(retorno)) partes = retorno.split('|') if len(partes) != len(campos): # procura por uma relação alternativa de campos do retorno for relacao_alternativa in campos_alternativos: if len(partes) == len(relacao_alternativa): relacao_campos = relacao_alternativa break else: raise ErroRespostaSATInvalida('Resposta nao possui o numero ' 'esperado de campos. Esperados %d campos, mas ' 'contem %d: "%s"' % ( len(campos), len(partes), as_ascii(retorno),)) else: relacao_campos = campos resultado = {} def _enumerate(sequence): for index, value in enumerate(sequence): yield index, value[0], value[1] for indice, campo, conversor in _enumerate(relacao_campos): resultado[campo] = conversor(partes[indice]) resposta = classe_resposta(**resultado) resposta.atributos.funcao = funcao resposta.atributos.verbatim = retorno if manter_verbatim else None return resposta
[ "def", "analisar_retorno", "(", "retorno", ",", "classe_resposta", "=", "RespostaSAT", ",", "campos", "=", "RespostaSAT", ".", "CAMPOS", ",", "campos_alternativos", "=", "[", "]", ",", "funcao", "=", "None", ",", "manter_verbatim", "=", "True", ")", ":", "if", "'|'", "not", "in", "retorno", ":", "raise", "ErroRespostaSATInvalida", "(", "'Resposta nao possui pipes separando os '", "'campos: \"%s\"'", "%", "as_ascii", "(", "retorno", ")", ")", "partes", "=", "retorno", ".", "split", "(", "'|'", ")", "if", "len", "(", "partes", ")", "!=", "len", "(", "campos", ")", ":", "# procura por uma relação alternativa de campos do retorno", "for", "relacao_alternativa", "in", "campos_alternativos", ":", "if", "len", "(", "partes", ")", "==", "len", "(", "relacao_alternativa", ")", ":", "relacao_campos", "=", "relacao_alternativa", "break", "else", ":", "raise", "ErroRespostaSATInvalida", "(", "'Resposta nao possui o numero '", "'esperado de campos. Esperados %d campos, mas '", "'contem %d: \"%s\"'", "%", "(", "len", "(", "campos", ")", ",", "len", "(", "partes", ")", ",", "as_ascii", "(", "retorno", ")", ",", ")", ")", "else", ":", "relacao_campos", "=", "campos", "resultado", "=", "{", "}", "def", "_enumerate", "(", "sequence", ")", ":", "for", "index", ",", "value", "in", "enumerate", "(", "sequence", ")", ":", "yield", "index", ",", "value", "[", "0", "]", ",", "value", "[", "1", "]", "for", "indice", ",", "campo", ",", "conversor", "in", "_enumerate", "(", "relacao_campos", ")", ":", "resultado", "[", "campo", "]", "=", "conversor", "(", "partes", "[", "indice", "]", ")", "resposta", "=", "classe_resposta", "(", "*", "*", "resultado", ")", "resposta", ".", "atributos", ".", "funcao", "=", "funcao", "resposta", ".", "atributos", ".", "verbatim", "=", "retorno", "if", "manter_verbatim", "else", "None", "return", "resposta" ]
Analisa o retorno (supostamente um retorno de uma função do SAT) conforme o padrão e campos esperados. O retorno deverá possuir dados separados entre si através de pipes e o número de campos deverá coincidir com os campos especificados. O campos devem ser especificados como uma tupla onde cada elemento da tupla deverá ser uma tupla contendo dois elementos: o nome do campo e uma função de conversão a partir de uma string unicode. Por exemplo: .. sourcecode:: python >>> retorno = '123456|08000|SAT em operacao||' >>> resposta = analisar_retorno(retorno, funcao='ConsultarSAT') >>> resposta.numeroSessao 123456 >>> resposta.EEEEE u'08000' >>> resposta.mensagem u'SAT em operacao' >>> resposta.cod u'' >>> resposta.mensagemSEFAZ u'' >>> resposta.atributos.funcao 'ConsultarSAT' >>> resposta.atributos.verbatim '123456|08000|SAT em operacao||' :param unicode retorno: O conteúdo **unicode** da resposta retornada pela função da DLL SAT. :param type classe_resposta: O tipo :class:`RespostaSAT` ou especialização que irá representar o retorno, após sua decomposição em campos. :param tuple campos: Especificação dos campos (nomes) e seus conversores a a partir do tipo ``unicode``. :param list campos_alternativos: Especifica conjuntos de campos alternativos que serão considerados caso o número de campos encontrados na resposta não coincida com o número de campos especificados em ``campos``. Para que a relação alternativa de campos funcione, é importante que cada relação de campos alternativos tenha um número diferente de campos. :param str funcao: Nome da função da DLL SAT que gerou o retorno, que estará disponível nos atributos adicionais à resposta. :param bool manter_verbatim: Se uma cópia verbatim da resposta deverá ser mantida nos atributos adicionais à resposta. :raises ErroRespostaSATInvalida: Se o retorno não estiver em conformidade com o padrão esperado ou se não possuir os campos especificados. :return: Uma instância de :class:`RespostaSAT` ou especialização. :rtype: satcfe.resposta.padrao.RespostaSAT
[ "Analisa", "o", "retorno", "(", "supostamente", "um", "retorno", "de", "uma", "função", "do", "SAT", ")", "conforme", "o", "padrão", "e", "campos", "esperados", ".", "O", "retorno", "deverá", "possuir", "dados", "separados", "entre", "si", "através", "de", "pipes", "e", "o", "número", "de", "campos", "deverá", "coincidir", "com", "os", "campos", "especificados", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L175-L268
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.comunicar_certificado_icpbrasil
def comunicar_certificado_icpbrasil(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.comunicar_certificado_icpbrasil`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ComunicarCertificadoICPBRASIL') if resposta.EEEEE not in ('05000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def comunicar_certificado_icpbrasil(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.comunicar_certificado_icpbrasil`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ComunicarCertificadoICPBRASIL') if resposta.EEEEE not in ('05000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "comunicar_certificado_icpbrasil", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'ComunicarCertificadoICPBRASIL'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'05000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.comunicar_certificado_icpbrasil`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "comunicar_certificado_icpbrasil", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L80-L88
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.consultar_sat
def consultar_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.consultar_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConsultarSAT') if resposta.EEEEE not in ('08000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def consultar_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.consultar_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConsultarSAT') if resposta.EEEEE not in ('08000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "consultar_sat", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'ConsultarSAT'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'08000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.consultar_sat`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "consultar_sat", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L92-L100
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.configurar_interface_de_rede
def configurar_interface_de_rede(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.configurar_interface_de_rede`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConfigurarInterfaceDeRede') if resposta.EEEEE not in ('12000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def configurar_interface_de_rede(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.configurar_interface_de_rede`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConfigurarInterfaceDeRede') if resposta.EEEEE not in ('12000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "configurar_interface_de_rede", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'ConfigurarInterfaceDeRede'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'12000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.configurar_interface_de_rede`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "configurar_interface_de_rede", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L104-L112
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.associar_assinatura
def associar_assinatura(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.associar_assinatura`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='AssociarAssinatura') if resposta.EEEEE not in ('13000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def associar_assinatura(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.associar_assinatura`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='AssociarAssinatura') if resposta.EEEEE not in ('13000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "associar_assinatura", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'AssociarAssinatura'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'13000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.associar_assinatura`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "associar_assinatura", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L116-L124
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.atualizar_software_sat
def atualizar_software_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.atualizar_software_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='AtualizarSoftwareSAT') if resposta.EEEEE not in ('14000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def atualizar_software_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.atualizar_software_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='AtualizarSoftwareSAT') if resposta.EEEEE not in ('14000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "atualizar_software_sat", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'AtualizarSoftwareSAT'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'14000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.atualizar_software_sat`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "atualizar_software_sat", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L128-L136
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.bloquear_sat
def bloquear_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.bloquear_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='BloquearSAT') if resposta.EEEEE not in ('16000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def bloquear_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.bloquear_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='BloquearSAT') if resposta.EEEEE not in ('16000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "bloquear_sat", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'BloquearSAT'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'16000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.bloquear_sat`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "bloquear_sat", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L140-L148
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.desbloquear_sat
def desbloquear_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.desbloquear_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='DesbloquearSAT') if resposta.EEEEE not in ('17000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def desbloquear_sat(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.desbloquear_sat`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='DesbloquearSAT') if resposta.EEEEE not in ('17000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "desbloquear_sat", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'DesbloquearSAT'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'17000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.desbloquear_sat`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "desbloquear_sat", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L152-L160
train
base4sistemas/satcfe
satcfe/resposta/padrao.py
RespostaSAT.trocar_codigo_de_ativacao
def trocar_codigo_de_ativacao(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.trocar_codigo_de_ativacao`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='TrocarCodigoDeAtivacao') if resposta.EEEEE not in ('18000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def trocar_codigo_de_ativacao(retorno): """Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.trocar_codigo_de_ativacao`. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='TrocarCodigoDeAtivacao') if resposta.EEEEE not in ('18000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "trocar_codigo_de_ativacao", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'TrocarCodigoDeAtivacao'", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'18000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaSAT` para o retorno (unicode) da função :meth:`~satcfe.base.FuncoesSAT.trocar_codigo_de_ativacao`.
[ "Constrói", "uma", ":", "class", ":", "RespostaSAT", "para", "o", "retorno", "(", "unicode", ")", "da", "função", ":", "meth", ":", "~satcfe", ".", "base", ".", "FuncoesSAT", ".", "trocar_codigo_de_ativacao", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/padrao.py#L164-L172
train
djgagne/hagelslag
hagelslag/data/ModelOutput.py
ModelOutput.load_data
def load_data(self): """ Load the specified variable from the ensemble files, then close the files. """ if self.ensemble_name.upper() == "SSEF": if self.variable[0:2] == "rh": pressure_level = self.variable[2:] relh_vars = ["sph", "tmp"] relh_vals = {} for var in relh_vars: mg = SSEFModelGrid(self.member_name, self.run_date, var + pressure_level, self.start_date, self.end_date, self.path, single_step=self.single_step) relh_vals[var], units = mg.load_data() mg.close() self.data = relative_humidity_pressure_level(relh_vals["tmp"], relh_vals["sph"], float(pressure_level) * 100) self.units = "%" elif self.variable == "melth": input_vars = ["hgtsfc", "hgt700", "hgt500", "tmp700", "tmp500"] input_vals = {} for var in input_vars: mg = SSEFModelGrid(self.member_name, self.run_date, var, self.start_date, self.end_date, self.path, single_step=self.single_step) input_vals[var], units = mg.load_data() mg.close() self.data = melting_layer_height(input_vals["hgtsfc"], input_vals["hgt700"], input_vals["hgt500"], input_vals["tmp700"], input_vals["tmp500"]) self.units = "m" else: mg = SSEFModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "NCAR": mg = NCARModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "HREFV2": proj_dict, grid_dict = read_ncar_map_file(self.map_file) mapping_data = make_proj_grids(proj_dict, grid_dict) mg = HREFv2ModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, mapping_data, self.sector_ind_path, single_step=self.single_step) self.data, self.units = mg.load_data() elif self.ensemble_name.upper() == "VSE": mg = VSEModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "HRRR": mg = HRRRModelGrid(self.run_date, self.variable, self.start_date, self.end_date, self.path) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "NCARSTORM": mg = NCARStormEventModelGrid(self.run_date, self.variable, self.start_date, self.end_date, self.path) self.data, self.units = mg.load_data() mg.close() else: print(self.ensemble_name + " not supported.")
python
def load_data(self): """ Load the specified variable from the ensemble files, then close the files. """ if self.ensemble_name.upper() == "SSEF": if self.variable[0:2] == "rh": pressure_level = self.variable[2:] relh_vars = ["sph", "tmp"] relh_vals = {} for var in relh_vars: mg = SSEFModelGrid(self.member_name, self.run_date, var + pressure_level, self.start_date, self.end_date, self.path, single_step=self.single_step) relh_vals[var], units = mg.load_data() mg.close() self.data = relative_humidity_pressure_level(relh_vals["tmp"], relh_vals["sph"], float(pressure_level) * 100) self.units = "%" elif self.variable == "melth": input_vars = ["hgtsfc", "hgt700", "hgt500", "tmp700", "tmp500"] input_vals = {} for var in input_vars: mg = SSEFModelGrid(self.member_name, self.run_date, var, self.start_date, self.end_date, self.path, single_step=self.single_step) input_vals[var], units = mg.load_data() mg.close() self.data = melting_layer_height(input_vals["hgtsfc"], input_vals["hgt700"], input_vals["hgt500"], input_vals["tmp700"], input_vals["tmp500"]) self.units = "m" else: mg = SSEFModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "NCAR": mg = NCARModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "HREFV2": proj_dict, grid_dict = read_ncar_map_file(self.map_file) mapping_data = make_proj_grids(proj_dict, grid_dict) mg = HREFv2ModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, mapping_data, self.sector_ind_path, single_step=self.single_step) self.data, self.units = mg.load_data() elif self.ensemble_name.upper() == "VSE": mg = VSEModelGrid(self.member_name, self.run_date, self.variable, self.start_date, self.end_date, self.path, single_step=self.single_step) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "HRRR": mg = HRRRModelGrid(self.run_date, self.variable, self.start_date, self.end_date, self.path) self.data, self.units = mg.load_data() mg.close() elif self.ensemble_name.upper() == "NCARSTORM": mg = NCARStormEventModelGrid(self.run_date, self.variable, self.start_date, self.end_date, self.path) self.data, self.units = mg.load_data() mg.close() else: print(self.ensemble_name + " not supported.")
[ "def", "load_data", "(", "self", ")", ":", "if", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"SSEF\"", ":", "if", "self", ".", "variable", "[", "0", ":", "2", "]", "==", "\"rh\"", ":", "pressure_level", "=", "self", ".", "variable", "[", "2", ":", "]", "relh_vars", "=", "[", "\"sph\"", ",", "\"tmp\"", "]", "relh_vals", "=", "{", "}", "for", "var", "in", "relh_vars", ":", "mg", "=", "SSEFModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "var", "+", "pressure_level", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "single_step", "=", "self", ".", "single_step", ")", "relh_vals", "[", "var", "]", ",", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "self", ".", "data", "=", "relative_humidity_pressure_level", "(", "relh_vals", "[", "\"tmp\"", "]", ",", "relh_vals", "[", "\"sph\"", "]", ",", "float", "(", "pressure_level", ")", "*", "100", ")", "self", ".", "units", "=", "\"%\"", "elif", "self", ".", "variable", "==", "\"melth\"", ":", "input_vars", "=", "[", "\"hgtsfc\"", ",", "\"hgt700\"", ",", "\"hgt500\"", ",", "\"tmp700\"", ",", "\"tmp500\"", "]", "input_vals", "=", "{", "}", "for", "var", "in", "input_vars", ":", "mg", "=", "SSEFModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "var", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "single_step", "=", "self", ".", "single_step", ")", "input_vals", "[", "var", "]", ",", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "self", ".", "data", "=", "melting_layer_height", "(", "input_vals", "[", "\"hgtsfc\"", "]", ",", "input_vals", "[", "\"hgt700\"", "]", ",", "input_vals", "[", "\"hgt500\"", "]", ",", "input_vals", "[", "\"tmp700\"", "]", ",", "input_vals", "[", "\"tmp500\"", "]", ")", "self", ".", "units", "=", "\"m\"", "else", ":", "mg", "=", "SSEFModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "single_step", "=", "self", ".", "single_step", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"NCAR\"", ":", "mg", "=", "NCARModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "single_step", "=", "self", ".", "single_step", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"HREFV2\"", ":", "proj_dict", ",", "grid_dict", "=", "read_ncar_map_file", "(", "self", ".", "map_file", ")", "mapping_data", "=", "make_proj_grids", "(", "proj_dict", ",", "grid_dict", ")", "mg", "=", "HREFv2ModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "mapping_data", ",", "self", ".", "sector_ind_path", ",", "single_step", "=", "self", ".", "single_step", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"VSE\"", ":", "mg", "=", "VSEModelGrid", "(", "self", ".", "member_name", ",", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ",", "single_step", "=", "self", ".", "single_step", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"HRRR\"", ":", "mg", "=", "HRRRModelGrid", "(", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"NCARSTORM\"", ":", "mg", "=", "NCARStormEventModelGrid", "(", "self", ".", "run_date", ",", "self", ".", "variable", ",", "self", ".", "start_date", ",", "self", ".", "end_date", ",", "self", ".", "path", ")", "self", ".", "data", ",", "self", ".", "units", "=", "mg", ".", "load_data", "(", ")", "mg", ".", "close", "(", ")", "else", ":", "print", "(", "self", ".", "ensemble_name", "+", "\" not supported.\"", ")" ]
Load the specified variable from the ensemble files, then close the files.
[ "Load", "the", "specified", "variable", "from", "the", "ensemble", "files", "then", "close", "the", "files", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/data/ModelOutput.py#L67-L172
train
djgagne/hagelslag
hagelslag/data/ModelOutput.py
ModelOutput.load_map_info
def load_map_info(self, map_file): """ Load map projection information and create latitude, longitude, x, y, i, and j grids for the projection. Args: map_file: File specifying the projection information. """ if self.ensemble_name.upper() == "SSEF": proj_dict, grid_dict = read_arps_map_file(map_file) self.dx = int(grid_dict["dx"]) mapping_data = make_proj_grids(proj_dict, grid_dict) for m, v in mapping_data.items(): setattr(self, m, v) self.i, self.j = np.indices(self.lon.shape) self.proj = get_proj_obj(proj_dict) elif self.ensemble_name.upper() in ["NCAR", "NCARSTORM", "HRRR", "VSE", "HREFV2"]: proj_dict, grid_dict = read_ncar_map_file(map_file) if self.member_name[0:7] == "1km_pbl": # Don't just look at the first 3 characters. You have to differentiate '1km_pbl1' and '1km_on_3km_pbl1' grid_dict["dx"] = 1000 grid_dict["dy"] = 1000 grid_dict["sw_lon"] = 258.697 grid_dict["sw_lat"] = 23.999 grid_dict["ne_lon"] = 282.868269206236 grid_dict["ne_lat"] = 36.4822338520542 self.dx = int(grid_dict["dx"]) mapping_data = make_proj_grids(proj_dict, grid_dict) for m, v in mapping_data.items(): setattr(self, m, v) self.i, self.j = np.indices(self.lon.shape) self.proj = get_proj_obj(proj_dict)
python
def load_map_info(self, map_file): """ Load map projection information and create latitude, longitude, x, y, i, and j grids for the projection. Args: map_file: File specifying the projection information. """ if self.ensemble_name.upper() == "SSEF": proj_dict, grid_dict = read_arps_map_file(map_file) self.dx = int(grid_dict["dx"]) mapping_data = make_proj_grids(proj_dict, grid_dict) for m, v in mapping_data.items(): setattr(self, m, v) self.i, self.j = np.indices(self.lon.shape) self.proj = get_proj_obj(proj_dict) elif self.ensemble_name.upper() in ["NCAR", "NCARSTORM", "HRRR", "VSE", "HREFV2"]: proj_dict, grid_dict = read_ncar_map_file(map_file) if self.member_name[0:7] == "1km_pbl": # Don't just look at the first 3 characters. You have to differentiate '1km_pbl1' and '1km_on_3km_pbl1' grid_dict["dx"] = 1000 grid_dict["dy"] = 1000 grid_dict["sw_lon"] = 258.697 grid_dict["sw_lat"] = 23.999 grid_dict["ne_lon"] = 282.868269206236 grid_dict["ne_lat"] = 36.4822338520542 self.dx = int(grid_dict["dx"]) mapping_data = make_proj_grids(proj_dict, grid_dict) for m, v in mapping_data.items(): setattr(self, m, v) self.i, self.j = np.indices(self.lon.shape) self.proj = get_proj_obj(proj_dict)
[ "def", "load_map_info", "(", "self", ",", "map_file", ")", ":", "if", "self", ".", "ensemble_name", ".", "upper", "(", ")", "==", "\"SSEF\"", ":", "proj_dict", ",", "grid_dict", "=", "read_arps_map_file", "(", "map_file", ")", "self", ".", "dx", "=", "int", "(", "grid_dict", "[", "\"dx\"", "]", ")", "mapping_data", "=", "make_proj_grids", "(", "proj_dict", ",", "grid_dict", ")", "for", "m", ",", "v", "in", "mapping_data", ".", "items", "(", ")", ":", "setattr", "(", "self", ",", "m", ",", "v", ")", "self", ".", "i", ",", "self", ".", "j", "=", "np", ".", "indices", "(", "self", ".", "lon", ".", "shape", ")", "self", ".", "proj", "=", "get_proj_obj", "(", "proj_dict", ")", "elif", "self", ".", "ensemble_name", ".", "upper", "(", ")", "in", "[", "\"NCAR\"", ",", "\"NCARSTORM\"", ",", "\"HRRR\"", ",", "\"VSE\"", ",", "\"HREFV2\"", "]", ":", "proj_dict", ",", "grid_dict", "=", "read_ncar_map_file", "(", "map_file", ")", "if", "self", ".", "member_name", "[", "0", ":", "7", "]", "==", "\"1km_pbl\"", ":", "# Don't just look at the first 3 characters. You have to differentiate '1km_pbl1' and '1km_on_3km_pbl1'", "grid_dict", "[", "\"dx\"", "]", "=", "1000", "grid_dict", "[", "\"dy\"", "]", "=", "1000", "grid_dict", "[", "\"sw_lon\"", "]", "=", "258.697", "grid_dict", "[", "\"sw_lat\"", "]", "=", "23.999", "grid_dict", "[", "\"ne_lon\"", "]", "=", "282.868269206236", "grid_dict", "[", "\"ne_lat\"", "]", "=", "36.4822338520542", "self", ".", "dx", "=", "int", "(", "grid_dict", "[", "\"dx\"", "]", ")", "mapping_data", "=", "make_proj_grids", "(", "proj_dict", ",", "grid_dict", ")", "for", "m", ",", "v", "in", "mapping_data", ".", "items", "(", ")", ":", "setattr", "(", "self", ",", "m", ",", "v", ")", "self", ".", "i", ",", "self", ".", "j", "=", "np", ".", "indices", "(", "self", ".", "lon", ".", "shape", ")", "self", ".", "proj", "=", "get_proj_obj", "(", "proj_dict", ")" ]
Load map projection information and create latitude, longitude, x, y, i, and j grids for the projection. Args: map_file: File specifying the projection information.
[ "Load", "map", "projection", "information", "and", "create", "latitude", "longitude", "x", "y", "i", "and", "j", "grids", "for", "the", "projection", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/data/ModelOutput.py#L174-L204
train
djgagne/hagelslag
hagelslag/processing/STObject.py
read_geojson
def read_geojson(filename): """ Reads a geojson file containing an STObject and initializes a new STObject from the information in the file. Args: filename: Name of the geojson file Returns: an STObject """ json_file = open(filename) data = json.load(json_file) json_file.close() times = data["properties"]["times"] main_data = dict(timesteps=[], masks=[], x=[], y=[], i=[], j=[]) attribute_data = dict() for feature in data["features"]: for main_name in main_data.keys(): main_data[main_name].append(np.array(feature["properties"][main_name])) for k, v in feature["properties"]["attributes"].items(): if k not in attribute_data.keys(): attribute_data[k] = [np.array(v)] else: attribute_data[k].append(np.array(v)) kwargs = {} for kw in ["dx", "step", "u", "v"]: if kw in data["properties"].keys(): kwargs[kw] = data["properties"][kw] sto = STObject(main_data["timesteps"], main_data["masks"], main_data["x"], main_data["y"], main_data["i"], main_data["j"], times[0], times[-1], **kwargs) for k, v in attribute_data.items(): sto.attributes[k] = v return sto
python
def read_geojson(filename): """ Reads a geojson file containing an STObject and initializes a new STObject from the information in the file. Args: filename: Name of the geojson file Returns: an STObject """ json_file = open(filename) data = json.load(json_file) json_file.close() times = data["properties"]["times"] main_data = dict(timesteps=[], masks=[], x=[], y=[], i=[], j=[]) attribute_data = dict() for feature in data["features"]: for main_name in main_data.keys(): main_data[main_name].append(np.array(feature["properties"][main_name])) for k, v in feature["properties"]["attributes"].items(): if k not in attribute_data.keys(): attribute_data[k] = [np.array(v)] else: attribute_data[k].append(np.array(v)) kwargs = {} for kw in ["dx", "step", "u", "v"]: if kw in data["properties"].keys(): kwargs[kw] = data["properties"][kw] sto = STObject(main_data["timesteps"], main_data["masks"], main_data["x"], main_data["y"], main_data["i"], main_data["j"], times[0], times[-1], **kwargs) for k, v in attribute_data.items(): sto.attributes[k] = v return sto
[ "def", "read_geojson", "(", "filename", ")", ":", "json_file", "=", "open", "(", "filename", ")", "data", "=", "json", ".", "load", "(", "json_file", ")", "json_file", ".", "close", "(", ")", "times", "=", "data", "[", "\"properties\"", "]", "[", "\"times\"", "]", "main_data", "=", "dict", "(", "timesteps", "=", "[", "]", ",", "masks", "=", "[", "]", ",", "x", "=", "[", "]", ",", "y", "=", "[", "]", ",", "i", "=", "[", "]", ",", "j", "=", "[", "]", ")", "attribute_data", "=", "dict", "(", ")", "for", "feature", "in", "data", "[", "\"features\"", "]", ":", "for", "main_name", "in", "main_data", ".", "keys", "(", ")", ":", "main_data", "[", "main_name", "]", ".", "append", "(", "np", ".", "array", "(", "feature", "[", "\"properties\"", "]", "[", "main_name", "]", ")", ")", "for", "k", ",", "v", "in", "feature", "[", "\"properties\"", "]", "[", "\"attributes\"", "]", ".", "items", "(", ")", ":", "if", "k", "not", "in", "attribute_data", ".", "keys", "(", ")", ":", "attribute_data", "[", "k", "]", "=", "[", "np", ".", "array", "(", "v", ")", "]", "else", ":", "attribute_data", "[", "k", "]", ".", "append", "(", "np", ".", "array", "(", "v", ")", ")", "kwargs", "=", "{", "}", "for", "kw", "in", "[", "\"dx\"", ",", "\"step\"", ",", "\"u\"", ",", "\"v\"", "]", ":", "if", "kw", "in", "data", "[", "\"properties\"", "]", ".", "keys", "(", ")", ":", "kwargs", "[", "kw", "]", "=", "data", "[", "\"properties\"", "]", "[", "kw", "]", "sto", "=", "STObject", "(", "main_data", "[", "\"timesteps\"", "]", ",", "main_data", "[", "\"masks\"", "]", ",", "main_data", "[", "\"x\"", "]", ",", "main_data", "[", "\"y\"", "]", ",", "main_data", "[", "\"i\"", "]", ",", "main_data", "[", "\"j\"", "]", ",", "times", "[", "0", "]", ",", "times", "[", "-", "1", "]", ",", "*", "*", "kwargs", ")", "for", "k", ",", "v", "in", "attribute_data", ".", "items", "(", ")", ":", "sto", ".", "attributes", "[", "k", "]", "=", "v", "return", "sto" ]
Reads a geojson file containing an STObject and initializes a new STObject from the information in the file. Args: filename: Name of the geojson file Returns: an STObject
[ "Reads", "a", "geojson", "file", "containing", "an", "STObject", "and", "initializes", "a", "new", "STObject", "from", "the", "information", "in", "the", "file", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L540-L572
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.center_of_mass
def center_of_mass(self, time): """ Calculate the center of mass at a given timestep. Args: time: Time at which the center of mass calculation is performed Returns: The x- and y-coordinates of the center of mass. """ if self.start_time <= time <= self.end_time: diff = time - self.start_time valid = np.flatnonzero(self.masks[diff] != 0) if valid.size > 0: com_x = 1.0 / self.timesteps[diff].ravel()[valid].sum() * np.sum(self.timesteps[diff].ravel()[valid] * self.x[diff].ravel()[valid]) com_y = 1.0 / self.timesteps[diff].ravel()[valid].sum() * np.sum(self.timesteps[diff].ravel()[valid] * self.y[diff].ravel()[valid]) else: com_x = np.mean(self.x[diff]) com_y = np.mean(self.y[diff]) else: com_x = None com_y = None return com_x, com_y
python
def center_of_mass(self, time): """ Calculate the center of mass at a given timestep. Args: time: Time at which the center of mass calculation is performed Returns: The x- and y-coordinates of the center of mass. """ if self.start_time <= time <= self.end_time: diff = time - self.start_time valid = np.flatnonzero(self.masks[diff] != 0) if valid.size > 0: com_x = 1.0 / self.timesteps[diff].ravel()[valid].sum() * np.sum(self.timesteps[diff].ravel()[valid] * self.x[diff].ravel()[valid]) com_y = 1.0 / self.timesteps[diff].ravel()[valid].sum() * np.sum(self.timesteps[diff].ravel()[valid] * self.y[diff].ravel()[valid]) else: com_x = np.mean(self.x[diff]) com_y = np.mean(self.y[diff]) else: com_x = None com_y = None return com_x, com_y
[ "def", "center_of_mass", "(", "self", ",", "time", ")", ":", "if", "self", ".", "start_time", "<=", "time", "<=", "self", ".", "end_time", ":", "diff", "=", "time", "-", "self", ".", "start_time", "valid", "=", "np", ".", "flatnonzero", "(", "self", ".", "masks", "[", "diff", "]", "!=", "0", ")", "if", "valid", ".", "size", ">", "0", ":", "com_x", "=", "1.0", "/", "self", ".", "timesteps", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", ".", "sum", "(", ")", "*", "np", ".", "sum", "(", "self", ".", "timesteps", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", "*", "self", ".", "x", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", ")", "com_y", "=", "1.0", "/", "self", ".", "timesteps", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", ".", "sum", "(", ")", "*", "np", ".", "sum", "(", "self", ".", "timesteps", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", "*", "self", ".", "y", "[", "diff", "]", ".", "ravel", "(", ")", "[", "valid", "]", ")", "else", ":", "com_x", "=", "np", ".", "mean", "(", "self", ".", "x", "[", "diff", "]", ")", "com_y", "=", "np", ".", "mean", "(", "self", ".", "y", "[", "diff", "]", ")", "else", ":", "com_x", "=", "None", "com_y", "=", "None", "return", "com_x", ",", "com_y" ]
Calculate the center of mass at a given timestep. Args: time: Time at which the center of mass calculation is performed Returns: The x- and y-coordinates of the center of mass.
[ "Calculate", "the", "center", "of", "mass", "at", "a", "given", "timestep", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L78-L102
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.trajectory
def trajectory(self): """ Calculates the center of mass for each time step and outputs an array Returns: """ traj = np.zeros((2, self.times.size)) for t, time in enumerate(self.times): traj[:, t] = self.center_of_mass(time) return traj
python
def trajectory(self): """ Calculates the center of mass for each time step and outputs an array Returns: """ traj = np.zeros((2, self.times.size)) for t, time in enumerate(self.times): traj[:, t] = self.center_of_mass(time) return traj
[ "def", "trajectory", "(", "self", ")", ":", "traj", "=", "np", ".", "zeros", "(", "(", "2", ",", "self", ".", "times", ".", "size", ")", ")", "for", "t", ",", "time", "in", "enumerate", "(", "self", ".", "times", ")", ":", "traj", "[", ":", ",", "t", "]", "=", "self", ".", "center_of_mass", "(", "time", ")", "return", "traj" ]
Calculates the center of mass for each time step and outputs an array Returns:
[ "Calculates", "the", "center", "of", "mass", "for", "each", "time", "step", "and", "outputs", "an", "array" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L143-L153
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.get_corner
def get_corner(self, time): """ Gets the corner array indices of the STObject at a given time that corresponds to the upper left corner of the bounding box for the STObject. Args: time: time at which the corner is being extracted. Returns: corner index. """ if self.start_time <= time <= self.end_time: diff = time - self.start_time return self.i[diff][0, 0], self.j[diff][0, 0] else: return -1, -1
python
def get_corner(self, time): """ Gets the corner array indices of the STObject at a given time that corresponds to the upper left corner of the bounding box for the STObject. Args: time: time at which the corner is being extracted. Returns: corner index. """ if self.start_time <= time <= self.end_time: diff = time - self.start_time return self.i[diff][0, 0], self.j[diff][0, 0] else: return -1, -1
[ "def", "get_corner", "(", "self", ",", "time", ")", ":", "if", "self", ".", "start_time", "<=", "time", "<=", "self", ".", "end_time", ":", "diff", "=", "time", "-", "self", ".", "start_time", "return", "self", ".", "i", "[", "diff", "]", "[", "0", ",", "0", "]", ",", "self", ".", "j", "[", "diff", "]", "[", "0", ",", "0", "]", "else", ":", "return", "-", "1", ",", "-", "1" ]
Gets the corner array indices of the STObject at a given time that corresponds to the upper left corner of the bounding box for the STObject. Args: time: time at which the corner is being extracted. Returns: corner index.
[ "Gets", "the", "corner", "array", "indices", "of", "the", "STObject", "at", "a", "given", "time", "that", "corresponds", "to", "the", "upper", "left", "corner", "of", "the", "bounding", "box", "for", "the", "STObject", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L155-L170
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.size
def size(self, time): """ Gets the size of the object at a given time. Args: time: Time value being queried. Returns: size of the object in pixels """ if self.start_time <= time <= self.end_time: return self.masks[time - self.start_time].sum() else: return 0
python
def size(self, time): """ Gets the size of the object at a given time. Args: time: Time value being queried. Returns: size of the object in pixels """ if self.start_time <= time <= self.end_time: return self.masks[time - self.start_time].sum() else: return 0
[ "def", "size", "(", "self", ",", "time", ")", ":", "if", "self", ".", "start_time", "<=", "time", "<=", "self", ".", "end_time", ":", "return", "self", ".", "masks", "[", "time", "-", "self", ".", "start_time", "]", ".", "sum", "(", ")", "else", ":", "return", "0" ]
Gets the size of the object at a given time. Args: time: Time value being queried. Returns: size of the object in pixels
[ "Gets", "the", "size", "of", "the", "object", "at", "a", "given", "time", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L172-L185
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.max_size
def max_size(self): """ Gets the largest size of the object over all timesteps. Returns: Maximum size of the object in pixels """ sizes = np.array([m.sum() for m in self.masks]) return sizes.max()
python
def max_size(self): """ Gets the largest size of the object over all timesteps. Returns: Maximum size of the object in pixels """ sizes = np.array([m.sum() for m in self.masks]) return sizes.max()
[ "def", "max_size", "(", "self", ")", ":", "sizes", "=", "np", ".", "array", "(", "[", "m", ".", "sum", "(", ")", "for", "m", "in", "self", ".", "masks", "]", ")", "return", "sizes", ".", "max", "(", ")" ]
Gets the largest size of the object over all timesteps. Returns: Maximum size of the object in pixels
[ "Gets", "the", "largest", "size", "of", "the", "object", "over", "all", "timesteps", ".", "Returns", ":", "Maximum", "size", "of", "the", "object", "in", "pixels" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L187-L195
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.max_intensity
def max_intensity(self, time): """ Calculate the maximum intensity found at a timestep. """ ti = np.where(time == self.times)[0][0] return self.timesteps[ti].max()
python
def max_intensity(self, time): """ Calculate the maximum intensity found at a timestep. """ ti = np.where(time == self.times)[0][0] return self.timesteps[ti].max()
[ "def", "max_intensity", "(", "self", ",", "time", ")", ":", "ti", "=", "np", ".", "where", "(", "time", "==", "self", ".", "times", ")", "[", "0", "]", "[", "0", "]", "return", "self", ".", "timesteps", "[", "ti", "]", ".", "max", "(", ")" ]
Calculate the maximum intensity found at a timestep.
[ "Calculate", "the", "maximum", "intensity", "found", "at", "a", "timestep", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L197-L203
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.extend
def extend(self, step): """ Adds the data from another STObject to this object. Args: step: another STObject being added after the current one in time. """ self.timesteps.extend(step.timesteps) self.masks.extend(step.masks) self.x.extend(step.x) self.y.extend(step.y) self.i.extend(step.i) self.j.extend(step.j) self.end_time = step.end_time self.times = np.arange(self.start_time, self.end_time + self.step, self.step) self.u = np.concatenate((self.u, step.u)) self.v = np.concatenate((self.v, step.v)) for attr in self.attributes.keys(): if attr in step.attributes.keys(): self.attributes[attr].extend(step.attributes[attr])
python
def extend(self, step): """ Adds the data from another STObject to this object. Args: step: another STObject being added after the current one in time. """ self.timesteps.extend(step.timesteps) self.masks.extend(step.masks) self.x.extend(step.x) self.y.extend(step.y) self.i.extend(step.i) self.j.extend(step.j) self.end_time = step.end_time self.times = np.arange(self.start_time, self.end_time + self.step, self.step) self.u = np.concatenate((self.u, step.u)) self.v = np.concatenate((self.v, step.v)) for attr in self.attributes.keys(): if attr in step.attributes.keys(): self.attributes[attr].extend(step.attributes[attr])
[ "def", "extend", "(", "self", ",", "step", ")", ":", "self", ".", "timesteps", ".", "extend", "(", "step", ".", "timesteps", ")", "self", ".", "masks", ".", "extend", "(", "step", ".", "masks", ")", "self", ".", "x", ".", "extend", "(", "step", ".", "x", ")", "self", ".", "y", ".", "extend", "(", "step", ".", "y", ")", "self", ".", "i", ".", "extend", "(", "step", ".", "i", ")", "self", ".", "j", ".", "extend", "(", "step", ".", "j", ")", "self", ".", "end_time", "=", "step", ".", "end_time", "self", ".", "times", "=", "np", ".", "arange", "(", "self", ".", "start_time", ",", "self", ".", "end_time", "+", "self", ".", "step", ",", "self", ".", "step", ")", "self", ".", "u", "=", "np", ".", "concatenate", "(", "(", "self", ".", "u", ",", "step", ".", "u", ")", ")", "self", ".", "v", "=", "np", ".", "concatenate", "(", "(", "self", ".", "v", ",", "step", ".", "v", ")", ")", "for", "attr", "in", "self", ".", "attributes", ".", "keys", "(", ")", ":", "if", "attr", "in", "step", ".", "attributes", ".", "keys", "(", ")", ":", "self", ".", "attributes", "[", "attr", "]", ".", "extend", "(", "step", ".", "attributes", "[", "attr", "]", ")" ]
Adds the data from another STObject to this object. Args: step: another STObject being added after the current one in time.
[ "Adds", "the", "data", "from", "another", "STObject", "to", "this", "object", ".", "Args", ":", "step", ":", "another", "STObject", "being", "added", "after", "the", "current", "one", "in", "time", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L205-L224
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.boundary_polygon
def boundary_polygon(self, time): """ Get coordinates of object boundary in counter-clockwise order """ ti = np.where(time == self.times)[0][0] com_x, com_y = self.center_of_mass(time) # If at least one point along perimeter of the mask rectangle is unmasked, find_boundaries() works. # But if all perimeter points are masked, find_boundaries() does not find the object. # Therefore, pad the mask with zeroes first and run find_boundaries on the padded array. padded_mask = np.pad(self.masks[ti], 1, 'constant', constant_values=0) chull = convex_hull_image(padded_mask) boundary_image = find_boundaries(chull, mode='inner', background=0) # Now remove the padding. boundary_image = boundary_image[1:-1,1:-1] boundary_x = self.x[ti].ravel()[boundary_image.ravel()] boundary_y = self.y[ti].ravel()[boundary_image.ravel()] r = np.sqrt((boundary_x - com_x) ** 2 + (boundary_y - com_y) ** 2) theta = np.arctan2((boundary_y - com_y), (boundary_x - com_x)) * 180.0 / np.pi + 360 polar_coords = np.array([(r[x], theta[x]) for x in range(r.size)], dtype=[('r', 'f4'), ('theta', 'f4')]) coord_order = np.argsort(polar_coords, order=['theta', 'r']) ordered_coords = np.vstack([boundary_x[coord_order], boundary_y[coord_order]]) return ordered_coords
python
def boundary_polygon(self, time): """ Get coordinates of object boundary in counter-clockwise order """ ti = np.where(time == self.times)[0][0] com_x, com_y = self.center_of_mass(time) # If at least one point along perimeter of the mask rectangle is unmasked, find_boundaries() works. # But if all perimeter points are masked, find_boundaries() does not find the object. # Therefore, pad the mask with zeroes first and run find_boundaries on the padded array. padded_mask = np.pad(self.masks[ti], 1, 'constant', constant_values=0) chull = convex_hull_image(padded_mask) boundary_image = find_boundaries(chull, mode='inner', background=0) # Now remove the padding. boundary_image = boundary_image[1:-1,1:-1] boundary_x = self.x[ti].ravel()[boundary_image.ravel()] boundary_y = self.y[ti].ravel()[boundary_image.ravel()] r = np.sqrt((boundary_x - com_x) ** 2 + (boundary_y - com_y) ** 2) theta = np.arctan2((boundary_y - com_y), (boundary_x - com_x)) * 180.0 / np.pi + 360 polar_coords = np.array([(r[x], theta[x]) for x in range(r.size)], dtype=[('r', 'f4'), ('theta', 'f4')]) coord_order = np.argsort(polar_coords, order=['theta', 'r']) ordered_coords = np.vstack([boundary_x[coord_order], boundary_y[coord_order]]) return ordered_coords
[ "def", "boundary_polygon", "(", "self", ",", "time", ")", ":", "ti", "=", "np", ".", "where", "(", "time", "==", "self", ".", "times", ")", "[", "0", "]", "[", "0", "]", "com_x", ",", "com_y", "=", "self", ".", "center_of_mass", "(", "time", ")", "# If at least one point along perimeter of the mask rectangle is unmasked, find_boundaries() works.", "# But if all perimeter points are masked, find_boundaries() does not find the object.", "# Therefore, pad the mask with zeroes first and run find_boundaries on the padded array.", "padded_mask", "=", "np", ".", "pad", "(", "self", ".", "masks", "[", "ti", "]", ",", "1", ",", "'constant'", ",", "constant_values", "=", "0", ")", "chull", "=", "convex_hull_image", "(", "padded_mask", ")", "boundary_image", "=", "find_boundaries", "(", "chull", ",", "mode", "=", "'inner'", ",", "background", "=", "0", ")", "# Now remove the padding.", "boundary_image", "=", "boundary_image", "[", "1", ":", "-", "1", ",", "1", ":", "-", "1", "]", "boundary_x", "=", "self", ".", "x", "[", "ti", "]", ".", "ravel", "(", ")", "[", "boundary_image", ".", "ravel", "(", ")", "]", "boundary_y", "=", "self", ".", "y", "[", "ti", "]", ".", "ravel", "(", ")", "[", "boundary_image", ".", "ravel", "(", ")", "]", "r", "=", "np", ".", "sqrt", "(", "(", "boundary_x", "-", "com_x", ")", "**", "2", "+", "(", "boundary_y", "-", "com_y", ")", "**", "2", ")", "theta", "=", "np", ".", "arctan2", "(", "(", "boundary_y", "-", "com_y", ")", ",", "(", "boundary_x", "-", "com_x", ")", ")", "*", "180.0", "/", "np", ".", "pi", "+", "360", "polar_coords", "=", "np", ".", "array", "(", "[", "(", "r", "[", "x", "]", ",", "theta", "[", "x", "]", ")", "for", "x", "in", "range", "(", "r", ".", "size", ")", "]", ",", "dtype", "=", "[", "(", "'r'", ",", "'f4'", ")", ",", "(", "'theta'", ",", "'f4'", ")", "]", ")", "coord_order", "=", "np", ".", "argsort", "(", "polar_coords", ",", "order", "=", "[", "'theta'", ",", "'r'", "]", ")", "ordered_coords", "=", "np", ".", "vstack", "(", "[", "boundary_x", "[", "coord_order", "]", ",", "boundary_y", "[", "coord_order", "]", "]", ")", "return", "ordered_coords" ]
Get coordinates of object boundary in counter-clockwise order
[ "Get", "coordinates", "of", "object", "boundary", "in", "counter", "-", "clockwise", "order" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L226-L247
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.estimate_motion
def estimate_motion(self, time, intensity_grid, max_u, max_v): """ Estimate the motion of the object with cross-correlation on the intensity values from the previous time step. Args: time: time being evaluated. intensity_grid: 2D array of intensities used in cross correlation. max_u: Maximum x-component of motion. Used to limit search area. max_v: Maximum y-component of motion. Used to limit search area Returns: u, v, and the minimum error. """ ti = np.where(time == self.times)[0][0] mask_vals = np.where(self.masks[ti].ravel() == 1) i_vals = self.i[ti].ravel()[mask_vals] j_vals = self.j[ti].ravel()[mask_vals] obj_vals = self.timesteps[ti].ravel()[mask_vals] u_shifts = np.arange(-max_u, max_u + 1) v_shifts = np.arange(-max_v, max_v + 1) min_error = 99999999999.0 best_u = 0 best_v = 0 for u in u_shifts: j_shift = j_vals - u for v in v_shifts: i_shift = i_vals - v if np.all((0 <= i_shift) & (i_shift < intensity_grid.shape[0]) & (0 <= j_shift) & (j_shift < intensity_grid.shape[1])): shift_vals = intensity_grid[i_shift, j_shift] else: shift_vals = np.zeros(i_shift.shape) # This isn't correlation; it is mean absolute error. error = np.abs(shift_vals - obj_vals).mean() if error < min_error: min_error = error best_u = u * self.dx best_v = v * self.dx # 60 seems arbitrarily high #if min_error > 60: # best_u = 0 # best_v = 0 self.u[ti] = best_u self.v[ti] = best_v return best_u, best_v, min_error
python
def estimate_motion(self, time, intensity_grid, max_u, max_v): """ Estimate the motion of the object with cross-correlation on the intensity values from the previous time step. Args: time: time being evaluated. intensity_grid: 2D array of intensities used in cross correlation. max_u: Maximum x-component of motion. Used to limit search area. max_v: Maximum y-component of motion. Used to limit search area Returns: u, v, and the minimum error. """ ti = np.where(time == self.times)[0][0] mask_vals = np.where(self.masks[ti].ravel() == 1) i_vals = self.i[ti].ravel()[mask_vals] j_vals = self.j[ti].ravel()[mask_vals] obj_vals = self.timesteps[ti].ravel()[mask_vals] u_shifts = np.arange(-max_u, max_u + 1) v_shifts = np.arange(-max_v, max_v + 1) min_error = 99999999999.0 best_u = 0 best_v = 0 for u in u_shifts: j_shift = j_vals - u for v in v_shifts: i_shift = i_vals - v if np.all((0 <= i_shift) & (i_shift < intensity_grid.shape[0]) & (0 <= j_shift) & (j_shift < intensity_grid.shape[1])): shift_vals = intensity_grid[i_shift, j_shift] else: shift_vals = np.zeros(i_shift.shape) # This isn't correlation; it is mean absolute error. error = np.abs(shift_vals - obj_vals).mean() if error < min_error: min_error = error best_u = u * self.dx best_v = v * self.dx # 60 seems arbitrarily high #if min_error > 60: # best_u = 0 # best_v = 0 self.u[ti] = best_u self.v[ti] = best_v return best_u, best_v, min_error
[ "def", "estimate_motion", "(", "self", ",", "time", ",", "intensity_grid", ",", "max_u", ",", "max_v", ")", ":", "ti", "=", "np", ".", "where", "(", "time", "==", "self", ".", "times", ")", "[", "0", "]", "[", "0", "]", "mask_vals", "=", "np", ".", "where", "(", "self", ".", "masks", "[", "ti", "]", ".", "ravel", "(", ")", "==", "1", ")", "i_vals", "=", "self", ".", "i", "[", "ti", "]", ".", "ravel", "(", ")", "[", "mask_vals", "]", "j_vals", "=", "self", ".", "j", "[", "ti", "]", ".", "ravel", "(", ")", "[", "mask_vals", "]", "obj_vals", "=", "self", ".", "timesteps", "[", "ti", "]", ".", "ravel", "(", ")", "[", "mask_vals", "]", "u_shifts", "=", "np", ".", "arange", "(", "-", "max_u", ",", "max_u", "+", "1", ")", "v_shifts", "=", "np", ".", "arange", "(", "-", "max_v", ",", "max_v", "+", "1", ")", "min_error", "=", "99999999999.0", "best_u", "=", "0", "best_v", "=", "0", "for", "u", "in", "u_shifts", ":", "j_shift", "=", "j_vals", "-", "u", "for", "v", "in", "v_shifts", ":", "i_shift", "=", "i_vals", "-", "v", "if", "np", ".", "all", "(", "(", "0", "<=", "i_shift", ")", "&", "(", "i_shift", "<", "intensity_grid", ".", "shape", "[", "0", "]", ")", "&", "(", "0", "<=", "j_shift", ")", "&", "(", "j_shift", "<", "intensity_grid", ".", "shape", "[", "1", "]", ")", ")", ":", "shift_vals", "=", "intensity_grid", "[", "i_shift", ",", "j_shift", "]", "else", ":", "shift_vals", "=", "np", ".", "zeros", "(", "i_shift", ".", "shape", ")", "# This isn't correlation; it is mean absolute error.", "error", "=", "np", ".", "abs", "(", "shift_vals", "-", "obj_vals", ")", ".", "mean", "(", ")", "if", "error", "<", "min_error", ":", "min_error", "=", "error", "best_u", "=", "u", "*", "self", ".", "dx", "best_v", "=", "v", "*", "self", ".", "dx", "# 60 seems arbitrarily high", "#if min_error > 60:", "# best_u = 0", "# best_v = 0", "self", ".", "u", "[", "ti", "]", "=", "best_u", "self", ".", "v", "[", "ti", "]", "=", "best_v", "return", "best_u", ",", "best_v", ",", "min_error" ]
Estimate the motion of the object with cross-correlation on the intensity values from the previous time step. Args: time: time being evaluated. intensity_grid: 2D array of intensities used in cross correlation. max_u: Maximum x-component of motion. Used to limit search area. max_v: Maximum y-component of motion. Used to limit search area Returns: u, v, and the minimum error.
[ "Estimate", "the", "motion", "of", "the", "object", "with", "cross", "-", "correlation", "on", "the", "intensity", "values", "from", "the", "previous", "time", "step", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L249-L293
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.count_overlap
def count_overlap(self, time, other_object, other_time): """ Counts the number of points that overlap between this STObject and another STObject. Used for tracking. """ ti = np.where(time == self.times)[0][0] ma = np.where(self.masks[ti].ravel() == 1) oti = np.where(other_time == other_object.times)[0] obj_coords = np.zeros(self.masks[ti].sum(), dtype=[('x', int), ('y', int)]) other_obj_coords = np.zeros(other_object.masks[oti].sum(), dtype=[('x', int), ('y', int)]) obj_coords['x'] = self.i[ti].ravel()[ma] obj_coords['y'] = self.j[ti].ravel()[ma] other_obj_coords['x'] = other_object.i[oti][other_object.masks[oti] == 1] other_obj_coords['y'] = other_object.j[oti][other_object.masks[oti] == 1] return float(np.intersect1d(obj_coords, other_obj_coords).size) / np.maximum(self.masks[ti].sum(), other_object.masks[oti].sum())
python
def count_overlap(self, time, other_object, other_time): """ Counts the number of points that overlap between this STObject and another STObject. Used for tracking. """ ti = np.where(time == self.times)[0][0] ma = np.where(self.masks[ti].ravel() == 1) oti = np.where(other_time == other_object.times)[0] obj_coords = np.zeros(self.masks[ti].sum(), dtype=[('x', int), ('y', int)]) other_obj_coords = np.zeros(other_object.masks[oti].sum(), dtype=[('x', int), ('y', int)]) obj_coords['x'] = self.i[ti].ravel()[ma] obj_coords['y'] = self.j[ti].ravel()[ma] other_obj_coords['x'] = other_object.i[oti][other_object.masks[oti] == 1] other_obj_coords['y'] = other_object.j[oti][other_object.masks[oti] == 1] return float(np.intersect1d(obj_coords, other_obj_coords).size) / np.maximum(self.masks[ti].sum(), other_object.masks[oti].sum())
[ "def", "count_overlap", "(", "self", ",", "time", ",", "other_object", ",", "other_time", ")", ":", "ti", "=", "np", ".", "where", "(", "time", "==", "self", ".", "times", ")", "[", "0", "]", "[", "0", "]", "ma", "=", "np", ".", "where", "(", "self", ".", "masks", "[", "ti", "]", ".", "ravel", "(", ")", "==", "1", ")", "oti", "=", "np", ".", "where", "(", "other_time", "==", "other_object", ".", "times", ")", "[", "0", "]", "obj_coords", "=", "np", ".", "zeros", "(", "self", ".", "masks", "[", "ti", "]", ".", "sum", "(", ")", ",", "dtype", "=", "[", "(", "'x'", ",", "int", ")", ",", "(", "'y'", ",", "int", ")", "]", ")", "other_obj_coords", "=", "np", ".", "zeros", "(", "other_object", ".", "masks", "[", "oti", "]", ".", "sum", "(", ")", ",", "dtype", "=", "[", "(", "'x'", ",", "int", ")", ",", "(", "'y'", ",", "int", ")", "]", ")", "obj_coords", "[", "'x'", "]", "=", "self", ".", "i", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", "obj_coords", "[", "'y'", "]", "=", "self", ".", "j", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", "other_obj_coords", "[", "'x'", "]", "=", "other_object", ".", "i", "[", "oti", "]", "[", "other_object", ".", "masks", "[", "oti", "]", "==", "1", "]", "other_obj_coords", "[", "'y'", "]", "=", "other_object", ".", "j", "[", "oti", "]", "[", "other_object", ".", "masks", "[", "oti", "]", "==", "1", "]", "return", "float", "(", "np", ".", "intersect1d", "(", "obj_coords", ",", "other_obj_coords", ")", ".", "size", ")", "/", "np", ".", "maximum", "(", "self", ".", "masks", "[", "ti", "]", ".", "sum", "(", ")", ",", "other_object", ".", "masks", "[", "oti", "]", ".", "sum", "(", ")", ")" ]
Counts the number of points that overlap between this STObject and another STObject. Used for tracking.
[ "Counts", "the", "number", "of", "points", "that", "overlap", "between", "this", "STObject", "and", "another", "STObject", ".", "Used", "for", "tracking", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L295-L310
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.extract_attribute_grid
def extract_attribute_grid(self, model_grid, potential=False, future=False): """ Extracts the data from a ModelOutput or ModelGrid object within the bounding box region of the STObject. Args: model_grid: A ModelGrid or ModelOutput Object potential: Extracts from the time before instead of the same time as the object """ if potential: var_name = model_grid.variable + "-potential" timesteps = np.arange(self.start_time - 1, self.end_time) elif future: var_name = model_grid.variable + "-future" timesteps = np.arange(self.start_time + 1, self.end_time + 2) else: var_name = model_grid.variable timesteps = np.arange(self.start_time, self.end_time + 1) self.attributes[var_name] = [] for ti, t in enumerate(timesteps): self.attributes[var_name].append( model_grid.data[t - model_grid.start_hour, self.i[ti], self.j[ti]])
python
def extract_attribute_grid(self, model_grid, potential=False, future=False): """ Extracts the data from a ModelOutput or ModelGrid object within the bounding box region of the STObject. Args: model_grid: A ModelGrid or ModelOutput Object potential: Extracts from the time before instead of the same time as the object """ if potential: var_name = model_grid.variable + "-potential" timesteps = np.arange(self.start_time - 1, self.end_time) elif future: var_name = model_grid.variable + "-future" timesteps = np.arange(self.start_time + 1, self.end_time + 2) else: var_name = model_grid.variable timesteps = np.arange(self.start_time, self.end_time + 1) self.attributes[var_name] = [] for ti, t in enumerate(timesteps): self.attributes[var_name].append( model_grid.data[t - model_grid.start_hour, self.i[ti], self.j[ti]])
[ "def", "extract_attribute_grid", "(", "self", ",", "model_grid", ",", "potential", "=", "False", ",", "future", "=", "False", ")", ":", "if", "potential", ":", "var_name", "=", "model_grid", ".", "variable", "+", "\"-potential\"", "timesteps", "=", "np", ".", "arange", "(", "self", ".", "start_time", "-", "1", ",", "self", ".", "end_time", ")", "elif", "future", ":", "var_name", "=", "model_grid", ".", "variable", "+", "\"-future\"", "timesteps", "=", "np", ".", "arange", "(", "self", ".", "start_time", "+", "1", ",", "self", ".", "end_time", "+", "2", ")", "else", ":", "var_name", "=", "model_grid", ".", "variable", "timesteps", "=", "np", ".", "arange", "(", "self", ".", "start_time", ",", "self", ".", "end_time", "+", "1", ")", "self", ".", "attributes", "[", "var_name", "]", "=", "[", "]", "for", "ti", ",", "t", "in", "enumerate", "(", "timesteps", ")", ":", "self", ".", "attributes", "[", "var_name", "]", ".", "append", "(", "model_grid", ".", "data", "[", "t", "-", "model_grid", ".", "start_hour", ",", "self", ".", "i", "[", "ti", "]", ",", "self", ".", "j", "[", "ti", "]", "]", ")" ]
Extracts the data from a ModelOutput or ModelGrid object within the bounding box region of the STObject. Args: model_grid: A ModelGrid or ModelOutput Object potential: Extracts from the time before instead of the same time as the object
[ "Extracts", "the", "data", "from", "a", "ModelOutput", "or", "ModelGrid", "object", "within", "the", "bounding", "box", "region", "of", "the", "STObject", ".", "Args", ":", "model_grid", ":", "A", "ModelGrid", "or", "ModelOutput", "Object", "potential", ":", "Extracts", "from", "the", "time", "before", "instead", "of", "the", "same", "time", "as", "the", "object" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L312-L333
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.extract_attribute_array
def extract_attribute_array(self, data_array, var_name): """ Extracts data from a 2D array that has the same dimensions as the grid used to identify the object. Args: data_array: 2D numpy array """ if var_name not in self.attributes.keys(): self.attributes[var_name] = [] for t in range(self.times.size): self.attributes[var_name].append(data_array[self.i[t], self.j[t]])
python
def extract_attribute_array(self, data_array, var_name): """ Extracts data from a 2D array that has the same dimensions as the grid used to identify the object. Args: data_array: 2D numpy array """ if var_name not in self.attributes.keys(): self.attributes[var_name] = [] for t in range(self.times.size): self.attributes[var_name].append(data_array[self.i[t], self.j[t]])
[ "def", "extract_attribute_array", "(", "self", ",", "data_array", ",", "var_name", ")", ":", "if", "var_name", "not", "in", "self", ".", "attributes", ".", "keys", "(", ")", ":", "self", ".", "attributes", "[", "var_name", "]", "=", "[", "]", "for", "t", "in", "range", "(", "self", ".", "times", ".", "size", ")", ":", "self", ".", "attributes", "[", "var_name", "]", ".", "append", "(", "data_array", "[", "self", ".", "i", "[", "t", "]", ",", "self", ".", "j", "[", "t", "]", "]", ")" ]
Extracts data from a 2D array that has the same dimensions as the grid used to identify the object. Args: data_array: 2D numpy array
[ "Extracts", "data", "from", "a", "2D", "array", "that", "has", "the", "same", "dimensions", "as", "the", "grid", "used", "to", "identify", "the", "object", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L335-L346
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.extract_tendency_grid
def extract_tendency_grid(self, model_grid): """ Extracts the difference in model outputs Args: model_grid: ModelOutput or ModelGrid object. """ var_name = model_grid.variable + "-tendency" self.attributes[var_name] = [] timesteps = np.arange(self.start_time, self.end_time + 1) for ti, t in enumerate(timesteps): t_index = t - model_grid.start_hour self.attributes[var_name].append( model_grid.data[t_index, self.i[ti], self.j[ti]] - model_grid.data[t_index - 1, self.i[ti], self.j[ti]] )
python
def extract_tendency_grid(self, model_grid): """ Extracts the difference in model outputs Args: model_grid: ModelOutput or ModelGrid object. """ var_name = model_grid.variable + "-tendency" self.attributes[var_name] = [] timesteps = np.arange(self.start_time, self.end_time + 1) for ti, t in enumerate(timesteps): t_index = t - model_grid.start_hour self.attributes[var_name].append( model_grid.data[t_index, self.i[ti], self.j[ti]] - model_grid.data[t_index - 1, self.i[ti], self.j[ti]] )
[ "def", "extract_tendency_grid", "(", "self", ",", "model_grid", ")", ":", "var_name", "=", "model_grid", ".", "variable", "+", "\"-tendency\"", "self", ".", "attributes", "[", "var_name", "]", "=", "[", "]", "timesteps", "=", "np", ".", "arange", "(", "self", ".", "start_time", ",", "self", ".", "end_time", "+", "1", ")", "for", "ti", ",", "t", "in", "enumerate", "(", "timesteps", ")", ":", "t_index", "=", "t", "-", "model_grid", ".", "start_hour", "self", ".", "attributes", "[", "var_name", "]", ".", "append", "(", "model_grid", ".", "data", "[", "t_index", ",", "self", ".", "i", "[", "ti", "]", ",", "self", ".", "j", "[", "ti", "]", "]", "-", "model_grid", ".", "data", "[", "t_index", "-", "1", ",", "self", ".", "i", "[", "ti", "]", ",", "self", ".", "j", "[", "ti", "]", "]", ")" ]
Extracts the difference in model outputs Args: model_grid: ModelOutput or ModelGrid object.
[ "Extracts", "the", "difference", "in", "model", "outputs" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L349-L364
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.calc_attribute_statistics
def calc_attribute_statistics(self, statistic_name): """ Calculates summary statistics over the domains of each attribute. Args: statistic_name (string): numpy statistic, such as mean, std, max, min Returns: dict of statistics from each attribute grid. """ stats = {} for var, grids in self.attributes.items(): if len(grids) > 1: stats[var] = getattr(np.array([getattr(np.ma.array(x, mask=self.masks[t] == 0), statistic_name)() for t, x in enumerate(grids)]), statistic_name)() else: stats[var] = getattr(np.ma.array(grids[0], mask=self.masks[0] == 0), statistic_name)() return stats
python
def calc_attribute_statistics(self, statistic_name): """ Calculates summary statistics over the domains of each attribute. Args: statistic_name (string): numpy statistic, such as mean, std, max, min Returns: dict of statistics from each attribute grid. """ stats = {} for var, grids in self.attributes.items(): if len(grids) > 1: stats[var] = getattr(np.array([getattr(np.ma.array(x, mask=self.masks[t] == 0), statistic_name)() for t, x in enumerate(grids)]), statistic_name)() else: stats[var] = getattr(np.ma.array(grids[0], mask=self.masks[0] == 0), statistic_name)() return stats
[ "def", "calc_attribute_statistics", "(", "self", ",", "statistic_name", ")", ":", "stats", "=", "{", "}", "for", "var", ",", "grids", "in", "self", ".", "attributes", ".", "items", "(", ")", ":", "if", "len", "(", "grids", ")", ">", "1", ":", "stats", "[", "var", "]", "=", "getattr", "(", "np", ".", "array", "(", "[", "getattr", "(", "np", ".", "ma", ".", "array", "(", "x", ",", "mask", "=", "self", ".", "masks", "[", "t", "]", "==", "0", ")", ",", "statistic_name", ")", "(", ")", "for", "t", ",", "x", "in", "enumerate", "(", "grids", ")", "]", ")", ",", "statistic_name", ")", "(", ")", "else", ":", "stats", "[", "var", "]", "=", "getattr", "(", "np", ".", "ma", ".", "array", "(", "grids", "[", "0", "]", ",", "mask", "=", "self", ".", "masks", "[", "0", "]", "==", "0", ")", ",", "statistic_name", ")", "(", ")", "return", "stats" ]
Calculates summary statistics over the domains of each attribute. Args: statistic_name (string): numpy statistic, such as mean, std, max, min Returns: dict of statistics from each attribute grid.
[ "Calculates", "summary", "statistics", "over", "the", "domains", "of", "each", "attribute", ".", "Args", ":", "statistic_name", "(", "string", ")", ":", "numpy", "statistic", "such", "as", "mean", "std", "max", "min" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L366-L383
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.calc_attribute_statistic
def calc_attribute_statistic(self, attribute, statistic, time): """ Calculate statistics based on the values of an attribute. The following statistics are supported: mean, max, min, std, ptp (range), median, skew (mean - median), and percentile_(percentile value). Args: attribute: Attribute extracted from model grid statistic: Name of statistic being used. time: timestep of the object being investigated Returns: The value of the statistic """ ti = np.where(self.times == time)[0][0] ma = np.where(self.masks[ti].ravel() == 1) if statistic in ['mean', 'max', 'min', 'std', 'ptp']: stat_val = getattr(self.attributes[attribute][ti].ravel()[ma], statistic)() elif statistic == 'median': stat_val = np.median(self.attributes[attribute][ti].ravel()[ma]) elif statistic == "skew": stat_val = np.mean(self.attributes[attribute][ti].ravel()[ma]) - \ np.median(self.attributes[attribute][ti].ravel()[ma]) elif 'percentile' in statistic: per = int(statistic.split("_")[1]) stat_val = np.percentile(self.attributes[attribute][ti].ravel()[ma], per) elif 'dt' in statistic: stat_name = statistic[:-3] if ti == 0: stat_val = 0 else: stat_val = self.calc_attribute_statistic(attribute, stat_name, time) \ - self.calc_attribute_statistic(attribute, stat_name, time - 1) else: stat_val = np.nan return stat_val
python
def calc_attribute_statistic(self, attribute, statistic, time): """ Calculate statistics based on the values of an attribute. The following statistics are supported: mean, max, min, std, ptp (range), median, skew (mean - median), and percentile_(percentile value). Args: attribute: Attribute extracted from model grid statistic: Name of statistic being used. time: timestep of the object being investigated Returns: The value of the statistic """ ti = np.where(self.times == time)[0][0] ma = np.where(self.masks[ti].ravel() == 1) if statistic in ['mean', 'max', 'min', 'std', 'ptp']: stat_val = getattr(self.attributes[attribute][ti].ravel()[ma], statistic)() elif statistic == 'median': stat_val = np.median(self.attributes[attribute][ti].ravel()[ma]) elif statistic == "skew": stat_val = np.mean(self.attributes[attribute][ti].ravel()[ma]) - \ np.median(self.attributes[attribute][ti].ravel()[ma]) elif 'percentile' in statistic: per = int(statistic.split("_")[1]) stat_val = np.percentile(self.attributes[attribute][ti].ravel()[ma], per) elif 'dt' in statistic: stat_name = statistic[:-3] if ti == 0: stat_val = 0 else: stat_val = self.calc_attribute_statistic(attribute, stat_name, time) \ - self.calc_attribute_statistic(attribute, stat_name, time - 1) else: stat_val = np.nan return stat_val
[ "def", "calc_attribute_statistic", "(", "self", ",", "attribute", ",", "statistic", ",", "time", ")", ":", "ti", "=", "np", ".", "where", "(", "self", ".", "times", "==", "time", ")", "[", "0", "]", "[", "0", "]", "ma", "=", "np", ".", "where", "(", "self", ".", "masks", "[", "ti", "]", ".", "ravel", "(", ")", "==", "1", ")", "if", "statistic", "in", "[", "'mean'", ",", "'max'", ",", "'min'", ",", "'std'", ",", "'ptp'", "]", ":", "stat_val", "=", "getattr", "(", "self", ".", "attributes", "[", "attribute", "]", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ",", "statistic", ")", "(", ")", "elif", "statistic", "==", "'median'", ":", "stat_val", "=", "np", ".", "median", "(", "self", ".", "attributes", "[", "attribute", "]", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ")", "elif", "statistic", "==", "\"skew\"", ":", "stat_val", "=", "np", ".", "mean", "(", "self", ".", "attributes", "[", "attribute", "]", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ")", "-", "np", ".", "median", "(", "self", ".", "attributes", "[", "attribute", "]", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ")", "elif", "'percentile'", "in", "statistic", ":", "per", "=", "int", "(", "statistic", ".", "split", "(", "\"_\"", ")", "[", "1", "]", ")", "stat_val", "=", "np", ".", "percentile", "(", "self", ".", "attributes", "[", "attribute", "]", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ",", "per", ")", "elif", "'dt'", "in", "statistic", ":", "stat_name", "=", "statistic", "[", ":", "-", "3", "]", "if", "ti", "==", "0", ":", "stat_val", "=", "0", "else", ":", "stat_val", "=", "self", ".", "calc_attribute_statistic", "(", "attribute", ",", "stat_name", ",", "time", ")", "-", "self", ".", "calc_attribute_statistic", "(", "attribute", ",", "stat_name", ",", "time", "-", "1", ")", "else", ":", "stat_val", "=", "np", ".", "nan", "return", "stat_val" ]
Calculate statistics based on the values of an attribute. The following statistics are supported: mean, max, min, std, ptp (range), median, skew (mean - median), and percentile_(percentile value). Args: attribute: Attribute extracted from model grid statistic: Name of statistic being used. time: timestep of the object being investigated Returns: The value of the statistic
[ "Calculate", "statistics", "based", "on", "the", "values", "of", "an", "attribute", ".", "The", "following", "statistics", "are", "supported", ":", "mean", "max", "min", "std", "ptp", "(", "range", ")", "median", "skew", "(", "mean", "-", "median", ")", "and", "percentile_", "(", "percentile", "value", ")", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L385-L419
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.calc_timestep_statistic
def calc_timestep_statistic(self, statistic, time): """ Calculate statistics from the primary attribute of the StObject. Args: statistic: statistic being calculated time: Timestep being investigated Returns: Value of the statistic """ ti = np.where(self.times == time)[0][0] ma = np.where(self.masks[ti].ravel() == 1) if statistic in ['mean', 'max', 'min', 'std', 'ptp']: stat_val = getattr(self.timesteps[ti].ravel()[ma], statistic)() elif statistic == 'median': stat_val = np.median(self.timesteps[ti].ravel()[ma]) elif 'percentile' in statistic: per = int(statistic.split("_")[1]) stat_val = np.percentile(self.timesteps[ti].ravel()[ma], per) elif 'dt' in statistic: stat_name = statistic[:-3] if ti == 0: stat_val = 0 else: stat_val = self.calc_timestep_statistic(stat_name, time) -\ self.calc_timestep_statistic(stat_name, time - 1) else: stat_val = np.nan return stat_val
python
def calc_timestep_statistic(self, statistic, time): """ Calculate statistics from the primary attribute of the StObject. Args: statistic: statistic being calculated time: Timestep being investigated Returns: Value of the statistic """ ti = np.where(self.times == time)[0][0] ma = np.where(self.masks[ti].ravel() == 1) if statistic in ['mean', 'max', 'min', 'std', 'ptp']: stat_val = getattr(self.timesteps[ti].ravel()[ma], statistic)() elif statistic == 'median': stat_val = np.median(self.timesteps[ti].ravel()[ma]) elif 'percentile' in statistic: per = int(statistic.split("_")[1]) stat_val = np.percentile(self.timesteps[ti].ravel()[ma], per) elif 'dt' in statistic: stat_name = statistic[:-3] if ti == 0: stat_val = 0 else: stat_val = self.calc_timestep_statistic(stat_name, time) -\ self.calc_timestep_statistic(stat_name, time - 1) else: stat_val = np.nan return stat_val
[ "def", "calc_timestep_statistic", "(", "self", ",", "statistic", ",", "time", ")", ":", "ti", "=", "np", ".", "where", "(", "self", ".", "times", "==", "time", ")", "[", "0", "]", "[", "0", "]", "ma", "=", "np", ".", "where", "(", "self", ".", "masks", "[", "ti", "]", ".", "ravel", "(", ")", "==", "1", ")", "if", "statistic", "in", "[", "'mean'", ",", "'max'", ",", "'min'", ",", "'std'", ",", "'ptp'", "]", ":", "stat_val", "=", "getattr", "(", "self", ".", "timesteps", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ",", "statistic", ")", "(", ")", "elif", "statistic", "==", "'median'", ":", "stat_val", "=", "np", ".", "median", "(", "self", ".", "timesteps", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ")", "elif", "'percentile'", "in", "statistic", ":", "per", "=", "int", "(", "statistic", ".", "split", "(", "\"_\"", ")", "[", "1", "]", ")", "stat_val", "=", "np", ".", "percentile", "(", "self", ".", "timesteps", "[", "ti", "]", ".", "ravel", "(", ")", "[", "ma", "]", ",", "per", ")", "elif", "'dt'", "in", "statistic", ":", "stat_name", "=", "statistic", "[", ":", "-", "3", "]", "if", "ti", "==", "0", ":", "stat_val", "=", "0", "else", ":", "stat_val", "=", "self", ".", "calc_timestep_statistic", "(", "stat_name", ",", "time", ")", "-", "self", ".", "calc_timestep_statistic", "(", "stat_name", ",", "time", "-", "1", ")", "else", ":", "stat_val", "=", "np", ".", "nan", "return", "stat_val" ]
Calculate statistics from the primary attribute of the StObject. Args: statistic: statistic being calculated time: Timestep being investigated Returns: Value of the statistic
[ "Calculate", "statistics", "from", "the", "primary", "attribute", "of", "the", "StObject", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L421-L450
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.calc_shape_statistics
def calc_shape_statistics(self, stat_names): """ Calculate shape statistics using regionprops applied to the object mask. Args: stat_names: List of statistics to be extracted from those calculated by regionprops. Returns: Dictionary of shape statistics """ stats = {} try: all_props = [regionprops(m) for m in self.masks] except TypeError: print(self.masks) exit() for stat in stat_names: stats[stat] = np.mean([p[0][stat] for p in all_props]) return stats
python
def calc_shape_statistics(self, stat_names): """ Calculate shape statistics using regionprops applied to the object mask. Args: stat_names: List of statistics to be extracted from those calculated by regionprops. Returns: Dictionary of shape statistics """ stats = {} try: all_props = [regionprops(m) for m in self.masks] except TypeError: print(self.masks) exit() for stat in stat_names: stats[stat] = np.mean([p[0][stat] for p in all_props]) return stats
[ "def", "calc_shape_statistics", "(", "self", ",", "stat_names", ")", ":", "stats", "=", "{", "}", "try", ":", "all_props", "=", "[", "regionprops", "(", "m", ")", "for", "m", "in", "self", ".", "masks", "]", "except", "TypeError", ":", "print", "(", "self", ".", "masks", ")", "exit", "(", ")", "for", "stat", "in", "stat_names", ":", "stats", "[", "stat", "]", "=", "np", ".", "mean", "(", "[", "p", "[", "0", "]", "[", "stat", "]", "for", "p", "in", "all_props", "]", ")", "return", "stats" ]
Calculate shape statistics using regionprops applied to the object mask. Args: stat_names: List of statistics to be extracted from those calculated by regionprops. Returns: Dictionary of shape statistics
[ "Calculate", "shape", "statistics", "using", "regionprops", "applied", "to", "the", "object", "mask", ".", "Args", ":", "stat_names", ":", "List", "of", "statistics", "to", "be", "extracted", "from", "those", "calculated", "by", "regionprops", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L452-L470
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.calc_shape_step
def calc_shape_step(self, stat_names, time): """ Calculate shape statistics for a single time step Args: stat_names: List of shape statistics calculated from region props time: Time being investigated Returns: List of shape statistics """ ti = np.where(self.times == time)[0][0] props = regionprops(self.masks[ti], self.timesteps[ti])[0] shape_stats = [] for stat_name in stat_names: if "moments_hu" in stat_name: hu_index = int(stat_name.split("_")[-1]) hu_name = "_".join(stat_name.split("_")[:-1]) hu_val = np.log(props[hu_name][hu_index]) if np.isnan(hu_val): shape_stats.append(0) else: shape_stats.append(hu_val) else: shape_stats.append(props[stat_name]) return shape_stats
python
def calc_shape_step(self, stat_names, time): """ Calculate shape statistics for a single time step Args: stat_names: List of shape statistics calculated from region props time: Time being investigated Returns: List of shape statistics """ ti = np.where(self.times == time)[0][0] props = regionprops(self.masks[ti], self.timesteps[ti])[0] shape_stats = [] for stat_name in stat_names: if "moments_hu" in stat_name: hu_index = int(stat_name.split("_")[-1]) hu_name = "_".join(stat_name.split("_")[:-1]) hu_val = np.log(props[hu_name][hu_index]) if np.isnan(hu_val): shape_stats.append(0) else: shape_stats.append(hu_val) else: shape_stats.append(props[stat_name]) return shape_stats
[ "def", "calc_shape_step", "(", "self", ",", "stat_names", ",", "time", ")", ":", "ti", "=", "np", ".", "where", "(", "self", ".", "times", "==", "time", ")", "[", "0", "]", "[", "0", "]", "props", "=", "regionprops", "(", "self", ".", "masks", "[", "ti", "]", ",", "self", ".", "timesteps", "[", "ti", "]", ")", "[", "0", "]", "shape_stats", "=", "[", "]", "for", "stat_name", "in", "stat_names", ":", "if", "\"moments_hu\"", "in", "stat_name", ":", "hu_index", "=", "int", "(", "stat_name", ".", "split", "(", "\"_\"", ")", "[", "-", "1", "]", ")", "hu_name", "=", "\"_\"", ".", "join", "(", "stat_name", ".", "split", "(", "\"_\"", ")", "[", ":", "-", "1", "]", ")", "hu_val", "=", "np", ".", "log", "(", "props", "[", "hu_name", "]", "[", "hu_index", "]", ")", "if", "np", ".", "isnan", "(", "hu_val", ")", ":", "shape_stats", ".", "append", "(", "0", ")", "else", ":", "shape_stats", ".", "append", "(", "hu_val", ")", "else", ":", "shape_stats", ".", "append", "(", "props", "[", "stat_name", "]", ")", "return", "shape_stats" ]
Calculate shape statistics for a single time step Args: stat_names: List of shape statistics calculated from region props time: Time being investigated Returns: List of shape statistics
[ "Calculate", "shape", "statistics", "for", "a", "single", "time", "step" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L472-L498
train
djgagne/hagelslag
hagelslag/processing/STObject.py
STObject.to_geojson
def to_geojson(self, filename, proj, metadata=None): """ Output the data in the STObject to a geoJSON file. Args: filename: Name of the file proj: PyProj object for converting the x and y coordinates back to latitude and longitue values. metadata: Metadata describing the object to be included in the top-level properties. """ if metadata is None: metadata = {} json_obj = {"type": "FeatureCollection", "features": [], "properties": {}} json_obj['properties']['times'] = self.times.tolist() json_obj['properties']['dx'] = self.dx json_obj['properties']['step'] = self.step json_obj['properties']['u'] = self.u.tolist() json_obj['properties']['v'] = self.v.tolist() for k, v in metadata.items(): json_obj['properties'][k] = v for t, time in enumerate(self.times): feature = {"type": "Feature", "geometry": {"type": "Polygon"}, "properties": {}} boundary_coords = self.boundary_polygon(time) lonlat = np.vstack(proj(boundary_coords[0], boundary_coords[1], inverse=True)) lonlat_list = lonlat.T.tolist() if len(lonlat_list) > 0: lonlat_list.append(lonlat_list[0]) feature["geometry"]["coordinates"] = [lonlat_list] for attr in ["timesteps", "masks", "x", "y", "i", "j"]: feature["properties"][attr] = getattr(self, attr)[t].tolist() feature["properties"]["attributes"] = {} for attr_name, steps in self.attributes.items(): feature["properties"]["attributes"][attr_name] = steps[t].tolist() json_obj['features'].append(feature) file_obj = open(filename, "w") json.dump(json_obj, file_obj, indent=1, sort_keys=True) file_obj.close() return
python
def to_geojson(self, filename, proj, metadata=None): """ Output the data in the STObject to a geoJSON file. Args: filename: Name of the file proj: PyProj object for converting the x and y coordinates back to latitude and longitue values. metadata: Metadata describing the object to be included in the top-level properties. """ if metadata is None: metadata = {} json_obj = {"type": "FeatureCollection", "features": [], "properties": {}} json_obj['properties']['times'] = self.times.tolist() json_obj['properties']['dx'] = self.dx json_obj['properties']['step'] = self.step json_obj['properties']['u'] = self.u.tolist() json_obj['properties']['v'] = self.v.tolist() for k, v in metadata.items(): json_obj['properties'][k] = v for t, time in enumerate(self.times): feature = {"type": "Feature", "geometry": {"type": "Polygon"}, "properties": {}} boundary_coords = self.boundary_polygon(time) lonlat = np.vstack(proj(boundary_coords[0], boundary_coords[1], inverse=True)) lonlat_list = lonlat.T.tolist() if len(lonlat_list) > 0: lonlat_list.append(lonlat_list[0]) feature["geometry"]["coordinates"] = [lonlat_list] for attr in ["timesteps", "masks", "x", "y", "i", "j"]: feature["properties"][attr] = getattr(self, attr)[t].tolist() feature["properties"]["attributes"] = {} for attr_name, steps in self.attributes.items(): feature["properties"]["attributes"][attr_name] = steps[t].tolist() json_obj['features'].append(feature) file_obj = open(filename, "w") json.dump(json_obj, file_obj, indent=1, sort_keys=True) file_obj.close() return
[ "def", "to_geojson", "(", "self", ",", "filename", ",", "proj", ",", "metadata", "=", "None", ")", ":", "if", "metadata", "is", "None", ":", "metadata", "=", "{", "}", "json_obj", "=", "{", "\"type\"", ":", "\"FeatureCollection\"", ",", "\"features\"", ":", "[", "]", ",", "\"properties\"", ":", "{", "}", "}", "json_obj", "[", "'properties'", "]", "[", "'times'", "]", "=", "self", ".", "times", ".", "tolist", "(", ")", "json_obj", "[", "'properties'", "]", "[", "'dx'", "]", "=", "self", ".", "dx", "json_obj", "[", "'properties'", "]", "[", "'step'", "]", "=", "self", ".", "step", "json_obj", "[", "'properties'", "]", "[", "'u'", "]", "=", "self", ".", "u", ".", "tolist", "(", ")", "json_obj", "[", "'properties'", "]", "[", "'v'", "]", "=", "self", ".", "v", ".", "tolist", "(", ")", "for", "k", ",", "v", "in", "metadata", ".", "items", "(", ")", ":", "json_obj", "[", "'properties'", "]", "[", "k", "]", "=", "v", "for", "t", ",", "time", "in", "enumerate", "(", "self", ".", "times", ")", ":", "feature", "=", "{", "\"type\"", ":", "\"Feature\"", ",", "\"geometry\"", ":", "{", "\"type\"", ":", "\"Polygon\"", "}", ",", "\"properties\"", ":", "{", "}", "}", "boundary_coords", "=", "self", ".", "boundary_polygon", "(", "time", ")", "lonlat", "=", "np", ".", "vstack", "(", "proj", "(", "boundary_coords", "[", "0", "]", ",", "boundary_coords", "[", "1", "]", ",", "inverse", "=", "True", ")", ")", "lonlat_list", "=", "lonlat", ".", "T", ".", "tolist", "(", ")", "if", "len", "(", "lonlat_list", ")", ">", "0", ":", "lonlat_list", ".", "append", "(", "lonlat_list", "[", "0", "]", ")", "feature", "[", "\"geometry\"", "]", "[", "\"coordinates\"", "]", "=", "[", "lonlat_list", "]", "for", "attr", "in", "[", "\"timesteps\"", ",", "\"masks\"", ",", "\"x\"", ",", "\"y\"", ",", "\"i\"", ",", "\"j\"", "]", ":", "feature", "[", "\"properties\"", "]", "[", "attr", "]", "=", "getattr", "(", "self", ",", "attr", ")", "[", "t", "]", ".", "tolist", "(", ")", "feature", "[", "\"properties\"", "]", "[", "\"attributes\"", "]", "=", "{", "}", "for", "attr_name", ",", "steps", "in", "self", ".", "attributes", ".", "items", "(", ")", ":", "feature", "[", "\"properties\"", "]", "[", "\"attributes\"", "]", "[", "attr_name", "]", "=", "steps", "[", "t", "]", ".", "tolist", "(", ")", "json_obj", "[", "'features'", "]", ".", "append", "(", "feature", ")", "file_obj", "=", "open", "(", "filename", ",", "\"w\"", ")", "json", ".", "dump", "(", "json_obj", ",", "file_obj", ",", "indent", "=", "1", ",", "sort_keys", "=", "True", ")", "file_obj", ".", "close", "(", ")", "return" ]
Output the data in the STObject to a geoJSON file. Args: filename: Name of the file proj: PyProj object for converting the x and y coordinates back to latitude and longitue values. metadata: Metadata describing the object to be included in the top-level properties.
[ "Output", "the", "data", "in", "the", "STObject", "to", "a", "geoJSON", "file", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/STObject.py#L500-L538
train
nion-software/nionswift
nion/swift/model/MemoryStorageSystem.py
MemoryStorageSystem.rewrite_properties
def rewrite_properties(self, properties): """Set the properties and write to disk.""" with self.__library_storage_lock: self.__library_storage = properties self.__write_properties(None)
python
def rewrite_properties(self, properties): """Set the properties and write to disk.""" with self.__library_storage_lock: self.__library_storage = properties self.__write_properties(None)
[ "def", "rewrite_properties", "(", "self", ",", "properties", ")", ":", "with", "self", ".", "__library_storage_lock", ":", "self", ".", "__library_storage", "=", "properties", "self", ".", "__write_properties", "(", "None", ")" ]
Set the properties and write to disk.
[ "Set", "the", "properties", "and", "write", "to", "disk", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/MemoryStorageSystem.py#L113-L117
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.model
def model(self, v=None): "Returns the model of node v" if v is None: v = self.estopping hist = self.hist trace = self.trace(v) ins = None if self._base._probability_calibration is not None: node = hist[-1] node.normalize() X = np.array([x.full_array() for x in node.hy]).T y = np.array(self._base._y_klass.full_array()) mask = np.ones(X.shape[0], dtype=np.bool) mask[np.array(self._base._mask_ts.index)] = False ins = self._base._probability_calibration().fit(X[mask], y[mask]) if self._classifier: nclasses = self._labels.shape[0] else: nclasses = None m = Model(trace, hist, nvar=self._base._nvar, classifier=self._classifier, labels=self._labels, probability_calibration=ins, nclasses=nclasses) return m
python
def model(self, v=None): "Returns the model of node v" if v is None: v = self.estopping hist = self.hist trace = self.trace(v) ins = None if self._base._probability_calibration is not None: node = hist[-1] node.normalize() X = np.array([x.full_array() for x in node.hy]).T y = np.array(self._base._y_klass.full_array()) mask = np.ones(X.shape[0], dtype=np.bool) mask[np.array(self._base._mask_ts.index)] = False ins = self._base._probability_calibration().fit(X[mask], y[mask]) if self._classifier: nclasses = self._labels.shape[0] else: nclasses = None m = Model(trace, hist, nvar=self._base._nvar, classifier=self._classifier, labels=self._labels, probability_calibration=ins, nclasses=nclasses) return m
[ "def", "model", "(", "self", ",", "v", "=", "None", ")", ":", "if", "v", "is", "None", ":", "v", "=", "self", ".", "estopping", "hist", "=", "self", ".", "hist", "trace", "=", "self", ".", "trace", "(", "v", ")", "ins", "=", "None", "if", "self", ".", "_base", ".", "_probability_calibration", "is", "not", "None", ":", "node", "=", "hist", "[", "-", "1", "]", "node", ".", "normalize", "(", ")", "X", "=", "np", ".", "array", "(", "[", "x", ".", "full_array", "(", ")", "for", "x", "in", "node", ".", "hy", "]", ")", ".", "T", "y", "=", "np", ".", "array", "(", "self", ".", "_base", ".", "_y_klass", ".", "full_array", "(", ")", ")", "mask", "=", "np", ".", "ones", "(", "X", ".", "shape", "[", "0", "]", ",", "dtype", "=", "np", ".", "bool", ")", "mask", "[", "np", ".", "array", "(", "self", ".", "_base", ".", "_mask_ts", ".", "index", ")", "]", "=", "False", "ins", "=", "self", ".", "_base", ".", "_probability_calibration", "(", ")", ".", "fit", "(", "X", "[", "mask", "]", ",", "y", "[", "mask", "]", ")", "if", "self", ".", "_classifier", ":", "nclasses", "=", "self", ".", "_labels", ".", "shape", "[", "0", "]", "else", ":", "nclasses", "=", "None", "m", "=", "Model", "(", "trace", ",", "hist", ",", "nvar", "=", "self", ".", "_base", ".", "_nvar", ",", "classifier", "=", "self", ".", "_classifier", ",", "labels", "=", "self", ".", "_labels", ",", "probability_calibration", "=", "ins", ",", "nclasses", "=", "nclasses", ")", "return", "m" ]
Returns the model of node v
[ "Returns", "the", "model", "of", "node", "v" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L252-L274
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.trace
def trace(self, n): "Restore the position in the history of individual v's nodes" trace_map = {} self._trace(n, trace_map) s = list(trace_map.keys()) s.sort() return s
python
def trace(self, n): "Restore the position in the history of individual v's nodes" trace_map = {} self._trace(n, trace_map) s = list(trace_map.keys()) s.sort() return s
[ "def", "trace", "(", "self", ",", "n", ")", ":", "trace_map", "=", "{", "}", "self", ".", "_trace", "(", "n", ",", "trace_map", ")", "s", "=", "list", "(", "trace_map", ".", "keys", "(", ")", ")", "s", ".", "sort", "(", ")", "return", "s" ]
Restore the position in the history of individual v's nodes
[ "Restore", "the", "position", "in", "the", "history", "of", "individual", "v", "s", "nodes" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L276-L282
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.tournament
def tournament(self, negative=False): """Tournament selection and when negative is True it performs negative tournament selection""" if self.generation <= self._random_generations and not negative: return self.random_selection() if not self._negative_selection and negative: return self.random_selection(negative=negative) vars = self.random() fit = [(k, self.population[x].fitness) for k, x in enumerate(vars)] if negative: fit = min(fit, key=lambda x: x[1]) else: fit = max(fit, key=lambda x: x[1]) index = fit[0] return vars[index]
python
def tournament(self, negative=False): """Tournament selection and when negative is True it performs negative tournament selection""" if self.generation <= self._random_generations and not negative: return self.random_selection() if not self._negative_selection and negative: return self.random_selection(negative=negative) vars = self.random() fit = [(k, self.population[x].fitness) for k, x in enumerate(vars)] if negative: fit = min(fit, key=lambda x: x[1]) else: fit = max(fit, key=lambda x: x[1]) index = fit[0] return vars[index]
[ "def", "tournament", "(", "self", ",", "negative", "=", "False", ")", ":", "if", "self", ".", "generation", "<=", "self", ".", "_random_generations", "and", "not", "negative", ":", "return", "self", ".", "random_selection", "(", ")", "if", "not", "self", ".", "_negative_selection", "and", "negative", ":", "return", "self", ".", "random_selection", "(", "negative", "=", "negative", ")", "vars", "=", "self", ".", "random", "(", ")", "fit", "=", "[", "(", "k", ",", "self", ".", "population", "[", "x", "]", ".", "fitness", ")", "for", "k", ",", "x", "in", "enumerate", "(", "vars", ")", "]", "if", "negative", ":", "fit", "=", "min", "(", "fit", ",", "key", "=", "lambda", "x", ":", "x", "[", "1", "]", ")", "else", ":", "fit", "=", "max", "(", "fit", ",", "key", "=", "lambda", "x", ":", "x", "[", "1", "]", ")", "index", "=", "fit", "[", "0", "]", "return", "vars", "[", "index", "]" ]
Tournament selection and when negative is True it performs negative tournament selection
[ "Tournament", "selection", "and", "when", "negative", "is", "True", "it", "performs", "negative", "tournament", "selection" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L319-L333
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.create_population
def create_population(self): "Create the initial population" base = self._base if base._share_inputs: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = used_inputs_var if base._pr_variable == 0: used_inputs_var = SelectNumbers([]) used_inputs_naive = SelectNumbers([x for x in range(base.nvar)]) elif base._pr_variable == 1: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = SelectNumbers([]) else: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = SelectNumbers([x for x in range(base.nvar)]) nb_input = Inputs(base, used_inputs_naive, functions=base._input_functions) while ((base._all_inputs and not base.stopping_criteria_tl()) or (self.popsize < base.popsize and not base.stopping_criteria())): if base._all_inputs and used_inputs_var.empty() and used_inputs_naive.empty(): base._init_popsize = self.popsize break if nb_input.use_all_variables(): v = nb_input.all_variables() if v is None: continue elif not used_inputs_var.empty() and np.random.random() < base._pr_variable: v = self.variable_input(used_inputs_var) if v is None: used_inputs_var.pos = used_inputs_var.size continue elif not used_inputs_naive.empty(): v = nb_input.input() if not used_inputs_var.empty() and used_inputs_naive.empty(): base._pr_variable = 1 if v is None: used_inputs_naive.pos = used_inputs_naive.size if not used_inputs_var.empty(): base._pr_variable = 1 continue else: gen = self.generation self.generation = 0 v = base.random_offspring() self.generation = gen self.add(v)
python
def create_population(self): "Create the initial population" base = self._base if base._share_inputs: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = used_inputs_var if base._pr_variable == 0: used_inputs_var = SelectNumbers([]) used_inputs_naive = SelectNumbers([x for x in range(base.nvar)]) elif base._pr_variable == 1: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = SelectNumbers([]) else: used_inputs_var = SelectNumbers([x for x in range(base.nvar)]) used_inputs_naive = SelectNumbers([x for x in range(base.nvar)]) nb_input = Inputs(base, used_inputs_naive, functions=base._input_functions) while ((base._all_inputs and not base.stopping_criteria_tl()) or (self.popsize < base.popsize and not base.stopping_criteria())): if base._all_inputs and used_inputs_var.empty() and used_inputs_naive.empty(): base._init_popsize = self.popsize break if nb_input.use_all_variables(): v = nb_input.all_variables() if v is None: continue elif not used_inputs_var.empty() and np.random.random() < base._pr_variable: v = self.variable_input(used_inputs_var) if v is None: used_inputs_var.pos = used_inputs_var.size continue elif not used_inputs_naive.empty(): v = nb_input.input() if not used_inputs_var.empty() and used_inputs_naive.empty(): base._pr_variable = 1 if v is None: used_inputs_naive.pos = used_inputs_naive.size if not used_inputs_var.empty(): base._pr_variable = 1 continue else: gen = self.generation self.generation = 0 v = base.random_offspring() self.generation = gen self.add(v)
[ "def", "create_population", "(", "self", ")", ":", "base", "=", "self", ".", "_base", "if", "base", ".", "_share_inputs", ":", "used_inputs_var", "=", "SelectNumbers", "(", "[", "x", "for", "x", "in", "range", "(", "base", ".", "nvar", ")", "]", ")", "used_inputs_naive", "=", "used_inputs_var", "if", "base", ".", "_pr_variable", "==", "0", ":", "used_inputs_var", "=", "SelectNumbers", "(", "[", "]", ")", "used_inputs_naive", "=", "SelectNumbers", "(", "[", "x", "for", "x", "in", "range", "(", "base", ".", "nvar", ")", "]", ")", "elif", "base", ".", "_pr_variable", "==", "1", ":", "used_inputs_var", "=", "SelectNumbers", "(", "[", "x", "for", "x", "in", "range", "(", "base", ".", "nvar", ")", "]", ")", "used_inputs_naive", "=", "SelectNumbers", "(", "[", "]", ")", "else", ":", "used_inputs_var", "=", "SelectNumbers", "(", "[", "x", "for", "x", "in", "range", "(", "base", ".", "nvar", ")", "]", ")", "used_inputs_naive", "=", "SelectNumbers", "(", "[", "x", "for", "x", "in", "range", "(", "base", ".", "nvar", ")", "]", ")", "nb_input", "=", "Inputs", "(", "base", ",", "used_inputs_naive", ",", "functions", "=", "base", ".", "_input_functions", ")", "while", "(", "(", "base", ".", "_all_inputs", "and", "not", "base", ".", "stopping_criteria_tl", "(", ")", ")", "or", "(", "self", ".", "popsize", "<", "base", ".", "popsize", "and", "not", "base", ".", "stopping_criteria", "(", ")", ")", ")", ":", "if", "base", ".", "_all_inputs", "and", "used_inputs_var", ".", "empty", "(", ")", "and", "used_inputs_naive", ".", "empty", "(", ")", ":", "base", ".", "_init_popsize", "=", "self", ".", "popsize", "break", "if", "nb_input", ".", "use_all_variables", "(", ")", ":", "v", "=", "nb_input", ".", "all_variables", "(", ")", "if", "v", "is", "None", ":", "continue", "elif", "not", "used_inputs_var", ".", "empty", "(", ")", "and", "np", ".", "random", ".", "random", "(", ")", "<", "base", ".", "_pr_variable", ":", "v", "=", "self", ".", "variable_input", "(", "used_inputs_var", ")", "if", "v", "is", "None", ":", "used_inputs_var", ".", "pos", "=", "used_inputs_var", ".", "size", "continue", "elif", "not", "used_inputs_naive", ".", "empty", "(", ")", ":", "v", "=", "nb_input", ".", "input", "(", ")", "if", "not", "used_inputs_var", ".", "empty", "(", ")", "and", "used_inputs_naive", ".", "empty", "(", ")", ":", "base", ".", "_pr_variable", "=", "1", "if", "v", "is", "None", ":", "used_inputs_naive", ".", "pos", "=", "used_inputs_naive", ".", "size", "if", "not", "used_inputs_var", ".", "empty", "(", ")", ":", "base", ".", "_pr_variable", "=", "1", "continue", "else", ":", "gen", "=", "self", ".", "generation", "self", ".", "generation", "=", "0", "v", "=", "base", ".", "random_offspring", "(", ")", "self", ".", "generation", "=", "gen", "self", ".", "add", "(", "v", ")" ]
Create the initial population
[ "Create", "the", "initial", "population" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L345-L390
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.add
def add(self, v): "Add an individual to the population" self.population.append(v) self._current_popsize += 1 v.position = len(self._hist) self._hist.append(v) self.bsf = v self.estopping = v self._density += self.get_density(v)
python
def add(self, v): "Add an individual to the population" self.population.append(v) self._current_popsize += 1 v.position = len(self._hist) self._hist.append(v) self.bsf = v self.estopping = v self._density += self.get_density(v)
[ "def", "add", "(", "self", ",", "v", ")", ":", "self", ".", "population", ".", "append", "(", "v", ")", "self", ".", "_current_popsize", "+=", "1", "v", ".", "position", "=", "len", "(", "self", ".", "_hist", ")", "self", ".", "_hist", ".", "append", "(", "v", ")", "self", ".", "bsf", "=", "v", "self", ".", "estopping", "=", "v", "self", ".", "_density", "+=", "self", ".", "get_density", "(", "v", ")" ]
Add an individual to the population
[ "Add", "an", "individual", "to", "the", "population" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L392-L400
train
mgraffg/EvoDAG
EvoDAG/population.py
BasePopulation.replace
def replace(self, v): """Replace an individual selected by negative tournament selection with individual v""" if self.popsize < self._popsize: return self.add(v) k = self.tournament(negative=True) self.clean(self.population[k]) self.population[k] = v v.position = len(self._hist) self._hist.append(v) self.bsf = v self.estopping = v self._inds_replace += 1 self._density += self.get_density(v) if self._inds_replace == self._popsize: self._inds_replace = 0 self.generation += 1 gc.collect()
python
def replace(self, v): """Replace an individual selected by negative tournament selection with individual v""" if self.popsize < self._popsize: return self.add(v) k = self.tournament(negative=True) self.clean(self.population[k]) self.population[k] = v v.position = len(self._hist) self._hist.append(v) self.bsf = v self.estopping = v self._inds_replace += 1 self._density += self.get_density(v) if self._inds_replace == self._popsize: self._inds_replace = 0 self.generation += 1 gc.collect()
[ "def", "replace", "(", "self", ",", "v", ")", ":", "if", "self", ".", "popsize", "<", "self", ".", "_popsize", ":", "return", "self", ".", "add", "(", "v", ")", "k", "=", "self", ".", "tournament", "(", "negative", "=", "True", ")", "self", ".", "clean", "(", "self", ".", "population", "[", "k", "]", ")", "self", ".", "population", "[", "k", "]", "=", "v", "v", ".", "position", "=", "len", "(", "self", ".", "_hist", ")", "self", ".", "_hist", ".", "append", "(", "v", ")", "self", ".", "bsf", "=", "v", "self", ".", "estopping", "=", "v", "self", ".", "_inds_replace", "+=", "1", "self", ".", "_density", "+=", "self", ".", "get_density", "(", "v", ")", "if", "self", ".", "_inds_replace", "==", "self", ".", "_popsize", ":", "self", ".", "_inds_replace", "=", "0", "self", ".", "generation", "+=", "1", "gc", ".", "collect", "(", ")" ]
Replace an individual selected by negative tournament selection with individual v
[ "Replace", "an", "individual", "selected", "by", "negative", "tournament", "selection", "with", "individual", "v" ]
e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5
https://github.com/mgraffg/EvoDAG/blob/e11fa1fd1ca9e69cca92696c86661a3dc7b3a1d5/EvoDAG/population.py#L402-L419
train
nion-software/nionswift
nion/swift/model/HDF5Handler.py
make_directory_if_needed
def make_directory_if_needed(directory_path): """ Make the directory path, if needed. """ if os.path.exists(directory_path): if not os.path.isdir(directory_path): raise OSError("Path is not a directory:", directory_path) else: os.makedirs(directory_path)
python
def make_directory_if_needed(directory_path): """ Make the directory path, if needed. """ if os.path.exists(directory_path): if not os.path.isdir(directory_path): raise OSError("Path is not a directory:", directory_path) else: os.makedirs(directory_path)
[ "def", "make_directory_if_needed", "(", "directory_path", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "directory_path", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "directory_path", ")", ":", "raise", "OSError", "(", "\"Path is not a directory:\"", ",", "directory_path", ")", "else", ":", "os", ".", "makedirs", "(", "directory_path", ")" ]
Make the directory path, if needed.
[ "Make", "the", "directory", "path", "if", "needed", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/HDF5Handler.py#L18-L26
train
ajk8/hatchery
hatchery/main.py
hatchery
def hatchery(): """ Main entry point for the hatchery program """ args = docopt.docopt(__doc__) task_list = args['<task>'] if not task_list or 'help' in task_list or args['--help']: print(__doc__.format(version=_version.__version__, config_files=config.CONFIG_LOCATIONS)) return 0 level_str = args['--log-level'] try: level_const = getattr(logging, level_str.upper()) logging.basicConfig(level=level_const) if level_const == logging.DEBUG: workdir.options.debug = True except LookupError: logging.basicConfig() logger.error('received invalid log level: ' + level_str) return 1 for task in task_list: if task not in ORDERED_TASKS: logger.info('starting task: check') logger.error('received invalid task: ' + task) return 1 for task in CHECK_TASKS: if task in task_list: task_check(args) break if 'package' in task_list and not args['--release-version']: logger.error('--release-version is required for the package task') return 1 config_dict = _get_config_or_die( calling_task='hatchery', required_params=['auto_push_tag'] ) if config_dict['auto_push_tag'] and 'upload' in task_list: logger.info('adding task: tag (auto_push_tag==True)') task_list.append('tag') # all commands will raise a SystemExit if they fail # check will have already been run for task in ORDERED_TASKS: if task in task_list and task != 'check': logger.info('starting task: ' + task) globals()['task_' + task](args) logger.info("all's well that ends well...hatchery out") return 0
python
def hatchery(): """ Main entry point for the hatchery program """ args = docopt.docopt(__doc__) task_list = args['<task>'] if not task_list or 'help' in task_list or args['--help']: print(__doc__.format(version=_version.__version__, config_files=config.CONFIG_LOCATIONS)) return 0 level_str = args['--log-level'] try: level_const = getattr(logging, level_str.upper()) logging.basicConfig(level=level_const) if level_const == logging.DEBUG: workdir.options.debug = True except LookupError: logging.basicConfig() logger.error('received invalid log level: ' + level_str) return 1 for task in task_list: if task not in ORDERED_TASKS: logger.info('starting task: check') logger.error('received invalid task: ' + task) return 1 for task in CHECK_TASKS: if task in task_list: task_check(args) break if 'package' in task_list and not args['--release-version']: logger.error('--release-version is required for the package task') return 1 config_dict = _get_config_or_die( calling_task='hatchery', required_params=['auto_push_tag'] ) if config_dict['auto_push_tag'] and 'upload' in task_list: logger.info('adding task: tag (auto_push_tag==True)') task_list.append('tag') # all commands will raise a SystemExit if they fail # check will have already been run for task in ORDERED_TASKS: if task in task_list and task != 'check': logger.info('starting task: ' + task) globals()['task_' + task](args) logger.info("all's well that ends well...hatchery out") return 0
[ "def", "hatchery", "(", ")", ":", "args", "=", "docopt", ".", "docopt", "(", "__doc__", ")", "task_list", "=", "args", "[", "'<task>'", "]", "if", "not", "task_list", "or", "'help'", "in", "task_list", "or", "args", "[", "'--help'", "]", ":", "print", "(", "__doc__", ".", "format", "(", "version", "=", "_version", ".", "__version__", ",", "config_files", "=", "config", ".", "CONFIG_LOCATIONS", ")", ")", "return", "0", "level_str", "=", "args", "[", "'--log-level'", "]", "try", ":", "level_const", "=", "getattr", "(", "logging", ",", "level_str", ".", "upper", "(", ")", ")", "logging", ".", "basicConfig", "(", "level", "=", "level_const", ")", "if", "level_const", "==", "logging", ".", "DEBUG", ":", "workdir", ".", "options", ".", "debug", "=", "True", "except", "LookupError", ":", "logging", ".", "basicConfig", "(", ")", "logger", ".", "error", "(", "'received invalid log level: '", "+", "level_str", ")", "return", "1", "for", "task", "in", "task_list", ":", "if", "task", "not", "in", "ORDERED_TASKS", ":", "logger", ".", "info", "(", "'starting task: check'", ")", "logger", ".", "error", "(", "'received invalid task: '", "+", "task", ")", "return", "1", "for", "task", "in", "CHECK_TASKS", ":", "if", "task", "in", "task_list", ":", "task_check", "(", "args", ")", "break", "if", "'package'", "in", "task_list", "and", "not", "args", "[", "'--release-version'", "]", ":", "logger", ".", "error", "(", "'--release-version is required for the package task'", ")", "return", "1", "config_dict", "=", "_get_config_or_die", "(", "calling_task", "=", "'hatchery'", ",", "required_params", "=", "[", "'auto_push_tag'", "]", ")", "if", "config_dict", "[", "'auto_push_tag'", "]", "and", "'upload'", "in", "task_list", ":", "logger", ".", "info", "(", "'adding task: tag (auto_push_tag==True)'", ")", "task_list", ".", "append", "(", "'tag'", ")", "# all commands will raise a SystemExit if they fail", "# check will have already been run", "for", "task", "in", "ORDERED_TASKS", ":", "if", "task", "in", "task_list", "and", "task", "!=", "'check'", ":", "logger", ".", "info", "(", "'starting task: '", "+", "task", ")", "globals", "(", ")", "[", "'task_'", "+", "task", "]", "(", "args", ")", "logger", ".", "info", "(", "\"all's well that ends well...hatchery out\"", ")", "return", "0" ]
Main entry point for the hatchery program
[ "Main", "entry", "point", "for", "the", "hatchery", "program" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/main.py#L379-L430
train
ajk8/hatchery
hatchery/executor.py
call
def call(cmd_args, suppress_output=False): """ Call an arbitary command and return the exit value, stdout, and stderr as a tuple Command can be passed in as either a string or iterable >>> result = call('hatchery', suppress_output=True) >>> result.exitval 0 >>> result = call(['hatchery', 'notreal']) >>> result.exitval 1 """ if not funcy.is_list(cmd_args) and not funcy.is_tuple(cmd_args): cmd_args = shlex.split(cmd_args) logger.info('executing `{}`'.format(' '.join(cmd_args))) call_request = CallRequest(cmd_args, suppress_output=suppress_output) call_result = call_request.run() if call_result.exitval: logger.error('`{}` returned error code {}'.format(' '.join(cmd_args), call_result.exitval)) return call_result
python
def call(cmd_args, suppress_output=False): """ Call an arbitary command and return the exit value, stdout, and stderr as a tuple Command can be passed in as either a string or iterable >>> result = call('hatchery', suppress_output=True) >>> result.exitval 0 >>> result = call(['hatchery', 'notreal']) >>> result.exitval 1 """ if not funcy.is_list(cmd_args) and not funcy.is_tuple(cmd_args): cmd_args = shlex.split(cmd_args) logger.info('executing `{}`'.format(' '.join(cmd_args))) call_request = CallRequest(cmd_args, suppress_output=suppress_output) call_result = call_request.run() if call_result.exitval: logger.error('`{}` returned error code {}'.format(' '.join(cmd_args), call_result.exitval)) return call_result
[ "def", "call", "(", "cmd_args", ",", "suppress_output", "=", "False", ")", ":", "if", "not", "funcy", ".", "is_list", "(", "cmd_args", ")", "and", "not", "funcy", ".", "is_tuple", "(", "cmd_args", ")", ":", "cmd_args", "=", "shlex", ".", "split", "(", "cmd_args", ")", "logger", ".", "info", "(", "'executing `{}`'", ".", "format", "(", "' '", ".", "join", "(", "cmd_args", ")", ")", ")", "call_request", "=", "CallRequest", "(", "cmd_args", ",", "suppress_output", "=", "suppress_output", ")", "call_result", "=", "call_request", ".", "run", "(", ")", "if", "call_result", ".", "exitval", ":", "logger", ".", "error", "(", "'`{}` returned error code {}'", ".", "format", "(", "' '", ".", "join", "(", "cmd_args", ")", ",", "call_result", ".", "exitval", ")", ")", "return", "call_result" ]
Call an arbitary command and return the exit value, stdout, and stderr as a tuple Command can be passed in as either a string or iterable >>> result = call('hatchery', suppress_output=True) >>> result.exitval 0 >>> result = call(['hatchery', 'notreal']) >>> result.exitval 1
[ "Call", "an", "arbitary", "command", "and", "return", "the", "exit", "value", "stdout", "and", "stderr", "as", "a", "tuple" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/executor.py#L66-L85
train
ajk8/hatchery
hatchery/executor.py
setup
def setup(cmd_args, suppress_output=False): """ Call a setup.py command or list of commands >>> result = setup('--name', suppress_output=True) >>> result.exitval 0 >>> result = setup('notreal') >>> result.exitval 1 """ if not funcy.is_list(cmd_args) and not funcy.is_tuple(cmd_args): cmd_args = shlex.split(cmd_args) cmd_args = [sys.executable, 'setup.py'] + [x for x in cmd_args] return call(cmd_args, suppress_output=suppress_output)
python
def setup(cmd_args, suppress_output=False): """ Call a setup.py command or list of commands >>> result = setup('--name', suppress_output=True) >>> result.exitval 0 >>> result = setup('notreal') >>> result.exitval 1 """ if not funcy.is_list(cmd_args) and not funcy.is_tuple(cmd_args): cmd_args = shlex.split(cmd_args) cmd_args = [sys.executable, 'setup.py'] + [x for x in cmd_args] return call(cmd_args, suppress_output=suppress_output)
[ "def", "setup", "(", "cmd_args", ",", "suppress_output", "=", "False", ")", ":", "if", "not", "funcy", ".", "is_list", "(", "cmd_args", ")", "and", "not", "funcy", ".", "is_tuple", "(", "cmd_args", ")", ":", "cmd_args", "=", "shlex", ".", "split", "(", "cmd_args", ")", "cmd_args", "=", "[", "sys", ".", "executable", ",", "'setup.py'", "]", "+", "[", "x", "for", "x", "in", "cmd_args", "]", "return", "call", "(", "cmd_args", ",", "suppress_output", "=", "suppress_output", ")" ]
Call a setup.py command or list of commands >>> result = setup('--name', suppress_output=True) >>> result.exitval 0 >>> result = setup('notreal') >>> result.exitval 1
[ "Call", "a", "setup", ".", "py", "command", "or", "list", "of", "commands" ]
e068c9f5366d2c98225babb03d4cde36c710194f
https://github.com/ajk8/hatchery/blob/e068c9f5366d2c98225babb03d4cde36c710194f/hatchery/executor.py#L88-L101
train
djgagne/hagelslag
hagelslag/data/MRMSGrid.py
MRMSGrid.load_data
def load_data(self): """ Loads data files and stores the output in the data attribute. """ data = [] valid_dates = [] mrms_files = np.array(sorted(os.listdir(self.path + self.variable + "/"))) mrms_file_dates = np.array([m_file.split("_")[-2].split("-")[0] for m_file in mrms_files]) old_mrms_file = None file_obj = None for t in range(self.all_dates.shape[0]): file_index = np.where(mrms_file_dates == self.all_dates[t].strftime("%Y%m%d"))[0] if len(file_index) > 0: mrms_file = mrms_files[file_index][0] if mrms_file is not None: if file_obj is not None: file_obj.close() file_obj = Dataset(self.path + self.variable + "/" + mrms_file) #old_mrms_file = mrms_file if "time" in file_obj.variables.keys(): time_var = "time" else: time_var = "date" file_valid_dates = pd.DatetimeIndex(num2date(file_obj.variables[time_var][:], file_obj.variables[time_var].units)) else: file_valid_dates = pd.DatetimeIndex([]) time_index = np.where(file_valid_dates.values == self.all_dates.values[t])[0] if len(time_index) > 0: data.append(file_obj.variables[self.variable][time_index[0]]) valid_dates.append(self.all_dates[t]) if file_obj is not None: file_obj.close() self.data = np.array(data) self.data[self.data < 0] = 0 self.data[self.data > 150] = 150 self.valid_dates = pd.DatetimeIndex(valid_dates)
python
def load_data(self): """ Loads data files and stores the output in the data attribute. """ data = [] valid_dates = [] mrms_files = np.array(sorted(os.listdir(self.path + self.variable + "/"))) mrms_file_dates = np.array([m_file.split("_")[-2].split("-")[0] for m_file in mrms_files]) old_mrms_file = None file_obj = None for t in range(self.all_dates.shape[0]): file_index = np.where(mrms_file_dates == self.all_dates[t].strftime("%Y%m%d"))[0] if len(file_index) > 0: mrms_file = mrms_files[file_index][0] if mrms_file is not None: if file_obj is not None: file_obj.close() file_obj = Dataset(self.path + self.variable + "/" + mrms_file) #old_mrms_file = mrms_file if "time" in file_obj.variables.keys(): time_var = "time" else: time_var = "date" file_valid_dates = pd.DatetimeIndex(num2date(file_obj.variables[time_var][:], file_obj.variables[time_var].units)) else: file_valid_dates = pd.DatetimeIndex([]) time_index = np.where(file_valid_dates.values == self.all_dates.values[t])[0] if len(time_index) > 0: data.append(file_obj.variables[self.variable][time_index[0]]) valid_dates.append(self.all_dates[t]) if file_obj is not None: file_obj.close() self.data = np.array(data) self.data[self.data < 0] = 0 self.data[self.data > 150] = 150 self.valid_dates = pd.DatetimeIndex(valid_dates)
[ "def", "load_data", "(", "self", ")", ":", "data", "=", "[", "]", "valid_dates", "=", "[", "]", "mrms_files", "=", "np", ".", "array", "(", "sorted", "(", "os", ".", "listdir", "(", "self", ".", "path", "+", "self", ".", "variable", "+", "\"/\"", ")", ")", ")", "mrms_file_dates", "=", "np", ".", "array", "(", "[", "m_file", ".", "split", "(", "\"_\"", ")", "[", "-", "2", "]", ".", "split", "(", "\"-\"", ")", "[", "0", "]", "for", "m_file", "in", "mrms_files", "]", ")", "old_mrms_file", "=", "None", "file_obj", "=", "None", "for", "t", "in", "range", "(", "self", ".", "all_dates", ".", "shape", "[", "0", "]", ")", ":", "file_index", "=", "np", ".", "where", "(", "mrms_file_dates", "==", "self", ".", "all_dates", "[", "t", "]", ".", "strftime", "(", "\"%Y%m%d\"", ")", ")", "[", "0", "]", "if", "len", "(", "file_index", ")", ">", "0", ":", "mrms_file", "=", "mrms_files", "[", "file_index", "]", "[", "0", "]", "if", "mrms_file", "is", "not", "None", ":", "if", "file_obj", "is", "not", "None", ":", "file_obj", ".", "close", "(", ")", "file_obj", "=", "Dataset", "(", "self", ".", "path", "+", "self", ".", "variable", "+", "\"/\"", "+", "mrms_file", ")", "#old_mrms_file = mrms_file", "if", "\"time\"", "in", "file_obj", ".", "variables", ".", "keys", "(", ")", ":", "time_var", "=", "\"time\"", "else", ":", "time_var", "=", "\"date\"", "file_valid_dates", "=", "pd", ".", "DatetimeIndex", "(", "num2date", "(", "file_obj", ".", "variables", "[", "time_var", "]", "[", ":", "]", ",", "file_obj", ".", "variables", "[", "time_var", "]", ".", "units", ")", ")", "else", ":", "file_valid_dates", "=", "pd", ".", "DatetimeIndex", "(", "[", "]", ")", "time_index", "=", "np", ".", "where", "(", "file_valid_dates", ".", "values", "==", "self", ".", "all_dates", ".", "values", "[", "t", "]", ")", "[", "0", "]", "if", "len", "(", "time_index", ")", ">", "0", ":", "data", ".", "append", "(", "file_obj", ".", "variables", "[", "self", ".", "variable", "]", "[", "time_index", "[", "0", "]", "]", ")", "valid_dates", ".", "append", "(", "self", ".", "all_dates", "[", "t", "]", ")", "if", "file_obj", "is", "not", "None", ":", "file_obj", ".", "close", "(", ")", "self", ".", "data", "=", "np", ".", "array", "(", "data", ")", "self", ".", "data", "[", "self", ".", "data", "<", "0", "]", "=", "0", "self", ".", "data", "[", "self", ".", "data", ">", "150", "]", "=", "150", "self", ".", "valid_dates", "=", "pd", ".", "DatetimeIndex", "(", "valid_dates", ")" ]
Loads data files and stores the output in the data attribute.
[ "Loads", "data", "files", "and", "stores", "the", "output", "in", "the", "data", "attribute", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/data/MRMSGrid.py#L44-L82
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
rescale_data
def rescale_data(data, data_min, data_max, out_min=0.0, out_max=100.0): """ Rescale your input data so that is ranges over integer values, which will perform better in the watershed. Args: data: 2D or 3D ndarray being rescaled data_min: minimum value of input data for scaling purposes data_max: maximum value of input data for scaling purposes out_min: minimum value of scaled data out_max: maximum value of scaled data Returns: Linearly scaled ndarray """ return (out_max - out_min) / (data_max - data_min) * (data - data_min) + out_min
python
def rescale_data(data, data_min, data_max, out_min=0.0, out_max=100.0): """ Rescale your input data so that is ranges over integer values, which will perform better in the watershed. Args: data: 2D or 3D ndarray being rescaled data_min: minimum value of input data for scaling purposes data_max: maximum value of input data for scaling purposes out_min: minimum value of scaled data out_max: maximum value of scaled data Returns: Linearly scaled ndarray """ return (out_max - out_min) / (data_max - data_min) * (data - data_min) + out_min
[ "def", "rescale_data", "(", "data", ",", "data_min", ",", "data_max", ",", "out_min", "=", "0.0", ",", "out_max", "=", "100.0", ")", ":", "return", "(", "out_max", "-", "out_min", ")", "/", "(", "data_max", "-", "data_min", ")", "*", "(", "data", "-", "data_min", ")", "+", "out_min" ]
Rescale your input data so that is ranges over integer values, which will perform better in the watershed. Args: data: 2D or 3D ndarray being rescaled data_min: minimum value of input data for scaling purposes data_max: maximum value of input data for scaling purposes out_min: minimum value of scaled data out_max: maximum value of scaled data Returns: Linearly scaled ndarray
[ "Rescale", "your", "input", "data", "so", "that", "is", "ranges", "over", "integer", "values", "which", "will", "perform", "better", "in", "the", "watershed", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L297-L311
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
EnhancedWatershed.label
def label(self, input_grid): """ Labels input grid using enhanced watershed algorithm. Args: input_grid (numpy.ndarray): Grid to be labeled. Returns: Array of labeled pixels """ marked = self.find_local_maxima(input_grid) marked = np.where(marked >= 0, 1, 0) # splabel returns two things in a tuple: an array and an integer # assign the first thing (array) to markers markers = splabel(marked)[0] return markers
python
def label(self, input_grid): """ Labels input grid using enhanced watershed algorithm. Args: input_grid (numpy.ndarray): Grid to be labeled. Returns: Array of labeled pixels """ marked = self.find_local_maxima(input_grid) marked = np.where(marked >= 0, 1, 0) # splabel returns two things in a tuple: an array and an integer # assign the first thing (array) to markers markers = splabel(marked)[0] return markers
[ "def", "label", "(", "self", ",", "input_grid", ")", ":", "marked", "=", "self", ".", "find_local_maxima", "(", "input_grid", ")", "marked", "=", "np", ".", "where", "(", "marked", ">=", "0", ",", "1", ",", "0", ")", "# splabel returns two things in a tuple: an array and an integer", "# assign the first thing (array) to markers", "markers", "=", "splabel", "(", "marked", ")", "[", "0", "]", "return", "markers" ]
Labels input grid using enhanced watershed algorithm. Args: input_grid (numpy.ndarray): Grid to be labeled. Returns: Array of labeled pixels
[ "Labels", "input", "grid", "using", "enhanced", "watershed", "algorithm", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L42-L57
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
EnhancedWatershed.find_local_maxima
def find_local_maxima(self, input_grid): """ Finds the local maxima in the inputGrid and perform region growing to identify objects. Args: input_grid: Raw input data. Returns: array with labeled objects. """ pixels, q_data = self.quantize(input_grid) centers = OrderedDict() for p in pixels.keys(): centers[p] = [] marked = np.ones(q_data.shape, dtype=int) * self.UNMARKED MIN_INFL = int(np.round(1 + 0.5 * np.sqrt(self.max_size))) MAX_INFL = 2 * MIN_INFL marked_so_far = [] # Find the maxima. These are high-values with enough clearance # around them. # Work from high to low bins. The pixels in the highest bin mark their # neighborhoods first. If you did it from low to high the lowest maxima # would mark their neighborhoods first and interfere with the identification of higher maxima. for b in sorted(pixels.keys(),reverse=True): # Square starts large with high intensity bins and gets smaller with low intensity bins. infl_dist = MIN_INFL + int(np.round(float(b) / self.max_bin * (MAX_INFL - MIN_INFL))) for p in pixels[b]: if marked[p] == self.UNMARKED: ok = False del marked_so_far[:] # Temporarily mark unmarked points in square around point (keep track of them in list marked_so_far). # If none of the points in square were marked already from a higher intensity center, # this counts as a new center and ok=True and points will remain marked. # Otherwise ok=False and marked points that were previously unmarked will be unmarked. for (i, j), v in np.ndenumerate(marked[p[0] - infl_dist:p[0] + infl_dist + 1, p[1] - infl_dist:p[1]+ infl_dist + 1]): if v == self.UNMARKED: ok = True marked[i - infl_dist + p[0],j - infl_dist + p[1]] = b marked_so_far.append((i - infl_dist + p[0],j - infl_dist + p[1])) else: # neighborhood already taken ok = False break # ok if point and surrounding square were not marked already. if ok: # highest point in its neighborhood centers[b].append(p) else: for m in marked_so_far: marked[m] = self.UNMARKED # Erase marks and start over. You have a list of centers now. marked[:, :] = self.UNMARKED deferred_from_last = [] deferred_to_next = [] # delta (int): maximum number of increments the cluster is allowed to range over. Larger d results in clusters over larger scales. for delta in range(0, self.delta + 1): # Work from high to low bins. for b in sorted(centers.keys(), reverse=True): bin_lower = b - delta deferred_from_last[:] = deferred_to_next[:] del deferred_to_next[:] foothills = [] n_centers = len(centers[b]) tot_centers = n_centers + len(deferred_from_last) for i in range(tot_centers): # done this way to minimize memory overhead of maintaining two lists if i < n_centers: center = centers[b][i] else: center = deferred_from_last[i - n_centers] if bin_lower < 0: bin_lower = 0 if marked[center] == self.UNMARKED: captured = self.set_maximum(q_data, marked, center, bin_lower, foothills) if not captured: # decrement to lower value to see if it'll get big enough deferred_to_next.append(center) else: pass # this is the last one for this bin self.remove_foothills(q_data, marked, b, bin_lower, centers, foothills) del deferred_from_last[:] del deferred_to_next[:] return marked
python
def find_local_maxima(self, input_grid): """ Finds the local maxima in the inputGrid and perform region growing to identify objects. Args: input_grid: Raw input data. Returns: array with labeled objects. """ pixels, q_data = self.quantize(input_grid) centers = OrderedDict() for p in pixels.keys(): centers[p] = [] marked = np.ones(q_data.shape, dtype=int) * self.UNMARKED MIN_INFL = int(np.round(1 + 0.5 * np.sqrt(self.max_size))) MAX_INFL = 2 * MIN_INFL marked_so_far = [] # Find the maxima. These are high-values with enough clearance # around them. # Work from high to low bins. The pixels in the highest bin mark their # neighborhoods first. If you did it from low to high the lowest maxima # would mark their neighborhoods first and interfere with the identification of higher maxima. for b in sorted(pixels.keys(),reverse=True): # Square starts large with high intensity bins and gets smaller with low intensity bins. infl_dist = MIN_INFL + int(np.round(float(b) / self.max_bin * (MAX_INFL - MIN_INFL))) for p in pixels[b]: if marked[p] == self.UNMARKED: ok = False del marked_so_far[:] # Temporarily mark unmarked points in square around point (keep track of them in list marked_so_far). # If none of the points in square were marked already from a higher intensity center, # this counts as a new center and ok=True and points will remain marked. # Otherwise ok=False and marked points that were previously unmarked will be unmarked. for (i, j), v in np.ndenumerate(marked[p[0] - infl_dist:p[0] + infl_dist + 1, p[1] - infl_dist:p[1]+ infl_dist + 1]): if v == self.UNMARKED: ok = True marked[i - infl_dist + p[0],j - infl_dist + p[1]] = b marked_so_far.append((i - infl_dist + p[0],j - infl_dist + p[1])) else: # neighborhood already taken ok = False break # ok if point and surrounding square were not marked already. if ok: # highest point in its neighborhood centers[b].append(p) else: for m in marked_so_far: marked[m] = self.UNMARKED # Erase marks and start over. You have a list of centers now. marked[:, :] = self.UNMARKED deferred_from_last = [] deferred_to_next = [] # delta (int): maximum number of increments the cluster is allowed to range over. Larger d results in clusters over larger scales. for delta in range(0, self.delta + 1): # Work from high to low bins. for b in sorted(centers.keys(), reverse=True): bin_lower = b - delta deferred_from_last[:] = deferred_to_next[:] del deferred_to_next[:] foothills = [] n_centers = len(centers[b]) tot_centers = n_centers + len(deferred_from_last) for i in range(tot_centers): # done this way to minimize memory overhead of maintaining two lists if i < n_centers: center = centers[b][i] else: center = deferred_from_last[i - n_centers] if bin_lower < 0: bin_lower = 0 if marked[center] == self.UNMARKED: captured = self.set_maximum(q_data, marked, center, bin_lower, foothills) if not captured: # decrement to lower value to see if it'll get big enough deferred_to_next.append(center) else: pass # this is the last one for this bin self.remove_foothills(q_data, marked, b, bin_lower, centers, foothills) del deferred_from_last[:] del deferred_to_next[:] return marked
[ "def", "find_local_maxima", "(", "self", ",", "input_grid", ")", ":", "pixels", ",", "q_data", "=", "self", ".", "quantize", "(", "input_grid", ")", "centers", "=", "OrderedDict", "(", ")", "for", "p", "in", "pixels", ".", "keys", "(", ")", ":", "centers", "[", "p", "]", "=", "[", "]", "marked", "=", "np", ".", "ones", "(", "q_data", ".", "shape", ",", "dtype", "=", "int", ")", "*", "self", ".", "UNMARKED", "MIN_INFL", "=", "int", "(", "np", ".", "round", "(", "1", "+", "0.5", "*", "np", ".", "sqrt", "(", "self", ".", "max_size", ")", ")", ")", "MAX_INFL", "=", "2", "*", "MIN_INFL", "marked_so_far", "=", "[", "]", "# Find the maxima. These are high-values with enough clearance", "# around them.", "# Work from high to low bins. The pixels in the highest bin mark their", "# neighborhoods first. If you did it from low to high the lowest maxima", "# would mark their neighborhoods first and interfere with the identification of higher maxima.", "for", "b", "in", "sorted", "(", "pixels", ".", "keys", "(", ")", ",", "reverse", "=", "True", ")", ":", "# Square starts large with high intensity bins and gets smaller with low intensity bins.", "infl_dist", "=", "MIN_INFL", "+", "int", "(", "np", ".", "round", "(", "float", "(", "b", ")", "/", "self", ".", "max_bin", "*", "(", "MAX_INFL", "-", "MIN_INFL", ")", ")", ")", "for", "p", "in", "pixels", "[", "b", "]", ":", "if", "marked", "[", "p", "]", "==", "self", ".", "UNMARKED", ":", "ok", "=", "False", "del", "marked_so_far", "[", ":", "]", "# Temporarily mark unmarked points in square around point (keep track of them in list marked_so_far).", "# If none of the points in square were marked already from a higher intensity center, ", "# this counts as a new center and ok=True and points will remain marked.", "# Otherwise ok=False and marked points that were previously unmarked will be unmarked.", "for", "(", "i", ",", "j", ")", ",", "v", "in", "np", ".", "ndenumerate", "(", "marked", "[", "p", "[", "0", "]", "-", "infl_dist", ":", "p", "[", "0", "]", "+", "infl_dist", "+", "1", ",", "p", "[", "1", "]", "-", "infl_dist", ":", "p", "[", "1", "]", "+", "infl_dist", "+", "1", "]", ")", ":", "if", "v", "==", "self", ".", "UNMARKED", ":", "ok", "=", "True", "marked", "[", "i", "-", "infl_dist", "+", "p", "[", "0", "]", ",", "j", "-", "infl_dist", "+", "p", "[", "1", "]", "]", "=", "b", "marked_so_far", ".", "append", "(", "(", "i", "-", "infl_dist", "+", "p", "[", "0", "]", ",", "j", "-", "infl_dist", "+", "p", "[", "1", "]", ")", ")", "else", ":", "# neighborhood already taken", "ok", "=", "False", "break", "# ok if point and surrounding square were not marked already.", "if", "ok", ":", "# highest point in its neighborhood", "centers", "[", "b", "]", ".", "append", "(", "p", ")", "else", ":", "for", "m", "in", "marked_so_far", ":", "marked", "[", "m", "]", "=", "self", ".", "UNMARKED", "# Erase marks and start over. You have a list of centers now.", "marked", "[", ":", ",", ":", "]", "=", "self", ".", "UNMARKED", "deferred_from_last", "=", "[", "]", "deferred_to_next", "=", "[", "]", "# delta (int): maximum number of increments the cluster is allowed to range over. Larger d results in clusters over larger scales.", "for", "delta", "in", "range", "(", "0", ",", "self", ".", "delta", "+", "1", ")", ":", "# Work from high to low bins.", "for", "b", "in", "sorted", "(", "centers", ".", "keys", "(", ")", ",", "reverse", "=", "True", ")", ":", "bin_lower", "=", "b", "-", "delta", "deferred_from_last", "[", ":", "]", "=", "deferred_to_next", "[", ":", "]", "del", "deferred_to_next", "[", ":", "]", "foothills", "=", "[", "]", "n_centers", "=", "len", "(", "centers", "[", "b", "]", ")", "tot_centers", "=", "n_centers", "+", "len", "(", "deferred_from_last", ")", "for", "i", "in", "range", "(", "tot_centers", ")", ":", "# done this way to minimize memory overhead of maintaining two lists", "if", "i", "<", "n_centers", ":", "center", "=", "centers", "[", "b", "]", "[", "i", "]", "else", ":", "center", "=", "deferred_from_last", "[", "i", "-", "n_centers", "]", "if", "bin_lower", "<", "0", ":", "bin_lower", "=", "0", "if", "marked", "[", "center", "]", "==", "self", ".", "UNMARKED", ":", "captured", "=", "self", ".", "set_maximum", "(", "q_data", ",", "marked", ",", "center", ",", "bin_lower", ",", "foothills", ")", "if", "not", "captured", ":", "# decrement to lower value to see if it'll get big enough", "deferred_to_next", ".", "append", "(", "center", ")", "else", ":", "pass", "# this is the last one for this bin", "self", ".", "remove_foothills", "(", "q_data", ",", "marked", ",", "b", ",", "bin_lower", ",", "centers", ",", "foothills", ")", "del", "deferred_from_last", "[", ":", "]", "del", "deferred_to_next", "[", ":", "]", "return", "marked" ]
Finds the local maxima in the inputGrid and perform region growing to identify objects. Args: input_grid: Raw input data. Returns: array with labeled objects.
[ "Finds", "the", "local", "maxima", "in", "the", "inputGrid", "and", "perform", "region", "growing", "to", "identify", "objects", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L81-L166
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
EnhancedWatershed.set_maximum
def set_maximum(self, q_data, marked, center, bin_lower, foothills): """ Grow a region at a certain bin level and check if the region has reached the maximum size. Args: q_data: Quantized data array marked: Array marking points that are objects center: Coordinates of the center pixel of the region being grown bin_lower: Intensity level of lower bin being evaluated foothills: List of points that are associated with a center but fall outside the the size or intensity criteria Returns: True if the object is finished growing and False if the object should be grown again at the next threshold level. """ as_bin = [] # pixels to be included in peak as_glob = [] # pixels to be globbed up as part of foothills marked_so_far = [] # pixels that have already been marked will_be_considered_again = False as_bin.append(center) center_data = q_data[center] while len(as_bin) > 0: p = as_bin.pop(-1) # remove and return last pixel in as_bin if marked[p] != self.UNMARKED: # already processed continue marked[p] = q_data[center] marked_so_far.append(p) # check neighbors for index,val in np.ndenumerate(marked[p[0] - 1:p[0] + 2, p[1] - 1:p[1] + 2]): # is neighbor part of peak or part of mountain? if val == self.UNMARKED: pixel = (index[0] - 1 + p[0],index[1] - 1 + p[1]) p_data = q_data[pixel] if (not will_be_considered_again) and (p_data >= 0) and (p_data < center_data): will_be_considered_again = True if p_data >= bin_lower and (np.abs(center_data - p_data) <= self.delta): as_bin.append(pixel) # Do not check that this is the closest: this way, a narrow channel of globbed pixels form elif p_data >= 0: as_glob.append(pixel) if bin_lower == 0: will_be_considered_again = False big_enough = len(marked_so_far) >= self.max_size if big_enough: # remove lower values within region of influence foothills.append((center, as_glob)) elif will_be_considered_again: # remove the check if you want to ignore regions smaller than max_size for m in marked_so_far: marked[m] = self.UNMARKED del as_bin[:] del as_glob[:] del marked_so_far[:] return big_enough or (not will_be_considered_again)
python
def set_maximum(self, q_data, marked, center, bin_lower, foothills): """ Grow a region at a certain bin level and check if the region has reached the maximum size. Args: q_data: Quantized data array marked: Array marking points that are objects center: Coordinates of the center pixel of the region being grown bin_lower: Intensity level of lower bin being evaluated foothills: List of points that are associated with a center but fall outside the the size or intensity criteria Returns: True if the object is finished growing and False if the object should be grown again at the next threshold level. """ as_bin = [] # pixels to be included in peak as_glob = [] # pixels to be globbed up as part of foothills marked_so_far = [] # pixels that have already been marked will_be_considered_again = False as_bin.append(center) center_data = q_data[center] while len(as_bin) > 0: p = as_bin.pop(-1) # remove and return last pixel in as_bin if marked[p] != self.UNMARKED: # already processed continue marked[p] = q_data[center] marked_so_far.append(p) # check neighbors for index,val in np.ndenumerate(marked[p[0] - 1:p[0] + 2, p[1] - 1:p[1] + 2]): # is neighbor part of peak or part of mountain? if val == self.UNMARKED: pixel = (index[0] - 1 + p[0],index[1] - 1 + p[1]) p_data = q_data[pixel] if (not will_be_considered_again) and (p_data >= 0) and (p_data < center_data): will_be_considered_again = True if p_data >= bin_lower and (np.abs(center_data - p_data) <= self.delta): as_bin.append(pixel) # Do not check that this is the closest: this way, a narrow channel of globbed pixels form elif p_data >= 0: as_glob.append(pixel) if bin_lower == 0: will_be_considered_again = False big_enough = len(marked_so_far) >= self.max_size if big_enough: # remove lower values within region of influence foothills.append((center, as_glob)) elif will_be_considered_again: # remove the check if you want to ignore regions smaller than max_size for m in marked_so_far: marked[m] = self.UNMARKED del as_bin[:] del as_glob[:] del marked_so_far[:] return big_enough or (not will_be_considered_again)
[ "def", "set_maximum", "(", "self", ",", "q_data", ",", "marked", ",", "center", ",", "bin_lower", ",", "foothills", ")", ":", "as_bin", "=", "[", "]", "# pixels to be included in peak", "as_glob", "=", "[", "]", "# pixels to be globbed up as part of foothills", "marked_so_far", "=", "[", "]", "# pixels that have already been marked", "will_be_considered_again", "=", "False", "as_bin", ".", "append", "(", "center", ")", "center_data", "=", "q_data", "[", "center", "]", "while", "len", "(", "as_bin", ")", ">", "0", ":", "p", "=", "as_bin", ".", "pop", "(", "-", "1", ")", "# remove and return last pixel in as_bin", "if", "marked", "[", "p", "]", "!=", "self", ".", "UNMARKED", ":", "# already processed", "continue", "marked", "[", "p", "]", "=", "q_data", "[", "center", "]", "marked_so_far", ".", "append", "(", "p", ")", "# check neighbors", "for", "index", ",", "val", "in", "np", ".", "ndenumerate", "(", "marked", "[", "p", "[", "0", "]", "-", "1", ":", "p", "[", "0", "]", "+", "2", ",", "p", "[", "1", "]", "-", "1", ":", "p", "[", "1", "]", "+", "2", "]", ")", ":", "# is neighbor part of peak or part of mountain?", "if", "val", "==", "self", ".", "UNMARKED", ":", "pixel", "=", "(", "index", "[", "0", "]", "-", "1", "+", "p", "[", "0", "]", ",", "index", "[", "1", "]", "-", "1", "+", "p", "[", "1", "]", ")", "p_data", "=", "q_data", "[", "pixel", "]", "if", "(", "not", "will_be_considered_again", ")", "and", "(", "p_data", ">=", "0", ")", "and", "(", "p_data", "<", "center_data", ")", ":", "will_be_considered_again", "=", "True", "if", "p_data", ">=", "bin_lower", "and", "(", "np", ".", "abs", "(", "center_data", "-", "p_data", ")", "<=", "self", ".", "delta", ")", ":", "as_bin", ".", "append", "(", "pixel", ")", "# Do not check that this is the closest: this way, a narrow channel of globbed pixels form", "elif", "p_data", ">=", "0", ":", "as_glob", ".", "append", "(", "pixel", ")", "if", "bin_lower", "==", "0", ":", "will_be_considered_again", "=", "False", "big_enough", "=", "len", "(", "marked_so_far", ")", ">=", "self", ".", "max_size", "if", "big_enough", ":", "# remove lower values within region of influence", "foothills", ".", "append", "(", "(", "center", ",", "as_glob", ")", ")", "elif", "will_be_considered_again", ":", "# remove the check if you want to ignore regions smaller than max_size", "for", "m", "in", "marked_so_far", ":", "marked", "[", "m", "]", "=", "self", ".", "UNMARKED", "del", "as_bin", "[", ":", "]", "del", "as_glob", "[", ":", "]", "del", "marked_so_far", "[", ":", "]", "return", "big_enough", "or", "(", "not", "will_be_considered_again", ")" ]
Grow a region at a certain bin level and check if the region has reached the maximum size. Args: q_data: Quantized data array marked: Array marking points that are objects center: Coordinates of the center pixel of the region being grown bin_lower: Intensity level of lower bin being evaluated foothills: List of points that are associated with a center but fall outside the the size or intensity criteria Returns: True if the object is finished growing and False if the object should be grown again at the next threshold level.
[ "Grow", "a", "region", "at", "a", "certain", "bin", "level", "and", "check", "if", "the", "region", "has", "reached", "the", "maximum", "size", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L168-L221
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
EnhancedWatershed.remove_foothills
def remove_foothills(self, q_data, marked, bin_num, bin_lower, centers, foothills): """ Mark points determined to be foothills as globbed, so that they are not included in future searches. Also searches neighboring points to foothill points to determine if they should also be considered foothills. Args: q_data: Quantized data marked: Marked bin_num: Current bin being searched bin_lower: Next bin being searched centers: dictionary of local maxima considered to be object centers foothills: List of foothill points being removed. """ hills = [] for foot in foothills: center = foot[0] hills[:] = foot[1][:] # remove all foothills while len(hills) > 0: # mark this point pt = hills.pop(-1) marked[pt] = self.GLOBBED for s_index, val in np.ndenumerate(marked[pt[0]-1:pt[0]+2,pt[1]-1:pt[1]+2]): index = (s_index[0] - 1 + pt[0], s_index[1] - 1 + pt[1]) # is neighbor part of peak or part of mountain? if val == self.UNMARKED: # will let in even minor peaks if (q_data[index] >= 0) and \ (q_data[index] < bin_lower) and \ ((q_data[index] <= q_data[pt]) or self.is_closest(index, center, centers, bin_num)): hills.append(index) del foothills[:]
python
def remove_foothills(self, q_data, marked, bin_num, bin_lower, centers, foothills): """ Mark points determined to be foothills as globbed, so that they are not included in future searches. Also searches neighboring points to foothill points to determine if they should also be considered foothills. Args: q_data: Quantized data marked: Marked bin_num: Current bin being searched bin_lower: Next bin being searched centers: dictionary of local maxima considered to be object centers foothills: List of foothill points being removed. """ hills = [] for foot in foothills: center = foot[0] hills[:] = foot[1][:] # remove all foothills while len(hills) > 0: # mark this point pt = hills.pop(-1) marked[pt] = self.GLOBBED for s_index, val in np.ndenumerate(marked[pt[0]-1:pt[0]+2,pt[1]-1:pt[1]+2]): index = (s_index[0] - 1 + pt[0], s_index[1] - 1 + pt[1]) # is neighbor part of peak or part of mountain? if val == self.UNMARKED: # will let in even minor peaks if (q_data[index] >= 0) and \ (q_data[index] < bin_lower) and \ ((q_data[index] <= q_data[pt]) or self.is_closest(index, center, centers, bin_num)): hills.append(index) del foothills[:]
[ "def", "remove_foothills", "(", "self", ",", "q_data", ",", "marked", ",", "bin_num", ",", "bin_lower", ",", "centers", ",", "foothills", ")", ":", "hills", "=", "[", "]", "for", "foot", "in", "foothills", ":", "center", "=", "foot", "[", "0", "]", "hills", "[", ":", "]", "=", "foot", "[", "1", "]", "[", ":", "]", "# remove all foothills", "while", "len", "(", "hills", ")", ">", "0", ":", "# mark this point", "pt", "=", "hills", ".", "pop", "(", "-", "1", ")", "marked", "[", "pt", "]", "=", "self", ".", "GLOBBED", "for", "s_index", ",", "val", "in", "np", ".", "ndenumerate", "(", "marked", "[", "pt", "[", "0", "]", "-", "1", ":", "pt", "[", "0", "]", "+", "2", ",", "pt", "[", "1", "]", "-", "1", ":", "pt", "[", "1", "]", "+", "2", "]", ")", ":", "index", "=", "(", "s_index", "[", "0", "]", "-", "1", "+", "pt", "[", "0", "]", ",", "s_index", "[", "1", "]", "-", "1", "+", "pt", "[", "1", "]", ")", "# is neighbor part of peak or part of mountain?", "if", "val", "==", "self", ".", "UNMARKED", ":", "# will let in even minor peaks", "if", "(", "q_data", "[", "index", "]", ">=", "0", ")", "and", "(", "q_data", "[", "index", "]", "<", "bin_lower", ")", "and", "(", "(", "q_data", "[", "index", "]", "<=", "q_data", "[", "pt", "]", ")", "or", "self", ".", "is_closest", "(", "index", ",", "center", ",", "centers", ",", "bin_num", ")", ")", ":", "hills", ".", "append", "(", "index", ")", "del", "foothills", "[", ":", "]" ]
Mark points determined to be foothills as globbed, so that they are not included in future searches. Also searches neighboring points to foothill points to determine if they should also be considered foothills. Args: q_data: Quantized data marked: Marked bin_num: Current bin being searched bin_lower: Next bin being searched centers: dictionary of local maxima considered to be object centers foothills: List of foothill points being removed.
[ "Mark", "points", "determined", "to", "be", "foothills", "as", "globbed", "so", "that", "they", "are", "not", "included", "in", "future", "searches", ".", "Also", "searches", "neighboring", "points", "to", "foothill", "points", "to", "determine", "if", "they", "should", "also", "be", "considered", "foothills", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L223-L255
train
djgagne/hagelslag
hagelslag/processing/EnhancedWatershedSegmenter.py
EnhancedWatershed.quantize
def quantize(self, input_grid): """ Quantize a grid into discrete steps based on input parameters. Args: input_grid: 2-d array of values Returns: Dictionary of value pointing to pixel locations, and quantized 2-d array of data """ pixels = {} for i in range(self.max_bin+1): pixels[i] = [] data = (np.array(input_grid, dtype=int) - self.min_thresh) / self.data_increment data[data < 0] = -1 data[data > self.max_bin] = self.max_bin good_points = np.where(data >= 0) for g in np.arange(good_points[0].shape[0]): pixels[data[(good_points[0][g], good_points[1][g])]].append((good_points[0][g], good_points[1][g])) return pixels, data
python
def quantize(self, input_grid): """ Quantize a grid into discrete steps based on input parameters. Args: input_grid: 2-d array of values Returns: Dictionary of value pointing to pixel locations, and quantized 2-d array of data """ pixels = {} for i in range(self.max_bin+1): pixels[i] = [] data = (np.array(input_grid, dtype=int) - self.min_thresh) / self.data_increment data[data < 0] = -1 data[data > self.max_bin] = self.max_bin good_points = np.where(data >= 0) for g in np.arange(good_points[0].shape[0]): pixels[data[(good_points[0][g], good_points[1][g])]].append((good_points[0][g], good_points[1][g])) return pixels, data
[ "def", "quantize", "(", "self", ",", "input_grid", ")", ":", "pixels", "=", "{", "}", "for", "i", "in", "range", "(", "self", ".", "max_bin", "+", "1", ")", ":", "pixels", "[", "i", "]", "=", "[", "]", "data", "=", "(", "np", ".", "array", "(", "input_grid", ",", "dtype", "=", "int", ")", "-", "self", ".", "min_thresh", ")", "/", "self", ".", "data_increment", "data", "[", "data", "<", "0", "]", "=", "-", "1", "data", "[", "data", ">", "self", ".", "max_bin", "]", "=", "self", ".", "max_bin", "good_points", "=", "np", ".", "where", "(", "data", ">=", "0", ")", "for", "g", "in", "np", ".", "arange", "(", "good_points", "[", "0", "]", ".", "shape", "[", "0", "]", ")", ":", "pixels", "[", "data", "[", "(", "good_points", "[", "0", "]", "[", "g", "]", ",", "good_points", "[", "1", "]", "[", "g", "]", ")", "]", "]", ".", "append", "(", "(", "good_points", "[", "0", "]", "[", "g", "]", ",", "good_points", "[", "1", "]", "[", "g", "]", ")", ")", "return", "pixels", ",", "data" ]
Quantize a grid into discrete steps based on input parameters. Args: input_grid: 2-d array of values Returns: Dictionary of value pointing to pixel locations, and quantized 2-d array of data
[ "Quantize", "a", "grid", "into", "discrete", "steps", "based", "on", "input", "parameters", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/processing/EnhancedWatershedSegmenter.py#L270-L290
train
softvar/simplegist
simplegist/mygist.py
Mygist.listall
def listall(self): ''' will display all the filenames. Result can be stored in an array for easy fetching of gistNames for future purposes. eg. a = Gist().mygists().listall() print a[0] #to fetch first gistName ''' file_name = [] r = requests.get( '%s/users/%s/gists' % (BASE_URL, self.user), headers=self.gist.header ) r_text = json.loads(r.text) limit = len(r.json()) if (r.status_code == 200 ): for g,no in zip(r_text, range(0,limit)): for key,value in r.json()[no]['files'].iteritems(): file_name.append(value['filename']) return file_name raise Exception('Username not found')
python
def listall(self): ''' will display all the filenames. Result can be stored in an array for easy fetching of gistNames for future purposes. eg. a = Gist().mygists().listall() print a[0] #to fetch first gistName ''' file_name = [] r = requests.get( '%s/users/%s/gists' % (BASE_URL, self.user), headers=self.gist.header ) r_text = json.loads(r.text) limit = len(r.json()) if (r.status_code == 200 ): for g,no in zip(r_text, range(0,limit)): for key,value in r.json()[no]['files'].iteritems(): file_name.append(value['filename']) return file_name raise Exception('Username not found')
[ "def", "listall", "(", "self", ")", ":", "file_name", "=", "[", "]", "r", "=", "requests", ".", "get", "(", "'%s/users/%s/gists'", "%", "(", "BASE_URL", ",", "self", ".", "user", ")", ",", "headers", "=", "self", ".", "gist", ".", "header", ")", "r_text", "=", "json", ".", "loads", "(", "r", ".", "text", ")", "limit", "=", "len", "(", "r", ".", "json", "(", ")", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "for", "g", ",", "no", "in", "zip", "(", "r_text", ",", "range", "(", "0", ",", "limit", ")", ")", ":", "for", "key", ",", "value", "in", "r", ".", "json", "(", ")", "[", "no", "]", "[", "'files'", "]", ".", "iteritems", "(", ")", ":", "file_name", ".", "append", "(", "value", "[", "'filename'", "]", ")", "return", "file_name", "raise", "Exception", "(", "'Username not found'", ")" ]
will display all the filenames. Result can be stored in an array for easy fetching of gistNames for future purposes. eg. a = Gist().mygists().listall() print a[0] #to fetch first gistName
[ "will", "display", "all", "the", "filenames", ".", "Result", "can", "be", "stored", "in", "an", "array", "for", "easy", "fetching", "of", "gistNames", "for", "future", "purposes", ".", "eg", ".", "a", "=", "Gist", "()", ".", "mygists", "()", ".", "listall", "()", "print", "a", "[", "0", "]", "#to", "fetch", "first", "gistName" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L13-L34
train
softvar/simplegist
simplegist/mygist.py
Mygist.content
def content(self, **args): ''' Doesn't require manual fetching of gistID of a gist passing gistName will return the content of gist. In case, names are ambigious, provide GistID or it will return the contents of recent ambigious gistname ''' self.gist_name = '' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid') if self.gist_id: r = requests.get( '%s'%BASE_URL+'/gists/%s' %self.gist_id, headers=self.gist.header ) if (r.status_code == 200): r_text = json.loads(r.text) if self.gist_name!='': content = r.json()['files'][self.gist_name]['content'] else: for key,value in r.json()['files'].iteritems(): content = r.json()['files'][value['filename']]['content'] return content raise Exception('No such gist found')
python
def content(self, **args): ''' Doesn't require manual fetching of gistID of a gist passing gistName will return the content of gist. In case, names are ambigious, provide GistID or it will return the contents of recent ambigious gistname ''' self.gist_name = '' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Either provide authenticated user\'s Unambigious Gistname or any unique Gistid') if self.gist_id: r = requests.get( '%s'%BASE_URL+'/gists/%s' %self.gist_id, headers=self.gist.header ) if (r.status_code == 200): r_text = json.loads(r.text) if self.gist_name!='': content = r.json()['files'][self.gist_name]['content'] else: for key,value in r.json()['files'].iteritems(): content = r.json()['files'][value['filename']]['content'] return content raise Exception('No such gist found')
[ "def", "content", "(", "self", ",", "*", "*", "args", ")", ":", "self", ".", "gist_name", "=", "''", "if", "'name'", "in", "args", ":", "self", ".", "gist_name", "=", "args", "[", "'name'", "]", "self", ".", "gist_id", "=", "self", ".", "getMyID", "(", "self", ".", "gist_name", ")", "elif", "'id'", "in", "args", ":", "self", ".", "gist_id", "=", "args", "[", "'id'", "]", "else", ":", "raise", "Exception", "(", "'Either provide authenticated user\\'s Unambigious Gistname or any unique Gistid'", ")", "if", "self", ".", "gist_id", ":", "r", "=", "requests", ".", "get", "(", "'%s'", "%", "BASE_URL", "+", "'/gists/%s'", "%", "self", ".", "gist_id", ",", "headers", "=", "self", ".", "gist", ".", "header", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "r_text", "=", "json", ".", "loads", "(", "r", ".", "text", ")", "if", "self", ".", "gist_name", "!=", "''", ":", "content", "=", "r", ".", "json", "(", ")", "[", "'files'", "]", "[", "self", ".", "gist_name", "]", "[", "'content'", "]", "else", ":", "for", "key", ",", "value", "in", "r", ".", "json", "(", ")", "[", "'files'", "]", ".", "iteritems", "(", ")", ":", "content", "=", "r", ".", "json", "(", ")", "[", "'files'", "]", "[", "value", "[", "'filename'", "]", "]", "[", "'content'", "]", "return", "content", "raise", "Exception", "(", "'No such gist found'", ")" ]
Doesn't require manual fetching of gistID of a gist passing gistName will return the content of gist. In case, names are ambigious, provide GistID or it will return the contents of recent ambigious gistname
[ "Doesn", "t", "require", "manual", "fetching", "of", "gistID", "of", "a", "gist", "passing", "gistName", "will", "return", "the", "content", "of", "gist", ".", "In", "case", "names", "are", "ambigious", "provide", "GistID", "or", "it", "will", "return", "the", "contents", "of", "recent", "ambigious", "gistname" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L78-L109
train
softvar/simplegist
simplegist/mygist.py
Mygist.edit
def edit(self, **args): ''' Doesn't require manual fetching of gistID of a gist passing gistName will return edit the gist ''' self.gist_name = '' if 'description' in args: self.description = args['description'] else: self.description = '' if 'name' in args and 'id' in args: self.gist_name = args['name'] self.gist_id = args['id'] elif 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Gist Name/ID must be provided') if 'content' in args: self.content = args['content'] else: raise Exception('Gist content can\'t be empty') if (self.gist_name == ''): self.gist_name = self.getgist(id=self.gist_id) data = {"description": self.description, "files": { self.gist_name: { "content": self.content } } } else: data = {"description": self.description, "files": { self.gist_name: { "content": self.content } } } if self.gist_id: r = requests.patch( '%s/gists/%s'%(BASE_URL,self.gist_id), headers=self.gist.header, data=json.dumps(data), ) if (r.status_code == 200): r_text = json.loads(r.text) response = { 'updated_content': self.content, 'created_at': r.json()['created_at'], 'comments':r.json()['comments'] } return response raise Exception('No such gist found')
python
def edit(self, **args): ''' Doesn't require manual fetching of gistID of a gist passing gistName will return edit the gist ''' self.gist_name = '' if 'description' in args: self.description = args['description'] else: self.description = '' if 'name' in args and 'id' in args: self.gist_name = args['name'] self.gist_id = args['id'] elif 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Gist Name/ID must be provided') if 'content' in args: self.content = args['content'] else: raise Exception('Gist content can\'t be empty') if (self.gist_name == ''): self.gist_name = self.getgist(id=self.gist_id) data = {"description": self.description, "files": { self.gist_name: { "content": self.content } } } else: data = {"description": self.description, "files": { self.gist_name: { "content": self.content } } } if self.gist_id: r = requests.patch( '%s/gists/%s'%(BASE_URL,self.gist_id), headers=self.gist.header, data=json.dumps(data), ) if (r.status_code == 200): r_text = json.loads(r.text) response = { 'updated_content': self.content, 'created_at': r.json()['created_at'], 'comments':r.json()['comments'] } return response raise Exception('No such gist found')
[ "def", "edit", "(", "self", ",", "*", "*", "args", ")", ":", "self", ".", "gist_name", "=", "''", "if", "'description'", "in", "args", ":", "self", ".", "description", "=", "args", "[", "'description'", "]", "else", ":", "self", ".", "description", "=", "''", "if", "'name'", "in", "args", "and", "'id'", "in", "args", ":", "self", ".", "gist_name", "=", "args", "[", "'name'", "]", "self", ".", "gist_id", "=", "args", "[", "'id'", "]", "elif", "'name'", "in", "args", ":", "self", ".", "gist_name", "=", "args", "[", "'name'", "]", "self", ".", "gist_id", "=", "self", ".", "getMyID", "(", "self", ".", "gist_name", ")", "elif", "'id'", "in", "args", ":", "self", ".", "gist_id", "=", "args", "[", "'id'", "]", "else", ":", "raise", "Exception", "(", "'Gist Name/ID must be provided'", ")", "if", "'content'", "in", "args", ":", "self", ".", "content", "=", "args", "[", "'content'", "]", "else", ":", "raise", "Exception", "(", "'Gist content can\\'t be empty'", ")", "if", "(", "self", ".", "gist_name", "==", "''", ")", ":", "self", ".", "gist_name", "=", "self", ".", "getgist", "(", "id", "=", "self", ".", "gist_id", ")", "data", "=", "{", "\"description\"", ":", "self", ".", "description", ",", "\"files\"", ":", "{", "self", ".", "gist_name", ":", "{", "\"content\"", ":", "self", ".", "content", "}", "}", "}", "else", ":", "data", "=", "{", "\"description\"", ":", "self", ".", "description", ",", "\"files\"", ":", "{", "self", ".", "gist_name", ":", "{", "\"content\"", ":", "self", ".", "content", "}", "}", "}", "if", "self", ".", "gist_id", ":", "r", "=", "requests", ".", "patch", "(", "'%s/gists/%s'", "%", "(", "BASE_URL", ",", "self", ".", "gist_id", ")", ",", "headers", "=", "self", ".", "gist", ".", "header", ",", "data", "=", "json", ".", "dumps", "(", "data", ")", ",", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "r_text", "=", "json", ".", "loads", "(", "r", ".", "text", ")", "response", "=", "{", "'updated_content'", ":", "self", ".", "content", ",", "'created_at'", ":", "r", ".", "json", "(", ")", "[", "'created_at'", "]", ",", "'comments'", ":", "r", ".", "json", "(", ")", "[", "'comments'", "]", "}", "return", "response", "raise", "Exception", "(", "'No such gist found'", ")" ]
Doesn't require manual fetching of gistID of a gist passing gistName will return edit the gist
[ "Doesn", "t", "require", "manual", "fetching", "of", "gistID", "of", "a", "gist", "passing", "gistName", "will", "return", "edit", "the", "gist" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L132-L195
train
softvar/simplegist
simplegist/mygist.py
Mygist.delete
def delete(self, **args): ''' Delete a gist by gistname/gistID ''' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Provide GistName to delete') url = 'gists' if self.gist_id: r = requests.delete( '%s/%s/%s'%(BASE_URL,url,self.gist_id), headers=self.gist.header ) if (r.status_code == 204): response = { 'id': self.gist_id, } return response raise Exception('Can not delete gist')
python
def delete(self, **args): ''' Delete a gist by gistname/gistID ''' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Provide GistName to delete') url = 'gists' if self.gist_id: r = requests.delete( '%s/%s/%s'%(BASE_URL,url,self.gist_id), headers=self.gist.header ) if (r.status_code == 204): response = { 'id': self.gist_id, } return response raise Exception('Can not delete gist')
[ "def", "delete", "(", "self", ",", "*", "*", "args", ")", ":", "if", "'name'", "in", "args", ":", "self", ".", "gist_name", "=", "args", "[", "'name'", "]", "self", ".", "gist_id", "=", "self", ".", "getMyID", "(", "self", ".", "gist_name", ")", "elif", "'id'", "in", "args", ":", "self", ".", "gist_id", "=", "args", "[", "'id'", "]", "else", ":", "raise", "Exception", "(", "'Provide GistName to delete'", ")", "url", "=", "'gists'", "if", "self", ".", "gist_id", ":", "r", "=", "requests", ".", "delete", "(", "'%s/%s/%s'", "%", "(", "BASE_URL", ",", "url", ",", "self", ".", "gist_id", ")", ",", "headers", "=", "self", ".", "gist", ".", "header", ")", "if", "(", "r", ".", "status_code", "==", "204", ")", ":", "response", "=", "{", "'id'", ":", "self", ".", "gist_id", ",", "}", "return", "response", "raise", "Exception", "(", "'Can not delete gist'", ")" ]
Delete a gist by gistname/gistID
[ "Delete", "a", "gist", "by", "gistname", "/", "gistID" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L198-L223
train
softvar/simplegist
simplegist/mygist.py
Mygist.starred
def starred(self, **args): ''' List the authenticated user's starred gists ''' ids =[] r = requests.get( '%s/gists/starred'%BASE_URL, headers=self.gist.header ) if 'limit' in args: limit = args['limit'] else: limit = len(r.json()) if (r.status_code == 200): for g in range(0,limit ): ids.append('%s/%s/%s' %(GIST_URL,r.json()[g]['user']['login'],r.json()[g]['id'])) return ids raise Exception('Username not found')
python
def starred(self, **args): ''' List the authenticated user's starred gists ''' ids =[] r = requests.get( '%s/gists/starred'%BASE_URL, headers=self.gist.header ) if 'limit' in args: limit = args['limit'] else: limit = len(r.json()) if (r.status_code == 200): for g in range(0,limit ): ids.append('%s/%s/%s' %(GIST_URL,r.json()[g]['user']['login'],r.json()[g]['id'])) return ids raise Exception('Username not found')
[ "def", "starred", "(", "self", ",", "*", "*", "args", ")", ":", "ids", "=", "[", "]", "r", "=", "requests", ".", "get", "(", "'%s/gists/starred'", "%", "BASE_URL", ",", "headers", "=", "self", ".", "gist", ".", "header", ")", "if", "'limit'", "in", "args", ":", "limit", "=", "args", "[", "'limit'", "]", "else", ":", "limit", "=", "len", "(", "r", ".", "json", "(", ")", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "for", "g", "in", "range", "(", "0", ",", "limit", ")", ":", "ids", ".", "append", "(", "'%s/%s/%s'", "%", "(", "GIST_URL", ",", "r", ".", "json", "(", ")", "[", "g", "]", "[", "'user'", "]", "[", "'login'", "]", ",", "r", ".", "json", "(", ")", "[", "g", "]", "[", "'id'", "]", ")", ")", "return", "ids", "raise", "Exception", "(", "'Username not found'", ")" ]
List the authenticated user's starred gists
[ "List", "the", "authenticated", "user", "s", "starred", "gists" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L226-L246
train
softvar/simplegist
simplegist/mygist.py
Mygist.links
def links(self,**args): ''' Return Gist URL-Link, Clone-Link and Script-Link to embed ''' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Gist Name/ID must be provided') if self.gist_id: r = requests.get( '%s/gists/%s'%(BASE_URL,self.gist_id), headers=self.gist.header, ) if (r.status_code == 200): content = { 'Github-User': r.json()['user']['login'], 'GistID': r.json()['id'], 'Gist-Link': '%s/%s/%s' %(GIST_URL,self.gist.username,r.json()['id']), 'Clone-Link': '%s/%s.git' %(GIST_URL,r.json()['id']), 'Embed-Script': '<script src="%s/%s/%s.js"</script>' %(GIST_URL,self.gist.username,r.json()['id']) } return content raise Exception('No such gist found')
python
def links(self,**args): ''' Return Gist URL-Link, Clone-Link and Script-Link to embed ''' if 'name' in args: self.gist_name = args['name'] self.gist_id = self.getMyID(self.gist_name) elif 'id' in args: self.gist_id = args['id'] else: raise Exception('Gist Name/ID must be provided') if self.gist_id: r = requests.get( '%s/gists/%s'%(BASE_URL,self.gist_id), headers=self.gist.header, ) if (r.status_code == 200): content = { 'Github-User': r.json()['user']['login'], 'GistID': r.json()['id'], 'Gist-Link': '%s/%s/%s' %(GIST_URL,self.gist.username,r.json()['id']), 'Clone-Link': '%s/%s.git' %(GIST_URL,r.json()['id']), 'Embed-Script': '<script src="%s/%s/%s.js"</script>' %(GIST_URL,self.gist.username,r.json()['id']) } return content raise Exception('No such gist found')
[ "def", "links", "(", "self", ",", "*", "*", "args", ")", ":", "if", "'name'", "in", "args", ":", "self", ".", "gist_name", "=", "args", "[", "'name'", "]", "self", ".", "gist_id", "=", "self", ".", "getMyID", "(", "self", ".", "gist_name", ")", "elif", "'id'", "in", "args", ":", "self", ".", "gist_id", "=", "args", "[", "'id'", "]", "else", ":", "raise", "Exception", "(", "'Gist Name/ID must be provided'", ")", "if", "self", ".", "gist_id", ":", "r", "=", "requests", ".", "get", "(", "'%s/gists/%s'", "%", "(", "BASE_URL", ",", "self", ".", "gist_id", ")", ",", "headers", "=", "self", ".", "gist", ".", "header", ",", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "content", "=", "{", "'Github-User'", ":", "r", ".", "json", "(", ")", "[", "'user'", "]", "[", "'login'", "]", ",", "'GistID'", ":", "r", ".", "json", "(", ")", "[", "'id'", "]", ",", "'Gist-Link'", ":", "'%s/%s/%s'", "%", "(", "GIST_URL", ",", "self", ".", "gist", ".", "username", ",", "r", ".", "json", "(", ")", "[", "'id'", "]", ")", ",", "'Clone-Link'", ":", "'%s/%s.git'", "%", "(", "GIST_URL", ",", "r", ".", "json", "(", ")", "[", "'id'", "]", ")", ",", "'Embed-Script'", ":", "'<script src=\"%s/%s/%s.js\"</script>'", "%", "(", "GIST_URL", ",", "self", ".", "gist", ".", "username", ",", "r", ".", "json", "(", ")", "[", "'id'", "]", ")", "}", "return", "content", "raise", "Exception", "(", "'No such gist found'", ")" ]
Return Gist URL-Link, Clone-Link and Script-Link to embed
[ "Return", "Gist", "URL", "-", "Link", "Clone", "-", "Link", "and", "Script", "-", "Link", "to", "embed" ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/mygist.py#L248-L275
train
djgagne/hagelslag
hagelslag/evaluation/NeighborEvaluator.py
NeighborEvaluator.load_forecasts
def load_forecasts(self): """ Load neighborhood probability forecasts. """ run_date_str = self.run_date.strftime("%Y%m%d") forecast_file = self.forecast_path + "{0}/{1}_{2}_{3}_consensus_{0}.nc".format(run_date_str, self.ensemble_name, self.model_name, self.forecast_variable) print("Forecast file: " + forecast_file) forecast_data = Dataset(forecast_file) for size_threshold in self.size_thresholds: for smoothing_radius in self.smoothing_radii: for neighbor_radius in self.neighbor_radii: hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius, smoothing_radius, self.forecast_variable, float(size_threshold)) period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour - self.start_hour + 1, neighbor_radius, smoothing_radius, self.forecast_variable, float(size_threshold)) print("Loading forecasts {0} {1} {2} {3} {4}".format(self.run_date, self.model_name, self.forecast_variable, size_threshold, smoothing_radius)) if hour_var in forecast_data.variables.keys(): self.hourly_forecasts[hour_var] = forecast_data.variables[hour_var][:] if period_var in forecast_data.variables.keys(): self.period_forecasts[period_var] = forecast_data.variables[period_var][:] forecast_data.close()
python
def load_forecasts(self): """ Load neighborhood probability forecasts. """ run_date_str = self.run_date.strftime("%Y%m%d") forecast_file = self.forecast_path + "{0}/{1}_{2}_{3}_consensus_{0}.nc".format(run_date_str, self.ensemble_name, self.model_name, self.forecast_variable) print("Forecast file: " + forecast_file) forecast_data = Dataset(forecast_file) for size_threshold in self.size_thresholds: for smoothing_radius in self.smoothing_radii: for neighbor_radius in self.neighbor_radii: hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius, smoothing_radius, self.forecast_variable, float(size_threshold)) period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour - self.start_hour + 1, neighbor_radius, smoothing_radius, self.forecast_variable, float(size_threshold)) print("Loading forecasts {0} {1} {2} {3} {4}".format(self.run_date, self.model_name, self.forecast_variable, size_threshold, smoothing_radius)) if hour_var in forecast_data.variables.keys(): self.hourly_forecasts[hour_var] = forecast_data.variables[hour_var][:] if period_var in forecast_data.variables.keys(): self.period_forecasts[period_var] = forecast_data.variables[period_var][:] forecast_data.close()
[ "def", "load_forecasts", "(", "self", ")", ":", "run_date_str", "=", "self", ".", "run_date", ".", "strftime", "(", "\"%Y%m%d\"", ")", "forecast_file", "=", "self", ".", "forecast_path", "+", "\"{0}/{1}_{2}_{3}_consensus_{0}.nc\"", ".", "format", "(", "run_date_str", ",", "self", ".", "ensemble_name", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ")", "print", "(", "\"Forecast file: \"", "+", "forecast_file", ")", "forecast_data", "=", "Dataset", "(", "forecast_file", ")", "for", "size_threshold", "in", "self", ".", "size_thresholds", ":", "for", "smoothing_radius", "in", "self", ".", "smoothing_radii", ":", "for", "neighbor_radius", "in", "self", ".", "neighbor_radii", ":", "hour_var", "=", "\"neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}\"", ".", "format", "(", "neighbor_radius", ",", "smoothing_radius", ",", "self", ".", "forecast_variable", ",", "float", "(", "size_threshold", ")", ")", "period_var", "=", "\"neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}\"", ".", "format", "(", "self", ".", "end_hour", "-", "self", ".", "start_hour", "+", "1", ",", "neighbor_radius", ",", "smoothing_radius", ",", "self", ".", "forecast_variable", ",", "float", "(", "size_threshold", ")", ")", "print", "(", "\"Loading forecasts {0} {1} {2} {3} {4}\"", ".", "format", "(", "self", ".", "run_date", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ",", "size_threshold", ",", "smoothing_radius", ")", ")", "if", "hour_var", "in", "forecast_data", ".", "variables", ".", "keys", "(", ")", ":", "self", ".", "hourly_forecasts", "[", "hour_var", "]", "=", "forecast_data", ".", "variables", "[", "hour_var", "]", "[", ":", "]", "if", "period_var", "in", "forecast_data", ".", "variables", ".", "keys", "(", ")", ":", "self", ".", "period_forecasts", "[", "period_var", "]", "=", "forecast_data", ".", "variables", "[", "period_var", "]", "[", ":", "]", "forecast_data", ".", "close", "(", ")" ]
Load neighborhood probability forecasts.
[ "Load", "neighborhood", "probability", "forecasts", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/evaluation/NeighborEvaluator.py#L62-L93
train
djgagne/hagelslag
hagelslag/evaluation/NeighborEvaluator.py
NeighborEvaluator.load_obs
def load_obs(self, mask_threshold=0.5): """ Loads observations and masking grid (if needed). Args: mask_threshold: Values greater than the threshold are kept, others are masked. """ print("Loading obs ", self.run_date, self.model_name, self.forecast_variable) start_date = self.run_date + timedelta(hours=self.start_hour) end_date = self.run_date + timedelta(hours=self.end_hour) mrms_grid = MRMSGrid(start_date, end_date, self.mrms_variable, self.mrms_path) mrms_grid.load_data() if len(mrms_grid.data) > 0: self.raw_obs[self.mrms_variable] = np.where(mrms_grid.data > 100, 100, mrms_grid.data) self.period_obs[self.mrms_variable] = self.raw_obs[self.mrms_variable].max(axis=0) if self.obs_mask: mask_grid = MRMSGrid(start_date, end_date, self.mask_variable, self.mrms_path) mask_grid.load_data() self.raw_obs[self.mask_variable] = np.where(mask_grid.data >= mask_threshold, 1, 0) self.period_obs[self.mask_variable] = self.raw_obs[self.mask_variable].max(axis=0)
python
def load_obs(self, mask_threshold=0.5): """ Loads observations and masking grid (if needed). Args: mask_threshold: Values greater than the threshold are kept, others are masked. """ print("Loading obs ", self.run_date, self.model_name, self.forecast_variable) start_date = self.run_date + timedelta(hours=self.start_hour) end_date = self.run_date + timedelta(hours=self.end_hour) mrms_grid = MRMSGrid(start_date, end_date, self.mrms_variable, self.mrms_path) mrms_grid.load_data() if len(mrms_grid.data) > 0: self.raw_obs[self.mrms_variable] = np.where(mrms_grid.data > 100, 100, mrms_grid.data) self.period_obs[self.mrms_variable] = self.raw_obs[self.mrms_variable].max(axis=0) if self.obs_mask: mask_grid = MRMSGrid(start_date, end_date, self.mask_variable, self.mrms_path) mask_grid.load_data() self.raw_obs[self.mask_variable] = np.where(mask_grid.data >= mask_threshold, 1, 0) self.period_obs[self.mask_variable] = self.raw_obs[self.mask_variable].max(axis=0)
[ "def", "load_obs", "(", "self", ",", "mask_threshold", "=", "0.5", ")", ":", "print", "(", "\"Loading obs \"", ",", "self", ".", "run_date", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ")", "start_date", "=", "self", ".", "run_date", "+", "timedelta", "(", "hours", "=", "self", ".", "start_hour", ")", "end_date", "=", "self", ".", "run_date", "+", "timedelta", "(", "hours", "=", "self", ".", "end_hour", ")", "mrms_grid", "=", "MRMSGrid", "(", "start_date", ",", "end_date", ",", "self", ".", "mrms_variable", ",", "self", ".", "mrms_path", ")", "mrms_grid", ".", "load_data", "(", ")", "if", "len", "(", "mrms_grid", ".", "data", ")", ">", "0", ":", "self", ".", "raw_obs", "[", "self", ".", "mrms_variable", "]", "=", "np", ".", "where", "(", "mrms_grid", ".", "data", ">", "100", ",", "100", ",", "mrms_grid", ".", "data", ")", "self", ".", "period_obs", "[", "self", ".", "mrms_variable", "]", "=", "self", ".", "raw_obs", "[", "self", ".", "mrms_variable", "]", ".", "max", "(", "axis", "=", "0", ")", "if", "self", ".", "obs_mask", ":", "mask_grid", "=", "MRMSGrid", "(", "start_date", ",", "end_date", ",", "self", ".", "mask_variable", ",", "self", ".", "mrms_path", ")", "mask_grid", ".", "load_data", "(", ")", "self", ".", "raw_obs", "[", "self", ".", "mask_variable", "]", "=", "np", ".", "where", "(", "mask_grid", ".", "data", ">=", "mask_threshold", ",", "1", ",", "0", ")", "self", ".", "period_obs", "[", "self", ".", "mask_variable", "]", "=", "self", ".", "raw_obs", "[", "self", ".", "mask_variable", "]", ".", "max", "(", "axis", "=", "0", ")" ]
Loads observations and masking grid (if needed). Args: mask_threshold: Values greater than the threshold are kept, others are masked.
[ "Loads", "observations", "and", "masking", "grid", "(", "if", "needed", ")", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/evaluation/NeighborEvaluator.py#L95-L114
train
djgagne/hagelslag
hagelslag/evaluation/NeighborEvaluator.py
NeighborEvaluator.load_coordinates
def load_coordinates(self): """ Loads lat-lon coordinates from a netCDF file. """ coord_file = Dataset(self.coordinate_file) if "lon" in coord_file.variables.keys(): self.coordinates["lon"] = coord_file.variables["lon"][:] self.coordinates["lat"] = coord_file.variables["lat"][:] else: self.coordinates["lon"] = coord_file.variables["XLONG"][0] self.coordinates["lat"] = coord_file.variables["XLAT"][0] coord_file.close()
python
def load_coordinates(self): """ Loads lat-lon coordinates from a netCDF file. """ coord_file = Dataset(self.coordinate_file) if "lon" in coord_file.variables.keys(): self.coordinates["lon"] = coord_file.variables["lon"][:] self.coordinates["lat"] = coord_file.variables["lat"][:] else: self.coordinates["lon"] = coord_file.variables["XLONG"][0] self.coordinates["lat"] = coord_file.variables["XLAT"][0] coord_file.close()
[ "def", "load_coordinates", "(", "self", ")", ":", "coord_file", "=", "Dataset", "(", "self", ".", "coordinate_file", ")", "if", "\"lon\"", "in", "coord_file", ".", "variables", ".", "keys", "(", ")", ":", "self", ".", "coordinates", "[", "\"lon\"", "]", "=", "coord_file", ".", "variables", "[", "\"lon\"", "]", "[", ":", "]", "self", ".", "coordinates", "[", "\"lat\"", "]", "=", "coord_file", ".", "variables", "[", "\"lat\"", "]", "[", ":", "]", "else", ":", "self", ".", "coordinates", "[", "\"lon\"", "]", "=", "coord_file", ".", "variables", "[", "\"XLONG\"", "]", "[", "0", "]", "self", ".", "coordinates", "[", "\"lat\"", "]", "=", "coord_file", ".", "variables", "[", "\"XLAT\"", "]", "[", "0", "]", "coord_file", ".", "close", "(", ")" ]
Loads lat-lon coordinates from a netCDF file.
[ "Loads", "lat", "-", "lon", "coordinates", "from", "a", "netCDF", "file", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/evaluation/NeighborEvaluator.py#L116-L127
train
djgagne/hagelslag
hagelslag/evaluation/NeighborEvaluator.py
NeighborEvaluator.evaluate_hourly_forecasts
def evaluate_hourly_forecasts(self): """ Calculates ROC curves and Reliability scores for each forecast hour. Returns: A pandas DataFrame containing forecast metadata as well as DistributedROC and Reliability objects. """ score_columns = ["Run_Date", "Forecast_Hour", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius", "Smoothing_Radius", "Size_Threshold", "ROC", "Reliability"] all_scores = pd.DataFrame(columns=score_columns) for h, hour in enumerate(range(self.start_hour, self.end_hour + 1)): for neighbor_radius in self.neighbor_radii: n_filter = disk(neighbor_radius) for s, size_threshold in enumerate(self.size_thresholds): print("Eval hourly forecast {0:02d} {1} {2} {3} {4:d} {5:d}".format(hour, self.model_name, self.forecast_variable, self.run_date, neighbor_radius, size_threshold)) hour_obs = fftconvolve(self.raw_obs[self.mrms_variable][h] >= self.obs_thresholds[s], n_filter, mode="same") hour_obs[hour_obs > 1] = 1 hour_obs[hour_obs < 1] = 0 if self.obs_mask: hour_obs = hour_obs[self.raw_obs[self.mask_variable][h] > 0] for smoothing_radius in self.smoothing_radii: hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius, smoothing_radius, self.forecast_variable, size_threshold) if self.obs_mask: hour_forecast = self.hourly_forecasts[hour_var][h][self.raw_obs[self.mask_variable][h] > 0] else: hour_forecast = self.hourly_forecasts[hour_var][h] roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5) roc.update(hour_forecast, hour_obs) rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5) rel.update(hour_forecast, hour_obs) row = [self.run_date, hour, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius, smoothing_radius, size_threshold, roc, rel] all_scores.loc[hour_var + "_{0:d}".format(hour)] = row return all_scores
python
def evaluate_hourly_forecasts(self): """ Calculates ROC curves and Reliability scores for each forecast hour. Returns: A pandas DataFrame containing forecast metadata as well as DistributedROC and Reliability objects. """ score_columns = ["Run_Date", "Forecast_Hour", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius", "Smoothing_Radius", "Size_Threshold", "ROC", "Reliability"] all_scores = pd.DataFrame(columns=score_columns) for h, hour in enumerate(range(self.start_hour, self.end_hour + 1)): for neighbor_radius in self.neighbor_radii: n_filter = disk(neighbor_radius) for s, size_threshold in enumerate(self.size_thresholds): print("Eval hourly forecast {0:02d} {1} {2} {3} {4:d} {5:d}".format(hour, self.model_name, self.forecast_variable, self.run_date, neighbor_radius, size_threshold)) hour_obs = fftconvolve(self.raw_obs[self.mrms_variable][h] >= self.obs_thresholds[s], n_filter, mode="same") hour_obs[hour_obs > 1] = 1 hour_obs[hour_obs < 1] = 0 if self.obs_mask: hour_obs = hour_obs[self.raw_obs[self.mask_variable][h] > 0] for smoothing_radius in self.smoothing_radii: hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius, smoothing_radius, self.forecast_variable, size_threshold) if self.obs_mask: hour_forecast = self.hourly_forecasts[hour_var][h][self.raw_obs[self.mask_variable][h] > 0] else: hour_forecast = self.hourly_forecasts[hour_var][h] roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5) roc.update(hour_forecast, hour_obs) rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5) rel.update(hour_forecast, hour_obs) row = [self.run_date, hour, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius, smoothing_radius, size_threshold, roc, rel] all_scores.loc[hour_var + "_{0:d}".format(hour)] = row return all_scores
[ "def", "evaluate_hourly_forecasts", "(", "self", ")", ":", "score_columns", "=", "[", "\"Run_Date\"", ",", "\"Forecast_Hour\"", ",", "\"Ensemble Name\"", ",", "\"Model_Name\"", ",", "\"Forecast_Variable\"", ",", "\"Neighbor_Radius\"", ",", "\"Smoothing_Radius\"", ",", "\"Size_Threshold\"", ",", "\"ROC\"", ",", "\"Reliability\"", "]", "all_scores", "=", "pd", ".", "DataFrame", "(", "columns", "=", "score_columns", ")", "for", "h", ",", "hour", "in", "enumerate", "(", "range", "(", "self", ".", "start_hour", ",", "self", ".", "end_hour", "+", "1", ")", ")", ":", "for", "neighbor_radius", "in", "self", ".", "neighbor_radii", ":", "n_filter", "=", "disk", "(", "neighbor_radius", ")", "for", "s", ",", "size_threshold", "in", "enumerate", "(", "self", ".", "size_thresholds", ")", ":", "print", "(", "\"Eval hourly forecast {0:02d} {1} {2} {3} {4:d} {5:d}\"", ".", "format", "(", "hour", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ",", "self", ".", "run_date", ",", "neighbor_radius", ",", "size_threshold", ")", ")", "hour_obs", "=", "fftconvolve", "(", "self", ".", "raw_obs", "[", "self", ".", "mrms_variable", "]", "[", "h", "]", ">=", "self", ".", "obs_thresholds", "[", "s", "]", ",", "n_filter", ",", "mode", "=", "\"same\"", ")", "hour_obs", "[", "hour_obs", ">", "1", "]", "=", "1", "hour_obs", "[", "hour_obs", "<", "1", "]", "=", "0", "if", "self", ".", "obs_mask", ":", "hour_obs", "=", "hour_obs", "[", "self", ".", "raw_obs", "[", "self", ".", "mask_variable", "]", "[", "h", "]", ">", "0", "]", "for", "smoothing_radius", "in", "self", ".", "smoothing_radii", ":", "hour_var", "=", "\"neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}\"", ".", "format", "(", "neighbor_radius", ",", "smoothing_radius", ",", "self", ".", "forecast_variable", ",", "size_threshold", ")", "if", "self", ".", "obs_mask", ":", "hour_forecast", "=", "self", ".", "hourly_forecasts", "[", "hour_var", "]", "[", "h", "]", "[", "self", ".", "raw_obs", "[", "self", ".", "mask_variable", "]", "[", "h", "]", ">", "0", "]", "else", ":", "hour_forecast", "=", "self", ".", "hourly_forecasts", "[", "hour_var", "]", "[", "h", "]", "roc", "=", "DistributedROC", "(", "thresholds", "=", "self", ".", "probability_levels", ",", "obs_threshold", "=", "0.5", ")", "roc", ".", "update", "(", "hour_forecast", ",", "hour_obs", ")", "rel", "=", "DistributedReliability", "(", "thresholds", "=", "self", ".", "probability_levels", ",", "obs_threshold", "=", "0.5", ")", "rel", ".", "update", "(", "hour_forecast", ",", "hour_obs", ")", "row", "=", "[", "self", ".", "run_date", ",", "hour", ",", "self", ".", "ensemble_name", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ",", "neighbor_radius", ",", "smoothing_radius", ",", "size_threshold", ",", "roc", ",", "rel", "]", "all_scores", ".", "loc", "[", "hour_var", "+", "\"_{0:d}\"", ".", "format", "(", "hour", ")", "]", "=", "row", "return", "all_scores" ]
Calculates ROC curves and Reliability scores for each forecast hour. Returns: A pandas DataFrame containing forecast metadata as well as DistributedROC and Reliability objects.
[ "Calculates", "ROC", "curves", "and", "Reliability", "scores", "for", "each", "forecast", "hour", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/evaluation/NeighborEvaluator.py#L129-L170
train
djgagne/hagelslag
hagelslag/evaluation/NeighborEvaluator.py
NeighborEvaluator.evaluate_period_forecasts
def evaluate_period_forecasts(self): """ Evaluates ROC and Reliability scores for forecasts over the full period from start hour to end hour Returns: A pandas DataFrame with full-period metadata and verification statistics """ score_columns = ["Run_Date", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius", "Smoothing_Radius", "Size_Threshold", "ROC", "Reliability"] all_scores = pd.DataFrame(columns=score_columns) if self.coordinate_file is not None: coord_mask = np.where((self.coordinates["lon"] >= self.lon_bounds[0]) & (self.coordinates["lon"] <= self.lon_bounds[1]) & (self.coordinates["lat"] >= self.lat_bounds[0]) & (self.coordinates["lat"] <= self.lat_bounds[1]) & (self.period_obs[self.mask_variable] > 0)) else: coord_mask = None for neighbor_radius in self.neighbor_radii: n_filter = disk(neighbor_radius) for s, size_threshold in enumerate(self.size_thresholds): period_obs = fftconvolve(self.period_obs[self.mrms_variable] >= self.obs_thresholds[s], n_filter, mode="same") period_obs[period_obs > 1] = 1 if self.obs_mask and self.coordinate_file is None: period_obs = period_obs[self.period_obs[self.mask_variable] > 0] elif self.obs_mask and self.coordinate_file is not None: period_obs = period_obs[coord_mask[0], coord_mask[1]] else: period_obs = period_obs.ravel() for smoothing_radius in self.smoothing_radii: print("Eval period forecast {0} {1} {2} {3} {4} {5}".format(self.model_name, self.forecast_variable, self.run_date, neighbor_radius, size_threshold, smoothing_radius)) period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour - self.start_hour + 1, neighbor_radius, smoothing_radius, self.forecast_variable, size_threshold) if self.obs_mask and self.coordinate_file is None: period_forecast = self.period_forecasts[period_var][self.period_obs[self.mask_variable] > 0] elif self.obs_mask and self.coordinate_file is not None: period_forecast = self.period_forecasts[period_var][coord_mask[0], coord_mask[1]] else: period_forecast = self.period_forecasts[period_var].ravel() roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5) roc.update(period_forecast, period_obs) rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5) rel.update(period_forecast, period_obs) row = [self.run_date, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius, smoothing_radius, size_threshold, roc, rel] all_scores.loc[period_var] = row return all_scores
python
def evaluate_period_forecasts(self): """ Evaluates ROC and Reliability scores for forecasts over the full period from start hour to end hour Returns: A pandas DataFrame with full-period metadata and verification statistics """ score_columns = ["Run_Date", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius", "Smoothing_Radius", "Size_Threshold", "ROC", "Reliability"] all_scores = pd.DataFrame(columns=score_columns) if self.coordinate_file is not None: coord_mask = np.where((self.coordinates["lon"] >= self.lon_bounds[0]) & (self.coordinates["lon"] <= self.lon_bounds[1]) & (self.coordinates["lat"] >= self.lat_bounds[0]) & (self.coordinates["lat"] <= self.lat_bounds[1]) & (self.period_obs[self.mask_variable] > 0)) else: coord_mask = None for neighbor_radius in self.neighbor_radii: n_filter = disk(neighbor_radius) for s, size_threshold in enumerate(self.size_thresholds): period_obs = fftconvolve(self.period_obs[self.mrms_variable] >= self.obs_thresholds[s], n_filter, mode="same") period_obs[period_obs > 1] = 1 if self.obs_mask and self.coordinate_file is None: period_obs = period_obs[self.period_obs[self.mask_variable] > 0] elif self.obs_mask and self.coordinate_file is not None: period_obs = period_obs[coord_mask[0], coord_mask[1]] else: period_obs = period_obs.ravel() for smoothing_radius in self.smoothing_radii: print("Eval period forecast {0} {1} {2} {3} {4} {5}".format(self.model_name, self.forecast_variable, self.run_date, neighbor_radius, size_threshold, smoothing_radius)) period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour - self.start_hour + 1, neighbor_radius, smoothing_radius, self.forecast_variable, size_threshold) if self.obs_mask and self.coordinate_file is None: period_forecast = self.period_forecasts[period_var][self.period_obs[self.mask_variable] > 0] elif self.obs_mask and self.coordinate_file is not None: period_forecast = self.period_forecasts[period_var][coord_mask[0], coord_mask[1]] else: period_forecast = self.period_forecasts[period_var].ravel() roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5) roc.update(period_forecast, period_obs) rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5) rel.update(period_forecast, period_obs) row = [self.run_date, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius, smoothing_radius, size_threshold, roc, rel] all_scores.loc[period_var] = row return all_scores
[ "def", "evaluate_period_forecasts", "(", "self", ")", ":", "score_columns", "=", "[", "\"Run_Date\"", ",", "\"Ensemble Name\"", ",", "\"Model_Name\"", ",", "\"Forecast_Variable\"", ",", "\"Neighbor_Radius\"", ",", "\"Smoothing_Radius\"", ",", "\"Size_Threshold\"", ",", "\"ROC\"", ",", "\"Reliability\"", "]", "all_scores", "=", "pd", ".", "DataFrame", "(", "columns", "=", "score_columns", ")", "if", "self", ".", "coordinate_file", "is", "not", "None", ":", "coord_mask", "=", "np", ".", "where", "(", "(", "self", ".", "coordinates", "[", "\"lon\"", "]", ">=", "self", ".", "lon_bounds", "[", "0", "]", ")", "&", "(", "self", ".", "coordinates", "[", "\"lon\"", "]", "<=", "self", ".", "lon_bounds", "[", "1", "]", ")", "&", "(", "self", ".", "coordinates", "[", "\"lat\"", "]", ">=", "self", ".", "lat_bounds", "[", "0", "]", ")", "&", "(", "self", ".", "coordinates", "[", "\"lat\"", "]", "<=", "self", ".", "lat_bounds", "[", "1", "]", ")", "&", "(", "self", ".", "period_obs", "[", "self", ".", "mask_variable", "]", ">", "0", ")", ")", "else", ":", "coord_mask", "=", "None", "for", "neighbor_radius", "in", "self", ".", "neighbor_radii", ":", "n_filter", "=", "disk", "(", "neighbor_radius", ")", "for", "s", ",", "size_threshold", "in", "enumerate", "(", "self", ".", "size_thresholds", ")", ":", "period_obs", "=", "fftconvolve", "(", "self", ".", "period_obs", "[", "self", ".", "mrms_variable", "]", ">=", "self", ".", "obs_thresholds", "[", "s", "]", ",", "n_filter", ",", "mode", "=", "\"same\"", ")", "period_obs", "[", "period_obs", ">", "1", "]", "=", "1", "if", "self", ".", "obs_mask", "and", "self", ".", "coordinate_file", "is", "None", ":", "period_obs", "=", "period_obs", "[", "self", ".", "period_obs", "[", "self", ".", "mask_variable", "]", ">", "0", "]", "elif", "self", ".", "obs_mask", "and", "self", ".", "coordinate_file", "is", "not", "None", ":", "period_obs", "=", "period_obs", "[", "coord_mask", "[", "0", "]", ",", "coord_mask", "[", "1", "]", "]", "else", ":", "period_obs", "=", "period_obs", ".", "ravel", "(", ")", "for", "smoothing_radius", "in", "self", ".", "smoothing_radii", ":", "print", "(", "\"Eval period forecast {0} {1} {2} {3} {4} {5}\"", ".", "format", "(", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ",", "self", ".", "run_date", ",", "neighbor_radius", ",", "size_threshold", ",", "smoothing_radius", ")", ")", "period_var", "=", "\"neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}\"", ".", "format", "(", "self", ".", "end_hour", "-", "self", ".", "start_hour", "+", "1", ",", "neighbor_radius", ",", "smoothing_radius", ",", "self", ".", "forecast_variable", ",", "size_threshold", ")", "if", "self", ".", "obs_mask", "and", "self", ".", "coordinate_file", "is", "None", ":", "period_forecast", "=", "self", ".", "period_forecasts", "[", "period_var", "]", "[", "self", ".", "period_obs", "[", "self", ".", "mask_variable", "]", ">", "0", "]", "elif", "self", ".", "obs_mask", "and", "self", ".", "coordinate_file", "is", "not", "None", ":", "period_forecast", "=", "self", ".", "period_forecasts", "[", "period_var", "]", "[", "coord_mask", "[", "0", "]", ",", "coord_mask", "[", "1", "]", "]", "else", ":", "period_forecast", "=", "self", ".", "period_forecasts", "[", "period_var", "]", ".", "ravel", "(", ")", "roc", "=", "DistributedROC", "(", "thresholds", "=", "self", ".", "probability_levels", ",", "obs_threshold", "=", "0.5", ")", "roc", ".", "update", "(", "period_forecast", ",", "period_obs", ")", "rel", "=", "DistributedReliability", "(", "thresholds", "=", "self", ".", "probability_levels", ",", "obs_threshold", "=", "0.5", ")", "rel", ".", "update", "(", "period_forecast", ",", "period_obs", ")", "row", "=", "[", "self", ".", "run_date", ",", "self", ".", "ensemble_name", ",", "self", ".", "model_name", ",", "self", ".", "forecast_variable", ",", "neighbor_radius", ",", "smoothing_radius", ",", "size_threshold", ",", "roc", ",", "rel", "]", "all_scores", ".", "loc", "[", "period_var", "]", "=", "row", "return", "all_scores" ]
Evaluates ROC and Reliability scores for forecasts over the full period from start hour to end hour Returns: A pandas DataFrame with full-period metadata and verification statistics
[ "Evaluates", "ROC", "and", "Reliability", "scores", "for", "forecasts", "over", "the", "full", "period", "from", "start", "hour", "to", "end", "hour" ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/evaluation/NeighborEvaluator.py#L172-L227
train
nion-software/nionswift
nion/swift/command.py
bootstrap_main
def bootstrap_main(args): """ Main function explicitly called from the C++ code. Return the main application object. """ version_info = sys.version_info if version_info.major != 3 or version_info.minor < 6: return None, "python36" main_fn = load_module_as_package("nionui_app.nionswift") if main_fn: return main_fn(["nionui_app.nionswift"] + args, {"pyqt": None}), None return None, "main"
python
def bootstrap_main(args): """ Main function explicitly called from the C++ code. Return the main application object. """ version_info = sys.version_info if version_info.major != 3 or version_info.minor < 6: return None, "python36" main_fn = load_module_as_package("nionui_app.nionswift") if main_fn: return main_fn(["nionui_app.nionswift"] + args, {"pyqt": None}), None return None, "main"
[ "def", "bootstrap_main", "(", "args", ")", ":", "version_info", "=", "sys", ".", "version_info", "if", "version_info", ".", "major", "!=", "3", "or", "version_info", ".", "minor", "<", "6", ":", "return", "None", ",", "\"python36\"", "main_fn", "=", "load_module_as_package", "(", "\"nionui_app.nionswift\"", ")", "if", "main_fn", ":", "return", "main_fn", "(", "[", "\"nionui_app.nionswift\"", "]", "+", "args", ",", "{", "\"pyqt\"", ":", "None", "}", ")", ",", "None", "return", "None", ",", "\"main\"" ]
Main function explicitly called from the C++ code. Return the main application object.
[ "Main", "function", "explicitly", "called", "from", "the", "C", "++", "code", ".", "Return", "the", "main", "application", "object", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/command.py#L49-L60
train
nion-software/nionswift
nion/swift/model/Profile.py
_migrate_library
def _migrate_library(workspace_dir: pathlib.Path, do_logging: bool=True) -> pathlib.Path: """ Migrate library to latest version. """ library_path_11 = workspace_dir / "Nion Swift Workspace.nslib" library_path_12 = workspace_dir / "Nion Swift Library 12.nslib" library_path_13 = workspace_dir / "Nion Swift Library 13.nslib" library_paths = (library_path_11, library_path_12) library_path_latest = library_path_13 if not os.path.exists(library_path_latest): for library_path in reversed(library_paths): if os.path.exists(library_path): if do_logging: logging.info("Migrating library: %s -> %s", library_path, library_path_latest) shutil.copyfile(library_path, library_path_latest) break return library_path_latest
python
def _migrate_library(workspace_dir: pathlib.Path, do_logging: bool=True) -> pathlib.Path: """ Migrate library to latest version. """ library_path_11 = workspace_dir / "Nion Swift Workspace.nslib" library_path_12 = workspace_dir / "Nion Swift Library 12.nslib" library_path_13 = workspace_dir / "Nion Swift Library 13.nslib" library_paths = (library_path_11, library_path_12) library_path_latest = library_path_13 if not os.path.exists(library_path_latest): for library_path in reversed(library_paths): if os.path.exists(library_path): if do_logging: logging.info("Migrating library: %s -> %s", library_path, library_path_latest) shutil.copyfile(library_path, library_path_latest) break return library_path_latest
[ "def", "_migrate_library", "(", "workspace_dir", ":", "pathlib", ".", "Path", ",", "do_logging", ":", "bool", "=", "True", ")", "->", "pathlib", ".", "Path", ":", "library_path_11", "=", "workspace_dir", "/", "\"Nion Swift Workspace.nslib\"", "library_path_12", "=", "workspace_dir", "/", "\"Nion Swift Library 12.nslib\"", "library_path_13", "=", "workspace_dir", "/", "\"Nion Swift Library 13.nslib\"", "library_paths", "=", "(", "library_path_11", ",", "library_path_12", ")", "library_path_latest", "=", "library_path_13", "if", "not", "os", ".", "path", ".", "exists", "(", "library_path_latest", ")", ":", "for", "library_path", "in", "reversed", "(", "library_paths", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "library_path", ")", ":", "if", "do_logging", ":", "logging", ".", "info", "(", "\"Migrating library: %s -> %s\"", ",", "library_path", ",", "library_path_latest", ")", "shutil", ".", "copyfile", "(", "library_path", ",", "library_path_latest", ")", "break", "return", "library_path_latest" ]
Migrate library to latest version.
[ "Migrate", "library", "to", "latest", "version", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/Profile.py#L99-L117
train
base4sistemas/satcfe
satcfe/resposta/consultarstatusoperacional.py
RespostaConsultarStatusOperacional.status
def status(self): """Nome amigável do campo ``ESTADO_OPERACAO``, conforme a "Tabela de Informações do Status do SAT". """ for valor, rotulo in ESTADOS_OPERACAO: if self.ESTADO_OPERACAO == valor: return rotulo return u'(desconhecido: {})'.format(self.ESTADO_OPERACAO)
python
def status(self): """Nome amigável do campo ``ESTADO_OPERACAO``, conforme a "Tabela de Informações do Status do SAT". """ for valor, rotulo in ESTADOS_OPERACAO: if self.ESTADO_OPERACAO == valor: return rotulo return u'(desconhecido: {})'.format(self.ESTADO_OPERACAO)
[ "def", "status", "(", "self", ")", ":", "for", "valor", ",", "rotulo", "in", "ESTADOS_OPERACAO", ":", "if", "self", ".", "ESTADO_OPERACAO", "==", "valor", ":", "return", "rotulo", "return", "u'(desconhecido: {})'", ".", "format", "(", "self", ".", "ESTADO_OPERACAO", ")" ]
Nome amigável do campo ``ESTADO_OPERACAO``, conforme a "Tabela de Informações do Status do SAT".
[ "Nome", "amigável", "do", "campo", "ESTADO_OPERACAO", "conforme", "a", "Tabela", "de", "Informações", "do", "Status", "do", "SAT", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/consultarstatusoperacional.py#L123-L130
train
base4sistemas/satcfe
satcfe/resposta/consultarstatusoperacional.py
RespostaConsultarStatusOperacional.analisar
def analisar(retorno): """Constrói uma :class:`RespostaConsultarStatusOperacional` a partir do retorno informado. :param unicode retorno: Retorno da função ``ConsultarStatusOperacional``. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConsultarStatusOperacional', classe_resposta=RespostaConsultarStatusOperacional, campos=RespostaSAT.CAMPOS + ( ('NSERIE', as_clean_unicode), ('TIPO_LAN', as_clean_unicode), ('LAN_IP', normalizar_ip), ('LAN_MAC', unicode), ('LAN_MASK', normalizar_ip), ('LAN_GW', normalizar_ip), ('LAN_DNS_1', normalizar_ip), ('LAN_DNS_2', normalizar_ip), ('STATUS_LAN', as_clean_unicode), ('NIVEL_BATERIA', as_clean_unicode), ('MT_TOTAL', as_clean_unicode), ('MT_USADA', as_clean_unicode), ('DH_ATUAL', as_datetime), ('VER_SB', as_clean_unicode), ('VER_LAYOUT', as_clean_unicode), ('ULTIMO_CF_E_SAT', as_clean_unicode), ('LISTA_INICIAL', as_clean_unicode), ('LISTA_FINAL', as_clean_unicode), ('DH_CFE', as_datetime_or_none), ('DH_ULTIMA', as_datetime), ('CERT_EMISSAO', as_date), ('CERT_VENCIMENTO', as_date), ('ESTADO_OPERACAO', int), ), campos_alternativos=[ # se falhar resultarão apenas os 5 campos padrão RespostaSAT.CAMPOS, ] ) if resposta.EEEEE not in ('10000',): raise ExcecaoRespostaSAT(resposta) return resposta
python
def analisar(retorno): """Constrói uma :class:`RespostaConsultarStatusOperacional` a partir do retorno informado. :param unicode retorno: Retorno da função ``ConsultarStatusOperacional``. """ resposta = analisar_retorno(forcar_unicode(retorno), funcao='ConsultarStatusOperacional', classe_resposta=RespostaConsultarStatusOperacional, campos=RespostaSAT.CAMPOS + ( ('NSERIE', as_clean_unicode), ('TIPO_LAN', as_clean_unicode), ('LAN_IP', normalizar_ip), ('LAN_MAC', unicode), ('LAN_MASK', normalizar_ip), ('LAN_GW', normalizar_ip), ('LAN_DNS_1', normalizar_ip), ('LAN_DNS_2', normalizar_ip), ('STATUS_LAN', as_clean_unicode), ('NIVEL_BATERIA', as_clean_unicode), ('MT_TOTAL', as_clean_unicode), ('MT_USADA', as_clean_unicode), ('DH_ATUAL', as_datetime), ('VER_SB', as_clean_unicode), ('VER_LAYOUT', as_clean_unicode), ('ULTIMO_CF_E_SAT', as_clean_unicode), ('LISTA_INICIAL', as_clean_unicode), ('LISTA_FINAL', as_clean_unicode), ('DH_CFE', as_datetime_or_none), ('DH_ULTIMA', as_datetime), ('CERT_EMISSAO', as_date), ('CERT_VENCIMENTO', as_date), ('ESTADO_OPERACAO', int), ), campos_alternativos=[ # se falhar resultarão apenas os 5 campos padrão RespostaSAT.CAMPOS, ] ) if resposta.EEEEE not in ('10000',): raise ExcecaoRespostaSAT(resposta) return resposta
[ "def", "analisar", "(", "retorno", ")", ":", "resposta", "=", "analisar_retorno", "(", "forcar_unicode", "(", "retorno", ")", ",", "funcao", "=", "'ConsultarStatusOperacional'", ",", "classe_resposta", "=", "RespostaConsultarStatusOperacional", ",", "campos", "=", "RespostaSAT", ".", "CAMPOS", "+", "(", "(", "'NSERIE'", ",", "as_clean_unicode", ")", ",", "(", "'TIPO_LAN'", ",", "as_clean_unicode", ")", ",", "(", "'LAN_IP'", ",", "normalizar_ip", ")", ",", "(", "'LAN_MAC'", ",", "unicode", ")", ",", "(", "'LAN_MASK'", ",", "normalizar_ip", ")", ",", "(", "'LAN_GW'", ",", "normalizar_ip", ")", ",", "(", "'LAN_DNS_1'", ",", "normalizar_ip", ")", ",", "(", "'LAN_DNS_2'", ",", "normalizar_ip", ")", ",", "(", "'STATUS_LAN'", ",", "as_clean_unicode", ")", ",", "(", "'NIVEL_BATERIA'", ",", "as_clean_unicode", ")", ",", "(", "'MT_TOTAL'", ",", "as_clean_unicode", ")", ",", "(", "'MT_USADA'", ",", "as_clean_unicode", ")", ",", "(", "'DH_ATUAL'", ",", "as_datetime", ")", ",", "(", "'VER_SB'", ",", "as_clean_unicode", ")", ",", "(", "'VER_LAYOUT'", ",", "as_clean_unicode", ")", ",", "(", "'ULTIMO_CF_E_SAT'", ",", "as_clean_unicode", ")", ",", "(", "'LISTA_INICIAL'", ",", "as_clean_unicode", ")", ",", "(", "'LISTA_FINAL'", ",", "as_clean_unicode", ")", ",", "(", "'DH_CFE'", ",", "as_datetime_or_none", ")", ",", "(", "'DH_ULTIMA'", ",", "as_datetime", ")", ",", "(", "'CERT_EMISSAO'", ",", "as_date", ")", ",", "(", "'CERT_VENCIMENTO'", ",", "as_date", ")", ",", "(", "'ESTADO_OPERACAO'", ",", "int", ")", ",", ")", ",", "campos_alternativos", "=", "[", "# se falhar resultarão apenas os 5 campos padrão", "RespostaSAT", ".", "CAMPOS", ",", "]", ")", "if", "resposta", ".", "EEEEE", "not", "in", "(", "'10000'", ",", ")", ":", "raise", "ExcecaoRespostaSAT", "(", "resposta", ")", "return", "resposta" ]
Constrói uma :class:`RespostaConsultarStatusOperacional` a partir do retorno informado. :param unicode retorno: Retorno da função ``ConsultarStatusOperacional``.
[ "Constrói", "uma", ":", "class", ":", "RespostaConsultarStatusOperacional", "a", "partir", "do", "retorno", "informado", "." ]
cb8e8815f4133d3e3d94cf526fa86767b4521ed9
https://github.com/base4sistemas/satcfe/blob/cb8e8815f4133d3e3d94cf526fa86767b4521ed9/satcfe/resposta/consultarstatusoperacional.py#L134-L175
train
djgagne/hagelslag
hagelslag/util/merge_forecast_data.py
merge_input_csv_forecast_json
def merge_input_csv_forecast_json(input_csv_file, forecast_json_path, condition_models, dist_models): """ Reads forecasts from json files and merges them with the input data from the step csv files. Args: input_csv_file: Name of the input data csv file being processed forecast_json_path: Path to the forecast json files toplevel directory condition_models: List of models used to forecast hail or no hail dist_models: List of models used to forecast the hail size distribution Returns: """ try: run_date = input_csv_file[:-4].split("_")[-1] print(run_date) ens_member = "_".join(input_csv_file.split("/")[-1][:-4].split("_")[3:-1]) ens_name = input_csv_file.split("/")[-1].split("_")[2] input_data = pd.read_csv(input_csv_file, index_col="Step_ID") full_json_path = forecast_json_path + "{0}/{1}/".format(run_date, ens_member) track_ids = sorted(input_data["Track_ID"].unique()) model_pred_cols = [] condition_models_ns = [] dist_models_ns = [] gamma_params = ["Shape", "Location", "Scale"] for condition_model in condition_models: model_pred_cols.append(condition_model.replace(" ", "-") + "_Condition") condition_models_ns.append(condition_model.replace(" ", "-")) for dist_model in dist_models: dist_models_ns.append(dist_model.replace(" ", "-")) for param in gamma_params: model_pred_cols.append(dist_model.replace(" ", "-") + "_" + param) pred_data = pd.DataFrame(index=input_data.index, columns=model_pred_cols, dtype=float) for track_id in track_ids: track_id_num = track_id.split("_")[-1] json_filename = full_json_path + "{0}_{1}_{2}_model_track_{3}.json".format(ens_name, run_date, ens_member, track_id_num) json_file = open(json_filename) json_data = json.load(json_file) json_file.close() for s, step in enumerate(json_data["features"]): step_id = track_id + "_{0:02d}".format(s) for cond_model in condition_models_ns: pred_data.loc[step_id, cond_model + "_Condition"] = step["properties"]["condition_" + cond_model] for dist_model in dist_models_ns: pred_data.loc[step_id, [dist_model + "_" + p for p in gamma_params]] = step["properties"]["dist_" + dist_model] out_data = input_data.merge(pred_data, left_index=True, right_index=True) return out_data, ens_name, ens_member except Exception as e: print(traceback.format_exc()) raise e
python
def merge_input_csv_forecast_json(input_csv_file, forecast_json_path, condition_models, dist_models): """ Reads forecasts from json files and merges them with the input data from the step csv files. Args: input_csv_file: Name of the input data csv file being processed forecast_json_path: Path to the forecast json files toplevel directory condition_models: List of models used to forecast hail or no hail dist_models: List of models used to forecast the hail size distribution Returns: """ try: run_date = input_csv_file[:-4].split("_")[-1] print(run_date) ens_member = "_".join(input_csv_file.split("/")[-1][:-4].split("_")[3:-1]) ens_name = input_csv_file.split("/")[-1].split("_")[2] input_data = pd.read_csv(input_csv_file, index_col="Step_ID") full_json_path = forecast_json_path + "{0}/{1}/".format(run_date, ens_member) track_ids = sorted(input_data["Track_ID"].unique()) model_pred_cols = [] condition_models_ns = [] dist_models_ns = [] gamma_params = ["Shape", "Location", "Scale"] for condition_model in condition_models: model_pred_cols.append(condition_model.replace(" ", "-") + "_Condition") condition_models_ns.append(condition_model.replace(" ", "-")) for dist_model in dist_models: dist_models_ns.append(dist_model.replace(" ", "-")) for param in gamma_params: model_pred_cols.append(dist_model.replace(" ", "-") + "_" + param) pred_data = pd.DataFrame(index=input_data.index, columns=model_pred_cols, dtype=float) for track_id in track_ids: track_id_num = track_id.split("_")[-1] json_filename = full_json_path + "{0}_{1}_{2}_model_track_{3}.json".format(ens_name, run_date, ens_member, track_id_num) json_file = open(json_filename) json_data = json.load(json_file) json_file.close() for s, step in enumerate(json_data["features"]): step_id = track_id + "_{0:02d}".format(s) for cond_model in condition_models_ns: pred_data.loc[step_id, cond_model + "_Condition"] = step["properties"]["condition_" + cond_model] for dist_model in dist_models_ns: pred_data.loc[step_id, [dist_model + "_" + p for p in gamma_params]] = step["properties"]["dist_" + dist_model] out_data = input_data.merge(pred_data, left_index=True, right_index=True) return out_data, ens_name, ens_member except Exception as e: print(traceback.format_exc()) raise e
[ "def", "merge_input_csv_forecast_json", "(", "input_csv_file", ",", "forecast_json_path", ",", "condition_models", ",", "dist_models", ")", ":", "try", ":", "run_date", "=", "input_csv_file", "[", ":", "-", "4", "]", ".", "split", "(", "\"_\"", ")", "[", "-", "1", "]", "print", "(", "run_date", ")", "ens_member", "=", "\"_\"", ".", "join", "(", "input_csv_file", ".", "split", "(", "\"/\"", ")", "[", "-", "1", "]", "[", ":", "-", "4", "]", ".", "split", "(", "\"_\"", ")", "[", "3", ":", "-", "1", "]", ")", "ens_name", "=", "input_csv_file", ".", "split", "(", "\"/\"", ")", "[", "-", "1", "]", ".", "split", "(", "\"_\"", ")", "[", "2", "]", "input_data", "=", "pd", ".", "read_csv", "(", "input_csv_file", ",", "index_col", "=", "\"Step_ID\"", ")", "full_json_path", "=", "forecast_json_path", "+", "\"{0}/{1}/\"", ".", "format", "(", "run_date", ",", "ens_member", ")", "track_ids", "=", "sorted", "(", "input_data", "[", "\"Track_ID\"", "]", ".", "unique", "(", ")", ")", "model_pred_cols", "=", "[", "]", "condition_models_ns", "=", "[", "]", "dist_models_ns", "=", "[", "]", "gamma_params", "=", "[", "\"Shape\"", ",", "\"Location\"", ",", "\"Scale\"", "]", "for", "condition_model", "in", "condition_models", ":", "model_pred_cols", ".", "append", "(", "condition_model", ".", "replace", "(", "\" \"", ",", "\"-\"", ")", "+", "\"_Condition\"", ")", "condition_models_ns", ".", "append", "(", "condition_model", ".", "replace", "(", "\" \"", ",", "\"-\"", ")", ")", "for", "dist_model", "in", "dist_models", ":", "dist_models_ns", ".", "append", "(", "dist_model", ".", "replace", "(", "\" \"", ",", "\"-\"", ")", ")", "for", "param", "in", "gamma_params", ":", "model_pred_cols", ".", "append", "(", "dist_model", ".", "replace", "(", "\" \"", ",", "\"-\"", ")", "+", "\"_\"", "+", "param", ")", "pred_data", "=", "pd", ".", "DataFrame", "(", "index", "=", "input_data", ".", "index", ",", "columns", "=", "model_pred_cols", ",", "dtype", "=", "float", ")", "for", "track_id", "in", "track_ids", ":", "track_id_num", "=", "track_id", ".", "split", "(", "\"_\"", ")", "[", "-", "1", "]", "json_filename", "=", "full_json_path", "+", "\"{0}_{1}_{2}_model_track_{3}.json\"", ".", "format", "(", "ens_name", ",", "run_date", ",", "ens_member", ",", "track_id_num", ")", "json_file", "=", "open", "(", "json_filename", ")", "json_data", "=", "json", ".", "load", "(", "json_file", ")", "json_file", ".", "close", "(", ")", "for", "s", ",", "step", "in", "enumerate", "(", "json_data", "[", "\"features\"", "]", ")", ":", "step_id", "=", "track_id", "+", "\"_{0:02d}\"", ".", "format", "(", "s", ")", "for", "cond_model", "in", "condition_models_ns", ":", "pred_data", ".", "loc", "[", "step_id", ",", "cond_model", "+", "\"_Condition\"", "]", "=", "step", "[", "\"properties\"", "]", "[", "\"condition_\"", "+", "cond_model", "]", "for", "dist_model", "in", "dist_models_ns", ":", "pred_data", ".", "loc", "[", "step_id", ",", "[", "dist_model", "+", "\"_\"", "+", "p", "for", "p", "in", "gamma_params", "]", "]", "=", "step", "[", "\"properties\"", "]", "[", "\"dist_\"", "+", "dist_model", "]", "out_data", "=", "input_data", ".", "merge", "(", "pred_data", ",", "left_index", "=", "True", ",", "right_index", "=", "True", ")", "return", "out_data", ",", "ens_name", ",", "ens_member", "except", "Exception", "as", "e", ":", "print", "(", "traceback", ".", "format_exc", "(", ")", ")", "raise", "e" ]
Reads forecasts from json files and merges them with the input data from the step csv files. Args: input_csv_file: Name of the input data csv file being processed forecast_json_path: Path to the forecast json files toplevel directory condition_models: List of models used to forecast hail or no hail dist_models: List of models used to forecast the hail size distribution Returns:
[ "Reads", "forecasts", "from", "json", "files", "and", "merges", "them", "with", "the", "input", "data", "from", "the", "step", "csv", "files", "." ]
6fb6c3df90bf4867e13a97d3460b14471d107df1
https://github.com/djgagne/hagelslag/blob/6fb6c3df90bf4867e13a97d3460b14471d107df1/hagelslag/util/merge_forecast_data.py#L53-L107
train
nion-software/nionswift
nion/swift/Thumbnails.py
ThumbnailProcessor.mark_data_dirty
def mark_data_dirty(self): """ Called from item to indicate its data or metadata has changed.""" self.__cache.set_cached_value_dirty(self.__display_item, self.__cache_property_name) self.__initialize_cache() self.__cached_value_dirty = True
python
def mark_data_dirty(self): """ Called from item to indicate its data or metadata has changed.""" self.__cache.set_cached_value_dirty(self.__display_item, self.__cache_property_name) self.__initialize_cache() self.__cached_value_dirty = True
[ "def", "mark_data_dirty", "(", "self", ")", ":", "self", ".", "__cache", ".", "set_cached_value_dirty", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ")", "self", ".", "__initialize_cache", "(", ")", "self", ".", "__cached_value_dirty", "=", "True" ]
Called from item to indicate its data or metadata has changed.
[ "Called", "from", "item", "to", "indicate", "its", "data", "or", "metadata", "has", "changed", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/Thumbnails.py#L56-L60
train
nion-software/nionswift
nion/swift/Thumbnails.py
ThumbnailProcessor.__initialize_cache
def __initialize_cache(self): """Initialize the cache values (cache values are used for optimization).""" if self.__cached_value_dirty is None: self.__cached_value_dirty = self.__cache.is_cached_value_dirty(self.__display_item, self.__cache_property_name) self.__cached_value = self.__cache.get_cached_value(self.__display_item, self.__cache_property_name)
python
def __initialize_cache(self): """Initialize the cache values (cache values are used for optimization).""" if self.__cached_value_dirty is None: self.__cached_value_dirty = self.__cache.is_cached_value_dirty(self.__display_item, self.__cache_property_name) self.__cached_value = self.__cache.get_cached_value(self.__display_item, self.__cache_property_name)
[ "def", "__initialize_cache", "(", "self", ")", ":", "if", "self", ".", "__cached_value_dirty", "is", "None", ":", "self", ".", "__cached_value_dirty", "=", "self", ".", "__cache", ".", "is_cached_value_dirty", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ")", "self", ".", "__cached_value", "=", "self", ".", "__cache", ".", "get_cached_value", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ")" ]
Initialize the cache values (cache values are used for optimization).
[ "Initialize", "the", "cache", "values", "(", "cache", "values", "are", "used", "for", "optimization", ")", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/Thumbnails.py#L62-L66
train
nion-software/nionswift
nion/swift/Thumbnails.py
ThumbnailProcessor.recompute_if_necessary
def recompute_if_necessary(self, ui): """Recompute the data on a thread, if necessary. If the data has recently been computed, this call will be rescheduled for the future. If the data is currently being computed, it do nothing.""" self.__initialize_cache() if self.__cached_value_dirty: with self.__is_recomputing_lock: is_recomputing = self.__is_recomputing self.__is_recomputing = True if is_recomputing: pass else: # the only way to get here is if we're not currently computing # this has the side effect of limiting the number of threads that # are sleeping. def recompute(): try: if self.__recompute_thread_cancel.wait(0.01): # helps tests run faster return minimum_time = 0.5 current_time = time.time() if current_time < self.__cached_value_time + minimum_time: if self.__recompute_thread_cancel.wait(self.__cached_value_time + minimum_time - current_time): return self.recompute_data(ui) finally: self.__is_recomputing = False self.__recompute_thread = None with self.__is_recomputing_lock: self.__recompute_thread = threading.Thread(target=recompute) self.__recompute_thread.start()
python
def recompute_if_necessary(self, ui): """Recompute the data on a thread, if necessary. If the data has recently been computed, this call will be rescheduled for the future. If the data is currently being computed, it do nothing.""" self.__initialize_cache() if self.__cached_value_dirty: with self.__is_recomputing_lock: is_recomputing = self.__is_recomputing self.__is_recomputing = True if is_recomputing: pass else: # the only way to get here is if we're not currently computing # this has the side effect of limiting the number of threads that # are sleeping. def recompute(): try: if self.__recompute_thread_cancel.wait(0.01): # helps tests run faster return minimum_time = 0.5 current_time = time.time() if current_time < self.__cached_value_time + minimum_time: if self.__recompute_thread_cancel.wait(self.__cached_value_time + minimum_time - current_time): return self.recompute_data(ui) finally: self.__is_recomputing = False self.__recompute_thread = None with self.__is_recomputing_lock: self.__recompute_thread = threading.Thread(target=recompute) self.__recompute_thread.start()
[ "def", "recompute_if_necessary", "(", "self", ",", "ui", ")", ":", "self", ".", "__initialize_cache", "(", ")", "if", "self", ".", "__cached_value_dirty", ":", "with", "self", ".", "__is_recomputing_lock", ":", "is_recomputing", "=", "self", ".", "__is_recomputing", "self", ".", "__is_recomputing", "=", "True", "if", "is_recomputing", ":", "pass", "else", ":", "# the only way to get here is if we're not currently computing", "# this has the side effect of limiting the number of threads that", "# are sleeping.", "def", "recompute", "(", ")", ":", "try", ":", "if", "self", ".", "__recompute_thread_cancel", ".", "wait", "(", "0.01", ")", ":", "# helps tests run faster", "return", "minimum_time", "=", "0.5", "current_time", "=", "time", ".", "time", "(", ")", "if", "current_time", "<", "self", ".", "__cached_value_time", "+", "minimum_time", ":", "if", "self", ".", "__recompute_thread_cancel", ".", "wait", "(", "self", ".", "__cached_value_time", "+", "minimum_time", "-", "current_time", ")", ":", "return", "self", ".", "recompute_data", "(", "ui", ")", "finally", ":", "self", ".", "__is_recomputing", "=", "False", "self", ".", "__recompute_thread", "=", "None", "with", "self", ".", "__is_recomputing_lock", ":", "self", ".", "__recompute_thread", "=", "threading", ".", "Thread", "(", "target", "=", "recompute", ")", "self", ".", "__recompute_thread", ".", "start", "(", ")" ]
Recompute the data on a thread, if necessary. If the data has recently been computed, this call will be rescheduled for the future. If the data is currently being computed, it do nothing.
[ "Recompute", "the", "data", "on", "a", "thread", "if", "necessary", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/Thumbnails.py#L68-L100
train
nion-software/nionswift
nion/swift/Thumbnails.py
ThumbnailProcessor.recompute_data
def recompute_data(self, ui): """Compute the data associated with this processor. This method is thread safe and may take a long time to return. It should not be called from the UI thread. Upon return, the results will be calculated with the latest data available and the cache will not be marked dirty. """ self.__initialize_cache() with self.__recompute_lock: if self.__cached_value_dirty: try: calculated_data = self.get_calculated_data(ui) except Exception as e: import traceback traceback.print_exc() traceback.print_stack() raise self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data) self.__cached_value = calculated_data self.__cached_value_dirty = False self.__cached_value_time = time.time() else: calculated_data = None if calculated_data is None: calculated_data = self.get_default_data() if calculated_data is not None: # if the default is not None, treat is as valid cached data self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data) self.__cached_value = calculated_data self.__cached_value_dirty = False self.__cached_value_time = time.time() else: # otherwise remove everything from the cache self.__cache.remove_cached_value(self.__display_item, self.__cache_property_name) self.__cached_value = None self.__cached_value_dirty = None self.__cached_value_time = 0 self.__recompute_lock.release() if callable(self.on_thumbnail_updated): self.on_thumbnail_updated() self.__recompute_lock.acquire()
python
def recompute_data(self, ui): """Compute the data associated with this processor. This method is thread safe and may take a long time to return. It should not be called from the UI thread. Upon return, the results will be calculated with the latest data available and the cache will not be marked dirty. """ self.__initialize_cache() with self.__recompute_lock: if self.__cached_value_dirty: try: calculated_data = self.get_calculated_data(ui) except Exception as e: import traceback traceback.print_exc() traceback.print_stack() raise self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data) self.__cached_value = calculated_data self.__cached_value_dirty = False self.__cached_value_time = time.time() else: calculated_data = None if calculated_data is None: calculated_data = self.get_default_data() if calculated_data is not None: # if the default is not None, treat is as valid cached data self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data) self.__cached_value = calculated_data self.__cached_value_dirty = False self.__cached_value_time = time.time() else: # otherwise remove everything from the cache self.__cache.remove_cached_value(self.__display_item, self.__cache_property_name) self.__cached_value = None self.__cached_value_dirty = None self.__cached_value_time = 0 self.__recompute_lock.release() if callable(self.on_thumbnail_updated): self.on_thumbnail_updated() self.__recompute_lock.acquire()
[ "def", "recompute_data", "(", "self", ",", "ui", ")", ":", "self", ".", "__initialize_cache", "(", ")", "with", "self", ".", "__recompute_lock", ":", "if", "self", ".", "__cached_value_dirty", ":", "try", ":", "calculated_data", "=", "self", ".", "get_calculated_data", "(", "ui", ")", "except", "Exception", "as", "e", ":", "import", "traceback", "traceback", ".", "print_exc", "(", ")", "traceback", ".", "print_stack", "(", ")", "raise", "self", ".", "__cache", ".", "set_cached_value", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ",", "calculated_data", ")", "self", ".", "__cached_value", "=", "calculated_data", "self", ".", "__cached_value_dirty", "=", "False", "self", ".", "__cached_value_time", "=", "time", ".", "time", "(", ")", "else", ":", "calculated_data", "=", "None", "if", "calculated_data", "is", "None", ":", "calculated_data", "=", "self", ".", "get_default_data", "(", ")", "if", "calculated_data", "is", "not", "None", ":", "# if the default is not None, treat is as valid cached data", "self", ".", "__cache", ".", "set_cached_value", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ",", "calculated_data", ")", "self", ".", "__cached_value", "=", "calculated_data", "self", ".", "__cached_value_dirty", "=", "False", "self", ".", "__cached_value_time", "=", "time", ".", "time", "(", ")", "else", ":", "# otherwise remove everything from the cache", "self", ".", "__cache", ".", "remove_cached_value", "(", "self", ".", "__display_item", ",", "self", ".", "__cache_property_name", ")", "self", ".", "__cached_value", "=", "None", "self", ".", "__cached_value_dirty", "=", "None", "self", ".", "__cached_value_time", "=", "0", "self", ".", "__recompute_lock", ".", "release", "(", ")", "if", "callable", "(", "self", ".", "on_thumbnail_updated", ")", ":", "self", ".", "on_thumbnail_updated", "(", ")", "self", ".", "__recompute_lock", ".", "acquire", "(", ")" ]
Compute the data associated with this processor. This method is thread safe and may take a long time to return. It should not be called from the UI thread. Upon return, the results will be calculated with the latest data available and the cache will not be marked dirty.
[ "Compute", "the", "data", "associated", "with", "this", "processor", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/Thumbnails.py#L102-L142
train
nion-software/nionswift
nion/swift/Thumbnails.py
ThumbnailManager.thumbnail_source_for_display_item
def thumbnail_source_for_display_item(self, ui, display_item: DisplayItem.DisplayItem) -> ThumbnailSource: """Returned ThumbnailSource must be closed.""" with self.__lock: thumbnail_source = self.__thumbnail_sources.get(display_item) if not thumbnail_source: thumbnail_source = ThumbnailSource(ui, display_item) self.__thumbnail_sources[display_item] = thumbnail_source def will_delete(thumbnail_source): del self.__thumbnail_sources[thumbnail_source._display_item] thumbnail_source._on_will_delete = will_delete else: assert thumbnail_source._ui == ui return thumbnail_source.add_ref()
python
def thumbnail_source_for_display_item(self, ui, display_item: DisplayItem.DisplayItem) -> ThumbnailSource: """Returned ThumbnailSource must be closed.""" with self.__lock: thumbnail_source = self.__thumbnail_sources.get(display_item) if not thumbnail_source: thumbnail_source = ThumbnailSource(ui, display_item) self.__thumbnail_sources[display_item] = thumbnail_source def will_delete(thumbnail_source): del self.__thumbnail_sources[thumbnail_source._display_item] thumbnail_source._on_will_delete = will_delete else: assert thumbnail_source._ui == ui return thumbnail_source.add_ref()
[ "def", "thumbnail_source_for_display_item", "(", "self", ",", "ui", ",", "display_item", ":", "DisplayItem", ".", "DisplayItem", ")", "->", "ThumbnailSource", ":", "with", "self", ".", "__lock", ":", "thumbnail_source", "=", "self", ".", "__thumbnail_sources", ".", "get", "(", "display_item", ")", "if", "not", "thumbnail_source", ":", "thumbnail_source", "=", "ThumbnailSource", "(", "ui", ",", "display_item", ")", "self", ".", "__thumbnail_sources", "[", "display_item", "]", "=", "thumbnail_source", "def", "will_delete", "(", "thumbnail_source", ")", ":", "del", "self", ".", "__thumbnail_sources", "[", "thumbnail_source", ".", "_display_item", "]", "thumbnail_source", ".", "_on_will_delete", "=", "will_delete", "else", ":", "assert", "thumbnail_source", ".", "_ui", "==", "ui", "return", "thumbnail_source", ".", "add_ref", "(", ")" ]
Returned ThumbnailSource must be closed.
[ "Returned", "ThumbnailSource", "must", "be", "closed", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/Thumbnails.py#L242-L256
train
nion-software/nionswift
nion/swift/model/PlugInManager.py
load_plug_ins
def load_plug_ins(app, root_dir): """Load plug-ins.""" global extensions ui = app.ui # a list of directories in which sub-directories PlugIns will be searched. subdirectories = [] # the default location is where the directory main packages are located. if root_dir: subdirectories.append(root_dir) # also search the default data location; create directory there if it doesn't exist to make it easier for user. # default data location will be application specific. data_location = ui.get_data_location() if data_location is not None: subdirectories.append(data_location) # create directories here if they don't exist plugins_dir = os.path.abspath(os.path.join(data_location, "PlugIns")) if not os.path.exists(plugins_dir): logging.info("Creating plug-ins directory %s", plugins_dir) os.makedirs(plugins_dir) # search the Nion/Swift subdirectory of the default document location too, # but don't create directories here - avoid polluting user visible directories. document_location = ui.get_document_location() if document_location is not None: subdirectories.append(os.path.join(document_location, "Nion", "Swift")) # do not create them in documents if they don't exist. this location is optional. # build a list of directories that will be loaded as plug-ins. PlugInDir = collections.namedtuple("PlugInDir", ["directory", "relative_path"]) plugin_dirs = list() # track directories that have already been searched. seen_plugin_dirs = list() # for each subdirectory, look in PlugIns for sub-directories that represent the plug-ins. for subdirectory in subdirectories: plugins_dir = os.path.abspath(os.path.join(subdirectory, "PlugIns")) if os.path.exists(plugins_dir) and not plugins_dir in seen_plugin_dirs: logging.info("Loading plug-ins from %s", plugins_dir) # add the PlugIns directory to the system import path. sys.path.append(plugins_dir) # now build a list of sub-directories representing plug-ins within plugins_dir. sorted_relative_paths = sorted([d for d in os.listdir(plugins_dir) if os.path.isdir(os.path.join(plugins_dir, d))]) plugin_dirs.extend([PlugInDir(plugins_dir, sorted_relative_path) for sorted_relative_path in sorted_relative_paths]) # mark plugins_dir as 'seen' to avoid search it twice. seen_plugin_dirs.append(plugins_dir) else: logging.info("NOT Loading plug-ins from %s (missing)", plugins_dir) version_map = dict() module_exists_map = dict() plugin_adapters = list() import nionswift_plugin for module_info in pkgutil.iter_modules(nionswift_plugin.__path__): plugin_adapters.append(ModuleAdapter(nionswift_plugin.__name__, module_info)) for directory, relative_path in plugin_dirs: plugin_adapters.append(PlugInAdapter(directory, relative_path)) progress = True while progress: progress = False plugin_adapters_copy = copy.deepcopy(plugin_adapters) plugin_adapters = list() for plugin_adapter in plugin_adapters_copy: manifest_path = plugin_adapter.manifest_path manifest = plugin_adapter.manifest if manifest: manifest_valid = True if not "name" in manifest: logging.info("Invalid manifest (missing 'name'): %s", manifest_path) manifest_valid = False if not "identifier" in manifest: logging.info("Invalid manifest (missing 'identifier'): %s", manifest_path) manifest_valid = False if "identifier" in manifest and not re.match("[_\-a-zA-Z][_\-a-zA-Z0-9.]*$", manifest["identifier"]): logging.info("Invalid manifest (invalid 'identifier': '%s'): %s", manifest["identifier"], manifest_path) manifest_valid = False if not "version" in manifest: logging.info("Invalid manifest (missing 'version'): %s", manifest_path) manifest_valid = False if "requires" in manifest and not isinstance(manifest["requires"], list): logging.info("Invalid manifest ('requires' not a list): %s", manifest_path) manifest_valid = False if not manifest_valid: continue for module in manifest.get("modules", list()): if module in module_exists_map: module_exists = module_exists_map.get(module) else: module_exists = importlib.util.find_spec(module) is not None module_exists_map[module] = module_exists if not module_exists: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").") logging.info("Cannot satisfy requirement (%s): %s", module, manifest_path) manifest_valid = False break for requirement in manifest.get("requires", list()): # TODO: see https://packaging.pypa.io/en/latest/ requirement_components = requirement.split() if len(requirement_components) != 3 or requirement_components[1] != "~=": logging.info("Invalid manifest (requirement '%s' invalid): %s", requirement, manifest_path) manifest_valid = False break identifier, operator, version_specifier = requirement_components[0], requirement_components[1], requirement_components[2] if identifier in version_map: if Utility.compare_versions("~" + version_specifier, version_map[identifier]) != 0: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").") logging.info("Cannot satisfy requirement (%s): %s", requirement, manifest_path) manifest_valid = False break else: # requirements not loaded yet; add back to plugin_adapters, but don't mark progress since nothing was loaded. logging.info("Plug-in '" + plugin_adapter.module_name + "' delayed (%s) (" + plugin_adapter.module_path + ").", requirement) plugin_adapters.append(plugin_adapter) manifest_valid = False break if not manifest_valid: continue version_map[manifest["identifier"]] = manifest["version"] # read the manifests, if any # repeat loop of plug-ins until no plug-ins left in the list # if all dependencies satisfied for a plug-in, load it # otherwise defer until next round # stop if no plug-ins loaded in the round # count on the user to have correct dependencies module = plugin_adapter.load() if module: __modules.append(module) progress = True for plugin_adapter in plugin_adapters: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (requirements) (" + plugin_adapter.module_path + ").") notify_modules("run")
python
def load_plug_ins(app, root_dir): """Load plug-ins.""" global extensions ui = app.ui # a list of directories in which sub-directories PlugIns will be searched. subdirectories = [] # the default location is where the directory main packages are located. if root_dir: subdirectories.append(root_dir) # also search the default data location; create directory there if it doesn't exist to make it easier for user. # default data location will be application specific. data_location = ui.get_data_location() if data_location is not None: subdirectories.append(data_location) # create directories here if they don't exist plugins_dir = os.path.abspath(os.path.join(data_location, "PlugIns")) if not os.path.exists(plugins_dir): logging.info("Creating plug-ins directory %s", plugins_dir) os.makedirs(plugins_dir) # search the Nion/Swift subdirectory of the default document location too, # but don't create directories here - avoid polluting user visible directories. document_location = ui.get_document_location() if document_location is not None: subdirectories.append(os.path.join(document_location, "Nion", "Swift")) # do not create them in documents if they don't exist. this location is optional. # build a list of directories that will be loaded as plug-ins. PlugInDir = collections.namedtuple("PlugInDir", ["directory", "relative_path"]) plugin_dirs = list() # track directories that have already been searched. seen_plugin_dirs = list() # for each subdirectory, look in PlugIns for sub-directories that represent the plug-ins. for subdirectory in subdirectories: plugins_dir = os.path.abspath(os.path.join(subdirectory, "PlugIns")) if os.path.exists(plugins_dir) and not plugins_dir in seen_plugin_dirs: logging.info("Loading plug-ins from %s", plugins_dir) # add the PlugIns directory to the system import path. sys.path.append(plugins_dir) # now build a list of sub-directories representing plug-ins within plugins_dir. sorted_relative_paths = sorted([d for d in os.listdir(plugins_dir) if os.path.isdir(os.path.join(plugins_dir, d))]) plugin_dirs.extend([PlugInDir(plugins_dir, sorted_relative_path) for sorted_relative_path in sorted_relative_paths]) # mark plugins_dir as 'seen' to avoid search it twice. seen_plugin_dirs.append(plugins_dir) else: logging.info("NOT Loading plug-ins from %s (missing)", plugins_dir) version_map = dict() module_exists_map = dict() plugin_adapters = list() import nionswift_plugin for module_info in pkgutil.iter_modules(nionswift_plugin.__path__): plugin_adapters.append(ModuleAdapter(nionswift_plugin.__name__, module_info)) for directory, relative_path in plugin_dirs: plugin_adapters.append(PlugInAdapter(directory, relative_path)) progress = True while progress: progress = False plugin_adapters_copy = copy.deepcopy(plugin_adapters) plugin_adapters = list() for plugin_adapter in plugin_adapters_copy: manifest_path = plugin_adapter.manifest_path manifest = plugin_adapter.manifest if manifest: manifest_valid = True if not "name" in manifest: logging.info("Invalid manifest (missing 'name'): %s", manifest_path) manifest_valid = False if not "identifier" in manifest: logging.info("Invalid manifest (missing 'identifier'): %s", manifest_path) manifest_valid = False if "identifier" in manifest and not re.match("[_\-a-zA-Z][_\-a-zA-Z0-9.]*$", manifest["identifier"]): logging.info("Invalid manifest (invalid 'identifier': '%s'): %s", manifest["identifier"], manifest_path) manifest_valid = False if not "version" in manifest: logging.info("Invalid manifest (missing 'version'): %s", manifest_path) manifest_valid = False if "requires" in manifest and not isinstance(manifest["requires"], list): logging.info("Invalid manifest ('requires' not a list): %s", manifest_path) manifest_valid = False if not manifest_valid: continue for module in manifest.get("modules", list()): if module in module_exists_map: module_exists = module_exists_map.get(module) else: module_exists = importlib.util.find_spec(module) is not None module_exists_map[module] = module_exists if not module_exists: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").") logging.info("Cannot satisfy requirement (%s): %s", module, manifest_path) manifest_valid = False break for requirement in manifest.get("requires", list()): # TODO: see https://packaging.pypa.io/en/latest/ requirement_components = requirement.split() if len(requirement_components) != 3 or requirement_components[1] != "~=": logging.info("Invalid manifest (requirement '%s' invalid): %s", requirement, manifest_path) manifest_valid = False break identifier, operator, version_specifier = requirement_components[0], requirement_components[1], requirement_components[2] if identifier in version_map: if Utility.compare_versions("~" + version_specifier, version_map[identifier]) != 0: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").") logging.info("Cannot satisfy requirement (%s): %s", requirement, manifest_path) manifest_valid = False break else: # requirements not loaded yet; add back to plugin_adapters, but don't mark progress since nothing was loaded. logging.info("Plug-in '" + plugin_adapter.module_name + "' delayed (%s) (" + plugin_adapter.module_path + ").", requirement) plugin_adapters.append(plugin_adapter) manifest_valid = False break if not manifest_valid: continue version_map[manifest["identifier"]] = manifest["version"] # read the manifests, if any # repeat loop of plug-ins until no plug-ins left in the list # if all dependencies satisfied for a plug-in, load it # otherwise defer until next round # stop if no plug-ins loaded in the round # count on the user to have correct dependencies module = plugin_adapter.load() if module: __modules.append(module) progress = True for plugin_adapter in plugin_adapters: logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (requirements) (" + plugin_adapter.module_path + ").") notify_modules("run")
[ "def", "load_plug_ins", "(", "app", ",", "root_dir", ")", ":", "global", "extensions", "ui", "=", "app", ".", "ui", "# a list of directories in which sub-directories PlugIns will be searched.", "subdirectories", "=", "[", "]", "# the default location is where the directory main packages are located.", "if", "root_dir", ":", "subdirectories", ".", "append", "(", "root_dir", ")", "# also search the default data location; create directory there if it doesn't exist to make it easier for user.", "# default data location will be application specific.", "data_location", "=", "ui", ".", "get_data_location", "(", ")", "if", "data_location", "is", "not", "None", ":", "subdirectories", ".", "append", "(", "data_location", ")", "# create directories here if they don't exist", "plugins_dir", "=", "os", ".", "path", ".", "abspath", "(", "os", ".", "path", ".", "join", "(", "data_location", ",", "\"PlugIns\"", ")", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "plugins_dir", ")", ":", "logging", ".", "info", "(", "\"Creating plug-ins directory %s\"", ",", "plugins_dir", ")", "os", ".", "makedirs", "(", "plugins_dir", ")", "# search the Nion/Swift subdirectory of the default document location too,", "# but don't create directories here - avoid polluting user visible directories.", "document_location", "=", "ui", ".", "get_document_location", "(", ")", "if", "document_location", "is", "not", "None", ":", "subdirectories", ".", "append", "(", "os", ".", "path", ".", "join", "(", "document_location", ",", "\"Nion\"", ",", "\"Swift\"", ")", ")", "# do not create them in documents if they don't exist. this location is optional.", "# build a list of directories that will be loaded as plug-ins.", "PlugInDir", "=", "collections", ".", "namedtuple", "(", "\"PlugInDir\"", ",", "[", "\"directory\"", ",", "\"relative_path\"", "]", ")", "plugin_dirs", "=", "list", "(", ")", "# track directories that have already been searched.", "seen_plugin_dirs", "=", "list", "(", ")", "# for each subdirectory, look in PlugIns for sub-directories that represent the plug-ins.", "for", "subdirectory", "in", "subdirectories", ":", "plugins_dir", "=", "os", ".", "path", ".", "abspath", "(", "os", ".", "path", ".", "join", "(", "subdirectory", ",", "\"PlugIns\"", ")", ")", "if", "os", ".", "path", ".", "exists", "(", "plugins_dir", ")", "and", "not", "plugins_dir", "in", "seen_plugin_dirs", ":", "logging", ".", "info", "(", "\"Loading plug-ins from %s\"", ",", "plugins_dir", ")", "# add the PlugIns directory to the system import path.", "sys", ".", "path", ".", "append", "(", "plugins_dir", ")", "# now build a list of sub-directories representing plug-ins within plugins_dir.", "sorted_relative_paths", "=", "sorted", "(", "[", "d", "for", "d", "in", "os", ".", "listdir", "(", "plugins_dir", ")", "if", "os", ".", "path", ".", "isdir", "(", "os", ".", "path", ".", "join", "(", "plugins_dir", ",", "d", ")", ")", "]", ")", "plugin_dirs", ".", "extend", "(", "[", "PlugInDir", "(", "plugins_dir", ",", "sorted_relative_path", ")", "for", "sorted_relative_path", "in", "sorted_relative_paths", "]", ")", "# mark plugins_dir as 'seen' to avoid search it twice.", "seen_plugin_dirs", ".", "append", "(", "plugins_dir", ")", "else", ":", "logging", ".", "info", "(", "\"NOT Loading plug-ins from %s (missing)\"", ",", "plugins_dir", ")", "version_map", "=", "dict", "(", ")", "module_exists_map", "=", "dict", "(", ")", "plugin_adapters", "=", "list", "(", ")", "import", "nionswift_plugin", "for", "module_info", "in", "pkgutil", ".", "iter_modules", "(", "nionswift_plugin", ".", "__path__", ")", ":", "plugin_adapters", ".", "append", "(", "ModuleAdapter", "(", "nionswift_plugin", ".", "__name__", ",", "module_info", ")", ")", "for", "directory", ",", "relative_path", "in", "plugin_dirs", ":", "plugin_adapters", ".", "append", "(", "PlugInAdapter", "(", "directory", ",", "relative_path", ")", ")", "progress", "=", "True", "while", "progress", ":", "progress", "=", "False", "plugin_adapters_copy", "=", "copy", ".", "deepcopy", "(", "plugin_adapters", ")", "plugin_adapters", "=", "list", "(", ")", "for", "plugin_adapter", "in", "plugin_adapters_copy", ":", "manifest_path", "=", "plugin_adapter", ".", "manifest_path", "manifest", "=", "plugin_adapter", ".", "manifest", "if", "manifest", ":", "manifest_valid", "=", "True", "if", "not", "\"name\"", "in", "manifest", ":", "logging", ".", "info", "(", "\"Invalid manifest (missing 'name'): %s\"", ",", "manifest_path", ")", "manifest_valid", "=", "False", "if", "not", "\"identifier\"", "in", "manifest", ":", "logging", ".", "info", "(", "\"Invalid manifest (missing 'identifier'): %s\"", ",", "manifest_path", ")", "manifest_valid", "=", "False", "if", "\"identifier\"", "in", "manifest", "and", "not", "re", ".", "match", "(", "\"[_\\-a-zA-Z][_\\-a-zA-Z0-9.]*$\"", ",", "manifest", "[", "\"identifier\"", "]", ")", ":", "logging", ".", "info", "(", "\"Invalid manifest (invalid 'identifier': '%s'): %s\"", ",", "manifest", "[", "\"identifier\"", "]", ",", "manifest_path", ")", "manifest_valid", "=", "False", "if", "not", "\"version\"", "in", "manifest", ":", "logging", ".", "info", "(", "\"Invalid manifest (missing 'version'): %s\"", ",", "manifest_path", ")", "manifest_valid", "=", "False", "if", "\"requires\"", "in", "manifest", "and", "not", "isinstance", "(", "manifest", "[", "\"requires\"", "]", ",", "list", ")", ":", "logging", ".", "info", "(", "\"Invalid manifest ('requires' not a list): %s\"", ",", "manifest_path", ")", "manifest_valid", "=", "False", "if", "not", "manifest_valid", ":", "continue", "for", "module", "in", "manifest", ".", "get", "(", "\"modules\"", ",", "list", "(", ")", ")", ":", "if", "module", "in", "module_exists_map", ":", "module_exists", "=", "module_exists_map", ".", "get", "(", "module", ")", "else", ":", "module_exists", "=", "importlib", ".", "util", ".", "find_spec", "(", "module", ")", "is", "not", "None", "module_exists_map", "[", "module", "]", "=", "module_exists", "if", "not", "module_exists", ":", "logging", ".", "info", "(", "\"Plug-in '\"", "+", "plugin_adapter", ".", "module_name", "+", "\"' NOT loaded (\"", "+", "plugin_adapter", ".", "module_path", "+", "\").\"", ")", "logging", ".", "info", "(", "\"Cannot satisfy requirement (%s): %s\"", ",", "module", ",", "manifest_path", ")", "manifest_valid", "=", "False", "break", "for", "requirement", "in", "manifest", ".", "get", "(", "\"requires\"", ",", "list", "(", ")", ")", ":", "# TODO: see https://packaging.pypa.io/en/latest/", "requirement_components", "=", "requirement", ".", "split", "(", ")", "if", "len", "(", "requirement_components", ")", "!=", "3", "or", "requirement_components", "[", "1", "]", "!=", "\"~=\"", ":", "logging", ".", "info", "(", "\"Invalid manifest (requirement '%s' invalid): %s\"", ",", "requirement", ",", "manifest_path", ")", "manifest_valid", "=", "False", "break", "identifier", ",", "operator", ",", "version_specifier", "=", "requirement_components", "[", "0", "]", ",", "requirement_components", "[", "1", "]", ",", "requirement_components", "[", "2", "]", "if", "identifier", "in", "version_map", ":", "if", "Utility", ".", "compare_versions", "(", "\"~\"", "+", "version_specifier", ",", "version_map", "[", "identifier", "]", ")", "!=", "0", ":", "logging", ".", "info", "(", "\"Plug-in '\"", "+", "plugin_adapter", ".", "module_name", "+", "\"' NOT loaded (\"", "+", "plugin_adapter", ".", "module_path", "+", "\").\"", ")", "logging", ".", "info", "(", "\"Cannot satisfy requirement (%s): %s\"", ",", "requirement", ",", "manifest_path", ")", "manifest_valid", "=", "False", "break", "else", ":", "# requirements not loaded yet; add back to plugin_adapters, but don't mark progress since nothing was loaded.", "logging", ".", "info", "(", "\"Plug-in '\"", "+", "plugin_adapter", ".", "module_name", "+", "\"' delayed (%s) (\"", "+", "plugin_adapter", ".", "module_path", "+", "\").\"", ",", "requirement", ")", "plugin_adapters", ".", "append", "(", "plugin_adapter", ")", "manifest_valid", "=", "False", "break", "if", "not", "manifest_valid", ":", "continue", "version_map", "[", "manifest", "[", "\"identifier\"", "]", "]", "=", "manifest", "[", "\"version\"", "]", "# read the manifests, if any", "# repeat loop of plug-ins until no plug-ins left in the list", "# if all dependencies satisfied for a plug-in, load it", "# otherwise defer until next round", "# stop if no plug-ins loaded in the round", "# count on the user to have correct dependencies", "module", "=", "plugin_adapter", ".", "load", "(", ")", "if", "module", ":", "__modules", ".", "append", "(", "module", ")", "progress", "=", "True", "for", "plugin_adapter", "in", "plugin_adapters", ":", "logging", ".", "info", "(", "\"Plug-in '\"", "+", "plugin_adapter", ".", "module_name", "+", "\"' NOT loaded (requirements) (\"", "+", "plugin_adapter", ".", "module_path", "+", "\").\"", ")", "notify_modules", "(", "\"run\"", ")" ]
Load plug-ins.
[ "Load", "plug", "-", "ins", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/PlugInManager.py#L140-L283
train
softvar/simplegist
simplegist/comments.py
Comments.getMyID
def getMyID(self,gist_name): ''' Getting gistID of a gist in order to make the workflow easy and uninterrupted. ''' r = requests.get( '%s'%BASE_URL+'/users/%s/gists' % self.user, headers=self.gist.header ) if (r.status_code == 200): r_text = json.loads(r.text) limit = len(r.json()) for g,no in zip(r_text, range(0,limit)): for ka,va in r.json()[no]['files'].iteritems(): if str(va['filename']) == str(gist_name): return r.json()[no]['id'] return 0 raise Exception('Username not found')
python
def getMyID(self,gist_name): ''' Getting gistID of a gist in order to make the workflow easy and uninterrupted. ''' r = requests.get( '%s'%BASE_URL+'/users/%s/gists' % self.user, headers=self.gist.header ) if (r.status_code == 200): r_text = json.loads(r.text) limit = len(r.json()) for g,no in zip(r_text, range(0,limit)): for ka,va in r.json()[no]['files'].iteritems(): if str(va['filename']) == str(gist_name): return r.json()[no]['id'] return 0 raise Exception('Username not found')
[ "def", "getMyID", "(", "self", ",", "gist_name", ")", ":", "r", "=", "requests", ".", "get", "(", "'%s'", "%", "BASE_URL", "+", "'/users/%s/gists'", "%", "self", ".", "user", ",", "headers", "=", "self", ".", "gist", ".", "header", ")", "if", "(", "r", ".", "status_code", "==", "200", ")", ":", "r_text", "=", "json", ".", "loads", "(", "r", ".", "text", ")", "limit", "=", "len", "(", "r", ".", "json", "(", ")", ")", "for", "g", ",", "no", "in", "zip", "(", "r_text", ",", "range", "(", "0", ",", "limit", ")", ")", ":", "for", "ka", ",", "va", "in", "r", ".", "json", "(", ")", "[", "no", "]", "[", "'files'", "]", ".", "iteritems", "(", ")", ":", "if", "str", "(", "va", "[", "'filename'", "]", ")", "==", "str", "(", "gist_name", ")", ":", "return", "r", ".", "json", "(", ")", "[", "no", "]", "[", "'id'", "]", "return", "0", "raise", "Exception", "(", "'Username not found'", ")" ]
Getting gistID of a gist in order to make the workflow easy and uninterrupted.
[ "Getting", "gistID", "of", "a", "gist", "in", "order", "to", "make", "the", "workflow", "easy", "and", "uninterrupted", "." ]
8d53edd15d76c7b10fb963a659c1cf9f46f5345d
https://github.com/softvar/simplegist/blob/8d53edd15d76c7b10fb963a659c1cf9f46f5345d/simplegist/comments.py#L10-L29
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController.close
def close(self): """Close the document controller. This method must be called to shut down the document controller. There are several paths by which it can be called, though. * User quits application via menu item. The menu item will call back to Application.exit which will close each document controller by calling this method. * User quits application using dock menu item. The Qt application will call aboutToClose in the document windows * User closes document window via menu item. * User closes document window via close box. The main concept of closing is that it is always triggered by the document window closing. This can be initiated from within Python by calling request_close on the document window. When the window closes, either by explicit request or by the user clicking a close box, it will invoke the about_to_close method on the document window. At this point, the window would still be open, so the about_to_close message can be used to tell the document controller to save anything it needs to save and prepare for closing. """ assert self.__closed == False self.__closed = True self.finish_periodic() # required to finish periodic operations during tests # dialogs for weak_dialog in self.__dialogs: dialog = weak_dialog() if dialog: try: dialog.request_close() except Exception as e: pass # menus self._file_menu = None self._edit_menu = None self._processing_menu = None self._view_menu = None self._window_menu = None self._help_menu = None self._library_menu = None self._processing_arithmetic_menu = None self._processing_reduce_menu = None self._processing_transform_menu = None self._processing_filter_menu = None self._processing_fourier_menu = None self._processing_graphics_menu = None self._processing_sequence_menu = None self._processing_redimension_menu = None self._display_type_menu = None if self.__workspace_controller: self.__workspace_controller.close() self.__workspace_controller = None self.__call_soon_event_listener.close() self.__call_soon_event_listener = None self.__filtered_display_items_model.close() self.__filtered_display_items_model = None self.filter_controller.close() self.filter_controller = None self.__display_items_model.close() self.__display_items_model = None # document_model may be shared between several DocumentControllers, so use reference counting # to determine when to close it. self.document_model.remove_ref() self.document_model = None self.did_close_event.fire(self) self.did_close_event = None super().close()
python
def close(self): """Close the document controller. This method must be called to shut down the document controller. There are several paths by which it can be called, though. * User quits application via menu item. The menu item will call back to Application.exit which will close each document controller by calling this method. * User quits application using dock menu item. The Qt application will call aboutToClose in the document windows * User closes document window via menu item. * User closes document window via close box. The main concept of closing is that it is always triggered by the document window closing. This can be initiated from within Python by calling request_close on the document window. When the window closes, either by explicit request or by the user clicking a close box, it will invoke the about_to_close method on the document window. At this point, the window would still be open, so the about_to_close message can be used to tell the document controller to save anything it needs to save and prepare for closing. """ assert self.__closed == False self.__closed = True self.finish_periodic() # required to finish periodic operations during tests # dialogs for weak_dialog in self.__dialogs: dialog = weak_dialog() if dialog: try: dialog.request_close() except Exception as e: pass # menus self._file_menu = None self._edit_menu = None self._processing_menu = None self._view_menu = None self._window_menu = None self._help_menu = None self._library_menu = None self._processing_arithmetic_menu = None self._processing_reduce_menu = None self._processing_transform_menu = None self._processing_filter_menu = None self._processing_fourier_menu = None self._processing_graphics_menu = None self._processing_sequence_menu = None self._processing_redimension_menu = None self._display_type_menu = None if self.__workspace_controller: self.__workspace_controller.close() self.__workspace_controller = None self.__call_soon_event_listener.close() self.__call_soon_event_listener = None self.__filtered_display_items_model.close() self.__filtered_display_items_model = None self.filter_controller.close() self.filter_controller = None self.__display_items_model.close() self.__display_items_model = None # document_model may be shared between several DocumentControllers, so use reference counting # to determine when to close it. self.document_model.remove_ref() self.document_model = None self.did_close_event.fire(self) self.did_close_event = None super().close()
[ "def", "close", "(", "self", ")", ":", "assert", "self", ".", "__closed", "==", "False", "self", ".", "__closed", "=", "True", "self", ".", "finish_periodic", "(", ")", "# required to finish periodic operations during tests", "# dialogs", "for", "weak_dialog", "in", "self", ".", "__dialogs", ":", "dialog", "=", "weak_dialog", "(", ")", "if", "dialog", ":", "try", ":", "dialog", ".", "request_close", "(", ")", "except", "Exception", "as", "e", ":", "pass", "# menus", "self", ".", "_file_menu", "=", "None", "self", ".", "_edit_menu", "=", "None", "self", ".", "_processing_menu", "=", "None", "self", ".", "_view_menu", "=", "None", "self", ".", "_window_menu", "=", "None", "self", ".", "_help_menu", "=", "None", "self", ".", "_library_menu", "=", "None", "self", ".", "_processing_arithmetic_menu", "=", "None", "self", ".", "_processing_reduce_menu", "=", "None", "self", ".", "_processing_transform_menu", "=", "None", "self", ".", "_processing_filter_menu", "=", "None", "self", ".", "_processing_fourier_menu", "=", "None", "self", ".", "_processing_graphics_menu", "=", "None", "self", ".", "_processing_sequence_menu", "=", "None", "self", ".", "_processing_redimension_menu", "=", "None", "self", ".", "_display_type_menu", "=", "None", "if", "self", ".", "__workspace_controller", ":", "self", ".", "__workspace_controller", ".", "close", "(", ")", "self", ".", "__workspace_controller", "=", "None", "self", ".", "__call_soon_event_listener", ".", "close", "(", ")", "self", ".", "__call_soon_event_listener", "=", "None", "self", ".", "__filtered_display_items_model", ".", "close", "(", ")", "self", ".", "__filtered_display_items_model", "=", "None", "self", ".", "filter_controller", ".", "close", "(", ")", "self", ".", "filter_controller", "=", "None", "self", ".", "__display_items_model", ".", "close", "(", ")", "self", ".", "__display_items_model", "=", "None", "# document_model may be shared between several DocumentControllers, so use reference counting", "# to determine when to close it.", "self", ".", "document_model", ".", "remove_ref", "(", ")", "self", ".", "document_model", "=", "None", "self", ".", "did_close_event", ".", "fire", "(", "self", ")", "self", ".", "did_close_event", "=", "None", "super", "(", ")", ".", "close", "(", ")" ]
Close the document controller. This method must be called to shut down the document controller. There are several paths by which it can be called, though. * User quits application via menu item. The menu item will call back to Application.exit which will close each document controller by calling this method. * User quits application using dock menu item. The Qt application will call aboutToClose in the document windows * User closes document window via menu item. * User closes document window via close box. The main concept of closing is that it is always triggered by the document window closing. This can be initiated from within Python by calling request_close on the document window. When the window closes, either by explicit request or by the user clicking a close box, it will invoke the about_to_close method on the document window. At this point, the window would still be open, so the about_to_close message can be used to tell the document controller to save anything it needs to save and prepare for closing.
[ "Close", "the", "document", "controller", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L130-L194
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController.add_periodic
def add_periodic(self, interval: float, listener_fn): """Add a listener function and return listener token. Token can be closed or deleted to unlisten.""" class PeriodicListener: def __init__(self, interval: float, listener_fn): self.interval = interval self.__listener_fn = listener_fn # the call function is very performance critical; make it fast by using a property # instead of a logic statement each time. if callable(listener_fn): self.call = self.__listener_fn else: def void(*args, **kwargs): pass self.call = void self.next_scheduled_time = time.time() + interval def close(self): self.__listener_fn = None def void(*args, **kwargs): pass self.call = void listener = PeriodicListener(interval, listener_fn) def remove_listener(weak_listener): with self.__weak_periodic_listeners_mutex: self.__weak_periodic_listeners.remove(weak_listener) weak_listener = weakref.ref(listener, remove_listener) with self.__weak_periodic_listeners_mutex: self.__weak_periodic_listeners.append(weak_listener) return listener
python
def add_periodic(self, interval: float, listener_fn): """Add a listener function and return listener token. Token can be closed or deleted to unlisten.""" class PeriodicListener: def __init__(self, interval: float, listener_fn): self.interval = interval self.__listener_fn = listener_fn # the call function is very performance critical; make it fast by using a property # instead of a logic statement each time. if callable(listener_fn): self.call = self.__listener_fn else: def void(*args, **kwargs): pass self.call = void self.next_scheduled_time = time.time() + interval def close(self): self.__listener_fn = None def void(*args, **kwargs): pass self.call = void listener = PeriodicListener(interval, listener_fn) def remove_listener(weak_listener): with self.__weak_periodic_listeners_mutex: self.__weak_periodic_listeners.remove(weak_listener) weak_listener = weakref.ref(listener, remove_listener) with self.__weak_periodic_listeners_mutex: self.__weak_periodic_listeners.append(weak_listener) return listener
[ "def", "add_periodic", "(", "self", ",", "interval", ":", "float", ",", "listener_fn", ")", ":", "class", "PeriodicListener", ":", "def", "__init__", "(", "self", ",", "interval", ":", "float", ",", "listener_fn", ")", ":", "self", ".", "interval", "=", "interval", "self", ".", "__listener_fn", "=", "listener_fn", "# the call function is very performance critical; make it fast by using a property", "# instead of a logic statement each time.", "if", "callable", "(", "listener_fn", ")", ":", "self", ".", "call", "=", "self", ".", "__listener_fn", "else", ":", "def", "void", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "pass", "self", ".", "call", "=", "void", "self", ".", "next_scheduled_time", "=", "time", ".", "time", "(", ")", "+", "interval", "def", "close", "(", "self", ")", ":", "self", ".", "__listener_fn", "=", "None", "def", "void", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "pass", "self", ".", "call", "=", "void", "listener", "=", "PeriodicListener", "(", "interval", ",", "listener_fn", ")", "def", "remove_listener", "(", "weak_listener", ")", ":", "with", "self", ".", "__weak_periodic_listeners_mutex", ":", "self", ".", "__weak_periodic_listeners", ".", "remove", "(", "weak_listener", ")", "weak_listener", "=", "weakref", ".", "ref", "(", "listener", ",", "remove_listener", ")", "with", "self", ".", "__weak_periodic_listeners_mutex", ":", "self", ".", "__weak_periodic_listeners", ".", "append", "(", "weak_listener", ")", "return", "listener" ]
Add a listener function and return listener token. Token can be closed or deleted to unlisten.
[ "Add", "a", "listener", "function", "and", "return", "listener", "token", ".", "Token", "can", "be", "closed", "or", "deleted", "to", "unlisten", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L562-L589
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController.__update_display_items_model
def __update_display_items_model(self, display_items_model: ListModel.FilteredListModel, data_group: typing.Optional[DataGroup.DataGroup], filter_id: typing.Optional[str]) -> None: """Update the data item model with a new container, filter, and sorting. This is called when the data item model is created or when the user changes the data group or sorting settings. """ with display_items_model.changes(): # change filter and sort together if data_group is not None: display_items_model.container = data_group display_items_model.filter = ListModel.Filter(True) display_items_model.sort_key = None display_items_model.filter_id = None elif filter_id == "latest-session": display_items_model.container = self.document_model display_items_model.filter = ListModel.EqFilter("session_id", self.document_model.session_id) display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id elif filter_id == "temporary": display_items_model.container = self.document_model display_items_model.filter = ListModel.NotEqFilter("category", "persistent") display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id elif filter_id == "none": # not intended to be used directly display_items_model.container = self.document_model display_items_model.filter = ListModel.Filter(False) display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id else: # "all" display_items_model.container = self.document_model display_items_model.filter = ListModel.EqFilter("category", "persistent") display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = None
python
def __update_display_items_model(self, display_items_model: ListModel.FilteredListModel, data_group: typing.Optional[DataGroup.DataGroup], filter_id: typing.Optional[str]) -> None: """Update the data item model with a new container, filter, and sorting. This is called when the data item model is created or when the user changes the data group or sorting settings. """ with display_items_model.changes(): # change filter and sort together if data_group is not None: display_items_model.container = data_group display_items_model.filter = ListModel.Filter(True) display_items_model.sort_key = None display_items_model.filter_id = None elif filter_id == "latest-session": display_items_model.container = self.document_model display_items_model.filter = ListModel.EqFilter("session_id", self.document_model.session_id) display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id elif filter_id == "temporary": display_items_model.container = self.document_model display_items_model.filter = ListModel.NotEqFilter("category", "persistent") display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id elif filter_id == "none": # not intended to be used directly display_items_model.container = self.document_model display_items_model.filter = ListModel.Filter(False) display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = filter_id else: # "all" display_items_model.container = self.document_model display_items_model.filter = ListModel.EqFilter("category", "persistent") display_items_model.sort_key = DataItem.sort_by_date_key display_items_model.sort_reverse = True display_items_model.filter_id = None
[ "def", "__update_display_items_model", "(", "self", ",", "display_items_model", ":", "ListModel", ".", "FilteredListModel", ",", "data_group", ":", "typing", ".", "Optional", "[", "DataGroup", ".", "DataGroup", "]", ",", "filter_id", ":", "typing", ".", "Optional", "[", "str", "]", ")", "->", "None", ":", "with", "display_items_model", ".", "changes", "(", ")", ":", "# change filter and sort together", "if", "data_group", "is", "not", "None", ":", "display_items_model", ".", "container", "=", "data_group", "display_items_model", ".", "filter", "=", "ListModel", ".", "Filter", "(", "True", ")", "display_items_model", ".", "sort_key", "=", "None", "display_items_model", ".", "filter_id", "=", "None", "elif", "filter_id", "==", "\"latest-session\"", ":", "display_items_model", ".", "container", "=", "self", ".", "document_model", "display_items_model", ".", "filter", "=", "ListModel", ".", "EqFilter", "(", "\"session_id\"", ",", "self", ".", "document_model", ".", "session_id", ")", "display_items_model", ".", "sort_key", "=", "DataItem", ".", "sort_by_date_key", "display_items_model", ".", "sort_reverse", "=", "True", "display_items_model", ".", "filter_id", "=", "filter_id", "elif", "filter_id", "==", "\"temporary\"", ":", "display_items_model", ".", "container", "=", "self", ".", "document_model", "display_items_model", ".", "filter", "=", "ListModel", ".", "NotEqFilter", "(", "\"category\"", ",", "\"persistent\"", ")", "display_items_model", ".", "sort_key", "=", "DataItem", ".", "sort_by_date_key", "display_items_model", ".", "sort_reverse", "=", "True", "display_items_model", ".", "filter_id", "=", "filter_id", "elif", "filter_id", "==", "\"none\"", ":", "# not intended to be used directly", "display_items_model", ".", "container", "=", "self", ".", "document_model", "display_items_model", ".", "filter", "=", "ListModel", ".", "Filter", "(", "False", ")", "display_items_model", ".", "sort_key", "=", "DataItem", ".", "sort_by_date_key", "display_items_model", ".", "sort_reverse", "=", "True", "display_items_model", ".", "filter_id", "=", "filter_id", "else", ":", "# \"all\"", "display_items_model", ".", "container", "=", "self", ".", "document_model", "display_items_model", ".", "filter", "=", "ListModel", ".", "EqFilter", "(", "\"category\"", ",", "\"persistent\"", ")", "display_items_model", ".", "sort_key", "=", "DataItem", ".", "sort_by_date_key", "display_items_model", ".", "sort_reverse", "=", "True", "display_items_model", ".", "filter_id", "=", "None" ]
Update the data item model with a new container, filter, and sorting. This is called when the data item model is created or when the user changes the data group or sorting settings.
[ "Update", "the", "data", "item", "model", "with", "a", "new", "container", "filter", "and", "sorting", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L658-L694
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController.focused_data_item
def focused_data_item(self) -> typing.Optional[DataItem.DataItem]: """Return the data item with keyboard focus.""" return self.__focused_display_item.data_item if self.__focused_display_item else None
python
def focused_data_item(self) -> typing.Optional[DataItem.DataItem]: """Return the data item with keyboard focus.""" return self.__focused_display_item.data_item if self.__focused_display_item else None
[ "def", "focused_data_item", "(", "self", ")", "->", "typing", ".", "Optional", "[", "DataItem", ".", "DataItem", "]", ":", "return", "self", ".", "__focused_display_item", ".", "data_item", "if", "self", ".", "__focused_display_item", "else", "None" ]
Return the data item with keyboard focus.
[ "Return", "the", "data", "item", "with", "keyboard", "focus", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L772-L774
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController.selected_display_item
def selected_display_item(self) -> typing.Optional[DisplayItem.DisplayItem]: """Return the selected display item. The selected display is the display ite that has keyboard focus in the data panel or a display panel. """ # first check for the [focused] data browser display_item = self.focused_display_item if not display_item: selected_display_panel = self.selected_display_panel display_item = selected_display_panel.display_item if selected_display_panel else None return display_item
python
def selected_display_item(self) -> typing.Optional[DisplayItem.DisplayItem]: """Return the selected display item. The selected display is the display ite that has keyboard focus in the data panel or a display panel. """ # first check for the [focused] data browser display_item = self.focused_display_item if not display_item: selected_display_panel = self.selected_display_panel display_item = selected_display_panel.display_item if selected_display_panel else None return display_item
[ "def", "selected_display_item", "(", "self", ")", "->", "typing", ".", "Optional", "[", "DisplayItem", ".", "DisplayItem", "]", ":", "# first check for the [focused] data browser", "display_item", "=", "self", ".", "focused_display_item", "if", "not", "display_item", ":", "selected_display_panel", "=", "self", ".", "selected_display_panel", "display_item", "=", "selected_display_panel", ".", "display_item", "if", "selected_display_panel", "else", "None", "return", "display_item" ]
Return the selected display item. The selected display is the display ite that has keyboard focus in the data panel or a display panel.
[ "Return", "the", "selected", "display", "item", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L795-L805
train
nion-software/nionswift
nion/swift/DocumentController.py
DocumentController._get_two_data_sources
def _get_two_data_sources(self): """Get two sensible data sources, which may be the same.""" selected_display_items = self.selected_display_items if len(selected_display_items) < 2: selected_display_items = list() display_item = self.selected_display_item if display_item: selected_display_items.append(display_item) if len(selected_display_items) == 1: display_item = selected_display_items[0] data_item = display_item.data_item if display_item else None if display_item and len(display_item.graphic_selection.indexes) == 2: index1 = display_item.graphic_selection.anchor_index index2 = list(display_item.graphic_selection.indexes.difference({index1}))[0] graphic1 = display_item.graphics[index1] graphic2 = display_item.graphics[index2] if data_item: if data_item.is_datum_1d and isinstance(graphic1, Graphics.IntervalGraphic) and isinstance(graphic2, Graphics.IntervalGraphic): crop_graphic1 = graphic1 crop_graphic2 = graphic2 elif data_item.is_datum_2d and isinstance(graphic1, Graphics.RectangleTypeGraphic) and isinstance(graphic2, Graphics.RectangleTypeGraphic): crop_graphic1 = graphic1 crop_graphic2 = graphic2 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 return (display_item, crop_graphic1), (display_item, crop_graphic2) if len(selected_display_items) == 2: display_item1 = selected_display_items[0] crop_graphic1 = self.__get_crop_graphic(display_item1) display_item2 = selected_display_items[1] crop_graphic2 = self.__get_crop_graphic(display_item2) return (display_item1, crop_graphic1), (display_item2, crop_graphic2) return None
python
def _get_two_data_sources(self): """Get two sensible data sources, which may be the same.""" selected_display_items = self.selected_display_items if len(selected_display_items) < 2: selected_display_items = list() display_item = self.selected_display_item if display_item: selected_display_items.append(display_item) if len(selected_display_items) == 1: display_item = selected_display_items[0] data_item = display_item.data_item if display_item else None if display_item and len(display_item.graphic_selection.indexes) == 2: index1 = display_item.graphic_selection.anchor_index index2 = list(display_item.graphic_selection.indexes.difference({index1}))[0] graphic1 = display_item.graphics[index1] graphic2 = display_item.graphics[index2] if data_item: if data_item.is_datum_1d and isinstance(graphic1, Graphics.IntervalGraphic) and isinstance(graphic2, Graphics.IntervalGraphic): crop_graphic1 = graphic1 crop_graphic2 = graphic2 elif data_item.is_datum_2d and isinstance(graphic1, Graphics.RectangleTypeGraphic) and isinstance(graphic2, Graphics.RectangleTypeGraphic): crop_graphic1 = graphic1 crop_graphic2 = graphic2 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 else: crop_graphic1 = self.__get_crop_graphic(display_item) crop_graphic2 = crop_graphic1 return (display_item, crop_graphic1), (display_item, crop_graphic2) if len(selected_display_items) == 2: display_item1 = selected_display_items[0] crop_graphic1 = self.__get_crop_graphic(display_item1) display_item2 = selected_display_items[1] crop_graphic2 = self.__get_crop_graphic(display_item2) return (display_item1, crop_graphic1), (display_item2, crop_graphic2) return None
[ "def", "_get_two_data_sources", "(", "self", ")", ":", "selected_display_items", "=", "self", ".", "selected_display_items", "if", "len", "(", "selected_display_items", ")", "<", "2", ":", "selected_display_items", "=", "list", "(", ")", "display_item", "=", "self", ".", "selected_display_item", "if", "display_item", ":", "selected_display_items", ".", "append", "(", "display_item", ")", "if", "len", "(", "selected_display_items", ")", "==", "1", ":", "display_item", "=", "selected_display_items", "[", "0", "]", "data_item", "=", "display_item", ".", "data_item", "if", "display_item", "else", "None", "if", "display_item", "and", "len", "(", "display_item", ".", "graphic_selection", ".", "indexes", ")", "==", "2", ":", "index1", "=", "display_item", ".", "graphic_selection", ".", "anchor_index", "index2", "=", "list", "(", "display_item", ".", "graphic_selection", ".", "indexes", ".", "difference", "(", "{", "index1", "}", ")", ")", "[", "0", "]", "graphic1", "=", "display_item", ".", "graphics", "[", "index1", "]", "graphic2", "=", "display_item", ".", "graphics", "[", "index2", "]", "if", "data_item", ":", "if", "data_item", ".", "is_datum_1d", "and", "isinstance", "(", "graphic1", ",", "Graphics", ".", "IntervalGraphic", ")", "and", "isinstance", "(", "graphic2", ",", "Graphics", ".", "IntervalGraphic", ")", ":", "crop_graphic1", "=", "graphic1", "crop_graphic2", "=", "graphic2", "elif", "data_item", ".", "is_datum_2d", "and", "isinstance", "(", "graphic1", ",", "Graphics", ".", "RectangleTypeGraphic", ")", "and", "isinstance", "(", "graphic2", ",", "Graphics", ".", "RectangleTypeGraphic", ")", ":", "crop_graphic1", "=", "graphic1", "crop_graphic2", "=", "graphic2", "else", ":", "crop_graphic1", "=", "self", ".", "__get_crop_graphic", "(", "display_item", ")", "crop_graphic2", "=", "crop_graphic1", "else", ":", "crop_graphic1", "=", "self", ".", "__get_crop_graphic", "(", "display_item", ")", "crop_graphic2", "=", "crop_graphic1", "else", ":", "crop_graphic1", "=", "self", ".", "__get_crop_graphic", "(", "display_item", ")", "crop_graphic2", "=", "crop_graphic1", "return", "(", "display_item", ",", "crop_graphic1", ")", ",", "(", "display_item", ",", "crop_graphic2", ")", "if", "len", "(", "selected_display_items", ")", "==", "2", ":", "display_item1", "=", "selected_display_items", "[", "0", "]", "crop_graphic1", "=", "self", ".", "__get_crop_graphic", "(", "display_item1", ")", "display_item2", "=", "selected_display_items", "[", "1", "]", "crop_graphic2", "=", "self", ".", "__get_crop_graphic", "(", "display_item2", ")", "return", "(", "display_item1", ",", "crop_graphic1", ")", ",", "(", "display_item2", ",", "crop_graphic2", ")", "return", "None" ]
Get two sensible data sources, which may be the same.
[ "Get", "two", "sensible", "data", "sources", "which", "may", "be", "the", "same", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/DocumentController.py#L2053-L2092
train
nion-software/nionswift
nion/swift/ImageCanvasItem.py
calculate_origin_and_size
def calculate_origin_and_size(canvas_size, data_shape, image_canvas_mode, image_zoom, image_position) -> typing.Tuple[typing.Any, typing.Any]: """Calculate origin and size for canvas size, data shape, and image display parameters.""" if data_shape is None: return None, None if image_canvas_mode == "fill": data_shape = data_shape scale_h = float(data_shape[1]) / canvas_size[1] scale_v = float(data_shape[0]) / canvas_size[0] if scale_v < scale_h: image_canvas_size = (canvas_size[0], canvas_size[0] * data_shape[1] / data_shape[0]) else: image_canvas_size = (canvas_size[1] * data_shape[0] / data_shape[1], canvas_size[1]) image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) elif image_canvas_mode == "fit": image_canvas_size = canvas_size image_canvas_origin = (0, 0) elif image_canvas_mode == "1:1": image_canvas_size = data_shape image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) elif image_canvas_mode == "2:1": image_canvas_size = (data_shape[0] * 0.5, data_shape[1] * 0.5) image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) else: image_canvas_size = (canvas_size[0] * image_zoom, canvas_size[1] * image_zoom) canvas_rect = Geometry.fit_to_size(((0, 0), image_canvas_size), data_shape) image_canvas_origin_y = (canvas_size[0] * 0.5) - image_position[0] * canvas_rect[1][0] - canvas_rect[0][0] image_canvas_origin_x = (canvas_size[1] * 0.5) - image_position[1] * canvas_rect[1][1] - canvas_rect[0][1] image_canvas_origin = (image_canvas_origin_y, image_canvas_origin_x) return image_canvas_origin, image_canvas_size
python
def calculate_origin_and_size(canvas_size, data_shape, image_canvas_mode, image_zoom, image_position) -> typing.Tuple[typing.Any, typing.Any]: """Calculate origin and size for canvas size, data shape, and image display parameters.""" if data_shape is None: return None, None if image_canvas_mode == "fill": data_shape = data_shape scale_h = float(data_shape[1]) / canvas_size[1] scale_v = float(data_shape[0]) / canvas_size[0] if scale_v < scale_h: image_canvas_size = (canvas_size[0], canvas_size[0] * data_shape[1] / data_shape[0]) else: image_canvas_size = (canvas_size[1] * data_shape[0] / data_shape[1], canvas_size[1]) image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) elif image_canvas_mode == "fit": image_canvas_size = canvas_size image_canvas_origin = (0, 0) elif image_canvas_mode == "1:1": image_canvas_size = data_shape image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) elif image_canvas_mode == "2:1": image_canvas_size = (data_shape[0] * 0.5, data_shape[1] * 0.5) image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5) else: image_canvas_size = (canvas_size[0] * image_zoom, canvas_size[1] * image_zoom) canvas_rect = Geometry.fit_to_size(((0, 0), image_canvas_size), data_shape) image_canvas_origin_y = (canvas_size[0] * 0.5) - image_position[0] * canvas_rect[1][0] - canvas_rect[0][0] image_canvas_origin_x = (canvas_size[1] * 0.5) - image_position[1] * canvas_rect[1][1] - canvas_rect[0][1] image_canvas_origin = (image_canvas_origin_y, image_canvas_origin_x) return image_canvas_origin, image_canvas_size
[ "def", "calculate_origin_and_size", "(", "canvas_size", ",", "data_shape", ",", "image_canvas_mode", ",", "image_zoom", ",", "image_position", ")", "->", "typing", ".", "Tuple", "[", "typing", ".", "Any", ",", "typing", ".", "Any", "]", ":", "if", "data_shape", "is", "None", ":", "return", "None", ",", "None", "if", "image_canvas_mode", "==", "\"fill\"", ":", "data_shape", "=", "data_shape", "scale_h", "=", "float", "(", "data_shape", "[", "1", "]", ")", "/", "canvas_size", "[", "1", "]", "scale_v", "=", "float", "(", "data_shape", "[", "0", "]", ")", "/", "canvas_size", "[", "0", "]", "if", "scale_v", "<", "scale_h", ":", "image_canvas_size", "=", "(", "canvas_size", "[", "0", "]", ",", "canvas_size", "[", "0", "]", "*", "data_shape", "[", "1", "]", "/", "data_shape", "[", "0", "]", ")", "else", ":", "image_canvas_size", "=", "(", "canvas_size", "[", "1", "]", "*", "data_shape", "[", "0", "]", "/", "data_shape", "[", "1", "]", ",", "canvas_size", "[", "1", "]", ")", "image_canvas_origin", "=", "(", "canvas_size", "[", "0", "]", "*", "0.5", "-", "image_canvas_size", "[", "0", "]", "*", "0.5", ",", "canvas_size", "[", "1", "]", "*", "0.5", "-", "image_canvas_size", "[", "1", "]", "*", "0.5", ")", "elif", "image_canvas_mode", "==", "\"fit\"", ":", "image_canvas_size", "=", "canvas_size", "image_canvas_origin", "=", "(", "0", ",", "0", ")", "elif", "image_canvas_mode", "==", "\"1:1\"", ":", "image_canvas_size", "=", "data_shape", "image_canvas_origin", "=", "(", "canvas_size", "[", "0", "]", "*", "0.5", "-", "image_canvas_size", "[", "0", "]", "*", "0.5", ",", "canvas_size", "[", "1", "]", "*", "0.5", "-", "image_canvas_size", "[", "1", "]", "*", "0.5", ")", "elif", "image_canvas_mode", "==", "\"2:1\"", ":", "image_canvas_size", "=", "(", "data_shape", "[", "0", "]", "*", "0.5", ",", "data_shape", "[", "1", "]", "*", "0.5", ")", "image_canvas_origin", "=", "(", "canvas_size", "[", "0", "]", "*", "0.5", "-", "image_canvas_size", "[", "0", "]", "*", "0.5", ",", "canvas_size", "[", "1", "]", "*", "0.5", "-", "image_canvas_size", "[", "1", "]", "*", "0.5", ")", "else", ":", "image_canvas_size", "=", "(", "canvas_size", "[", "0", "]", "*", "image_zoom", ",", "canvas_size", "[", "1", "]", "*", "image_zoom", ")", "canvas_rect", "=", "Geometry", ".", "fit_to_size", "(", "(", "(", "0", ",", "0", ")", ",", "image_canvas_size", ")", ",", "data_shape", ")", "image_canvas_origin_y", "=", "(", "canvas_size", "[", "0", "]", "*", "0.5", ")", "-", "image_position", "[", "0", "]", "*", "canvas_rect", "[", "1", "]", "[", "0", "]", "-", "canvas_rect", "[", "0", "]", "[", "0", "]", "image_canvas_origin_x", "=", "(", "canvas_size", "[", "1", "]", "*", "0.5", ")", "-", "image_position", "[", "1", "]", "*", "canvas_rect", "[", "1", "]", "[", "1", "]", "-", "canvas_rect", "[", "0", "]", "[", "1", "]", "image_canvas_origin", "=", "(", "image_canvas_origin_y", ",", "image_canvas_origin_x", ")", "return", "image_canvas_origin", ",", "image_canvas_size" ]
Calculate origin and size for canvas size, data shape, and image display parameters.
[ "Calculate", "origin", "and", "size", "for", "canvas", "size", "data", "shape", "and", "image", "display", "parameters", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/ImageCanvasItem.py#L332-L360
train
nion-software/nionswift
nion/swift/model/FileStorageSystem.py
read_library
def read_library(persistent_storage_system, ignore_older_files) -> typing.Dict: """Read data items from the data reference handler and return as a list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items. """ data_item_uuids = set() utilized_deletions = set() # the uuid's skipped due to being deleted deletions = list() reader_info_list, library_updates = auto_migrate_storage_system(persistent_storage_system=persistent_storage_system, new_persistent_storage_system=persistent_storage_system, data_item_uuids=data_item_uuids, deletions=deletions, utilized_deletions=utilized_deletions, ignore_older_files=ignore_older_files) # next, for each auto migration, create a temporary storage system and read items from that storage system # using auto_migrate_storage_system. the data items returned will have been copied to the current storage # system (persistent object context). for auto_migration in reversed(persistent_storage_system.get_auto_migrations()): old_persistent_storage_system = FileStorageSystem(auto_migration.library_path, auto_migration.paths) if auto_migration.paths else auto_migration.storage_system new_reader_info_list, new_library_updates = auto_migrate_storage_system(persistent_storage_system=old_persistent_storage_system, new_persistent_storage_system=persistent_storage_system, data_item_uuids=data_item_uuids, deletions=deletions, utilized_deletions=utilized_deletions, ignore_older_files=ignore_older_files) reader_info_list.extend(new_reader_info_list) library_updates.update(new_library_updates) assert len(reader_info_list) == len(data_item_uuids) library_storage_properties = persistent_storage_system.library_storage_properties for reader_info in reader_info_list: properties = reader_info.properties properties = Utility.clean_dict(copy.deepcopy(properties) if properties else dict()) version = properties.get("version", 0) if version == DataItem.DataItem.writer_version: data_item_uuid = uuid.UUID(properties.get("uuid", uuid.uuid4())) library_update = library_updates.get(data_item_uuid, dict()) library_storage_properties.setdefault("connections", list()).extend(library_update.get("connections", list())) library_storage_properties.setdefault("computations", list()).extend(library_update.get("computations", list())) library_storage_properties.setdefault("display_items", list()).extend(library_update.get("display_items", list())) # mark deletions that need to be tracked because they've been deleted but are also present in older libraries # and would be migrated during reading unless they explicitly are prevented from doing so (via data_item_deletions). # utilized deletions are the ones that were attempted; if nothing was attempted, then no reason to track it anymore # since there is nothing to migrate in the future. library_storage_properties["data_item_deletions"] = [str(uuid_) for uuid_ in utilized_deletions] connections_list = library_storage_properties.get("connections", list()) assert len(connections_list) == len({connection.get("uuid") for connection in connections_list}) computations_list = library_storage_properties.get("computations", list()) assert len(computations_list) == len({computation.get("uuid") for computation in computations_list}) # migrations if library_storage_properties.get("version", 0) < 2: for data_group_properties in library_storage_properties.get("data_groups", list()): data_group_properties.pop("data_groups") display_item_references = data_group_properties.setdefault("display_item_references", list()) data_item_uuid_strs = data_group_properties.pop("data_item_uuids", list()) for data_item_uuid_str in data_item_uuid_strs: for display_item_properties in library_storage_properties.get("display_items", list()): data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())] if data_item_uuid_str in data_item_references: display_item_references.append(display_item_properties["uuid"]) data_item_uuid_to_display_item_uuid_map = dict() data_item_uuid_to_display_item_dict_map = dict() display_to_display_item_map = dict() display_to_display_data_channel_map = dict() for display_item_properties in library_storage_properties.get("display_items", list()): display_to_display_item_map[display_item_properties["display"]["uuid"]] = display_item_properties["uuid"] display_to_display_data_channel_map[display_item_properties["display"]["uuid"]] = display_item_properties["display_data_channels"][0]["uuid"] data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())] for data_item_uuid_str in data_item_references: data_item_uuid_to_display_item_uuid_map.setdefault(data_item_uuid_str, display_item_properties["uuid"]) data_item_uuid_to_display_item_dict_map.setdefault(data_item_uuid_str, display_item_properties) display_item_properties.pop("display", None) for workspace_properties in library_storage_properties.get("workspaces", list()): def replace1(d): if "children" in d: for dd in d["children"]: replace1(dd) if "data_item_uuid" in d: data_item_uuid_str = d.pop("data_item_uuid") display_item_uuid_str = data_item_uuid_to_display_item_uuid_map.get(data_item_uuid_str) if display_item_uuid_str: d["display_item_uuid"] = display_item_uuid_str replace1(workspace_properties["layout"]) for connection_dict in library_storage_properties.get("connections", list()): source_uuid_str = connection_dict["source_uuid"] if connection_dict["type"] == "interval-list-connection": connection_dict["source_uuid"] = display_to_display_item_map.get(source_uuid_str, None) if connection_dict["type"] == "property-connection" and connection_dict["source_property"] == "slice_interval": connection_dict["source_uuid"] = display_to_display_data_channel_map.get(source_uuid_str, None) def fix_specifier(specifier_dict): if specifier_dict.get("type") in ("data_item", "display_xdata", "cropped_xdata", "cropped_display_xdata", "filter_xdata", "filtered_xdata"): if specifier_dict.get("uuid") in data_item_uuid_to_display_item_dict_map: specifier_dict["uuid"] = data_item_uuid_to_display_item_dict_map[specifier_dict["uuid"]]["display_data_channels"][0]["uuid"] else: specifier_dict.pop("uuid", None) if specifier_dict.get("type") == "data_item": specifier_dict["type"] = "data_source" if specifier_dict.get("type") == "data_item_object": specifier_dict["type"] = "data_item" if specifier_dict.get("type") == "region": specifier_dict["type"] = "graphic" for computation_dict in library_storage_properties.get("computations", list()): for variable_dict in computation_dict.get("variables", list()): if "specifier" in variable_dict: specifier_dict = variable_dict["specifier"] if specifier_dict is not None: fix_specifier(specifier_dict) if "secondary_specifier" in variable_dict: specifier_dict = variable_dict["secondary_specifier"] if specifier_dict is not None: fix_specifier(specifier_dict) for result_dict in computation_dict.get("results", list()): fix_specifier(result_dict["specifier"]) library_storage_properties["version"] = DocumentModel.DocumentModel.library_version # TODO: add consistency checks: no duplicated items [by uuid] such as connections or computations or data items assert library_storage_properties["version"] == DocumentModel.DocumentModel.library_version persistent_storage_system.rewrite_properties(library_storage_properties) properties = copy.deepcopy(library_storage_properties) for reader_info in reader_info_list: data_item_properties = Utility.clean_dict(reader_info.properties if reader_info.properties else dict()) if data_item_properties.get("version", 0) == DataItem.DataItem.writer_version: data_item_properties["__large_format"] = reader_info.large_format data_item_properties["__identifier"] = reader_info.identifier properties.setdefault("data_items", list()).append(data_item_properties) def data_item_created(data_item_properties: typing.Mapping) -> str: return data_item_properties.get("created", "1900-01-01T00:00:00.000000") properties["data_items"] = sorted(properties.get("data_items", list()), key=data_item_created) return properties
python
def read_library(persistent_storage_system, ignore_older_files) -> typing.Dict: """Read data items from the data reference handler and return as a list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items. """ data_item_uuids = set() utilized_deletions = set() # the uuid's skipped due to being deleted deletions = list() reader_info_list, library_updates = auto_migrate_storage_system(persistent_storage_system=persistent_storage_system, new_persistent_storage_system=persistent_storage_system, data_item_uuids=data_item_uuids, deletions=deletions, utilized_deletions=utilized_deletions, ignore_older_files=ignore_older_files) # next, for each auto migration, create a temporary storage system and read items from that storage system # using auto_migrate_storage_system. the data items returned will have been copied to the current storage # system (persistent object context). for auto_migration in reversed(persistent_storage_system.get_auto_migrations()): old_persistent_storage_system = FileStorageSystem(auto_migration.library_path, auto_migration.paths) if auto_migration.paths else auto_migration.storage_system new_reader_info_list, new_library_updates = auto_migrate_storage_system(persistent_storage_system=old_persistent_storage_system, new_persistent_storage_system=persistent_storage_system, data_item_uuids=data_item_uuids, deletions=deletions, utilized_deletions=utilized_deletions, ignore_older_files=ignore_older_files) reader_info_list.extend(new_reader_info_list) library_updates.update(new_library_updates) assert len(reader_info_list) == len(data_item_uuids) library_storage_properties = persistent_storage_system.library_storage_properties for reader_info in reader_info_list: properties = reader_info.properties properties = Utility.clean_dict(copy.deepcopy(properties) if properties else dict()) version = properties.get("version", 0) if version == DataItem.DataItem.writer_version: data_item_uuid = uuid.UUID(properties.get("uuid", uuid.uuid4())) library_update = library_updates.get(data_item_uuid, dict()) library_storage_properties.setdefault("connections", list()).extend(library_update.get("connections", list())) library_storage_properties.setdefault("computations", list()).extend(library_update.get("computations", list())) library_storage_properties.setdefault("display_items", list()).extend(library_update.get("display_items", list())) # mark deletions that need to be tracked because they've been deleted but are also present in older libraries # and would be migrated during reading unless they explicitly are prevented from doing so (via data_item_deletions). # utilized deletions are the ones that were attempted; if nothing was attempted, then no reason to track it anymore # since there is nothing to migrate in the future. library_storage_properties["data_item_deletions"] = [str(uuid_) for uuid_ in utilized_deletions] connections_list = library_storage_properties.get("connections", list()) assert len(connections_list) == len({connection.get("uuid") for connection in connections_list}) computations_list = library_storage_properties.get("computations", list()) assert len(computations_list) == len({computation.get("uuid") for computation in computations_list}) # migrations if library_storage_properties.get("version", 0) < 2: for data_group_properties in library_storage_properties.get("data_groups", list()): data_group_properties.pop("data_groups") display_item_references = data_group_properties.setdefault("display_item_references", list()) data_item_uuid_strs = data_group_properties.pop("data_item_uuids", list()) for data_item_uuid_str in data_item_uuid_strs: for display_item_properties in library_storage_properties.get("display_items", list()): data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())] if data_item_uuid_str in data_item_references: display_item_references.append(display_item_properties["uuid"]) data_item_uuid_to_display_item_uuid_map = dict() data_item_uuid_to_display_item_dict_map = dict() display_to_display_item_map = dict() display_to_display_data_channel_map = dict() for display_item_properties in library_storage_properties.get("display_items", list()): display_to_display_item_map[display_item_properties["display"]["uuid"]] = display_item_properties["uuid"] display_to_display_data_channel_map[display_item_properties["display"]["uuid"]] = display_item_properties["display_data_channels"][0]["uuid"] data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())] for data_item_uuid_str in data_item_references: data_item_uuid_to_display_item_uuid_map.setdefault(data_item_uuid_str, display_item_properties["uuid"]) data_item_uuid_to_display_item_dict_map.setdefault(data_item_uuid_str, display_item_properties) display_item_properties.pop("display", None) for workspace_properties in library_storage_properties.get("workspaces", list()): def replace1(d): if "children" in d: for dd in d["children"]: replace1(dd) if "data_item_uuid" in d: data_item_uuid_str = d.pop("data_item_uuid") display_item_uuid_str = data_item_uuid_to_display_item_uuid_map.get(data_item_uuid_str) if display_item_uuid_str: d["display_item_uuid"] = display_item_uuid_str replace1(workspace_properties["layout"]) for connection_dict in library_storage_properties.get("connections", list()): source_uuid_str = connection_dict["source_uuid"] if connection_dict["type"] == "interval-list-connection": connection_dict["source_uuid"] = display_to_display_item_map.get(source_uuid_str, None) if connection_dict["type"] == "property-connection" and connection_dict["source_property"] == "slice_interval": connection_dict["source_uuid"] = display_to_display_data_channel_map.get(source_uuid_str, None) def fix_specifier(specifier_dict): if specifier_dict.get("type") in ("data_item", "display_xdata", "cropped_xdata", "cropped_display_xdata", "filter_xdata", "filtered_xdata"): if specifier_dict.get("uuid") in data_item_uuid_to_display_item_dict_map: specifier_dict["uuid"] = data_item_uuid_to_display_item_dict_map[specifier_dict["uuid"]]["display_data_channels"][0]["uuid"] else: specifier_dict.pop("uuid", None) if specifier_dict.get("type") == "data_item": specifier_dict["type"] = "data_source" if specifier_dict.get("type") == "data_item_object": specifier_dict["type"] = "data_item" if specifier_dict.get("type") == "region": specifier_dict["type"] = "graphic" for computation_dict in library_storage_properties.get("computations", list()): for variable_dict in computation_dict.get("variables", list()): if "specifier" in variable_dict: specifier_dict = variable_dict["specifier"] if specifier_dict is not None: fix_specifier(specifier_dict) if "secondary_specifier" in variable_dict: specifier_dict = variable_dict["secondary_specifier"] if specifier_dict is not None: fix_specifier(specifier_dict) for result_dict in computation_dict.get("results", list()): fix_specifier(result_dict["specifier"]) library_storage_properties["version"] = DocumentModel.DocumentModel.library_version # TODO: add consistency checks: no duplicated items [by uuid] such as connections or computations or data items assert library_storage_properties["version"] == DocumentModel.DocumentModel.library_version persistent_storage_system.rewrite_properties(library_storage_properties) properties = copy.deepcopy(library_storage_properties) for reader_info in reader_info_list: data_item_properties = Utility.clean_dict(reader_info.properties if reader_info.properties else dict()) if data_item_properties.get("version", 0) == DataItem.DataItem.writer_version: data_item_properties["__large_format"] = reader_info.large_format data_item_properties["__identifier"] = reader_info.identifier properties.setdefault("data_items", list()).append(data_item_properties) def data_item_created(data_item_properties: typing.Mapping) -> str: return data_item_properties.get("created", "1900-01-01T00:00:00.000000") properties["data_items"] = sorted(properties.get("data_items", list()), key=data_item_created) return properties
[ "def", "read_library", "(", "persistent_storage_system", ",", "ignore_older_files", ")", "->", "typing", ".", "Dict", ":", "data_item_uuids", "=", "set", "(", ")", "utilized_deletions", "=", "set", "(", ")", "# the uuid's skipped due to being deleted", "deletions", "=", "list", "(", ")", "reader_info_list", ",", "library_updates", "=", "auto_migrate_storage_system", "(", "persistent_storage_system", "=", "persistent_storage_system", ",", "new_persistent_storage_system", "=", "persistent_storage_system", ",", "data_item_uuids", "=", "data_item_uuids", ",", "deletions", "=", "deletions", ",", "utilized_deletions", "=", "utilized_deletions", ",", "ignore_older_files", "=", "ignore_older_files", ")", "# next, for each auto migration, create a temporary storage system and read items from that storage system", "# using auto_migrate_storage_system. the data items returned will have been copied to the current storage", "# system (persistent object context).", "for", "auto_migration", "in", "reversed", "(", "persistent_storage_system", ".", "get_auto_migrations", "(", ")", ")", ":", "old_persistent_storage_system", "=", "FileStorageSystem", "(", "auto_migration", ".", "library_path", ",", "auto_migration", ".", "paths", ")", "if", "auto_migration", ".", "paths", "else", "auto_migration", ".", "storage_system", "new_reader_info_list", ",", "new_library_updates", "=", "auto_migrate_storage_system", "(", "persistent_storage_system", "=", "old_persistent_storage_system", ",", "new_persistent_storage_system", "=", "persistent_storage_system", ",", "data_item_uuids", "=", "data_item_uuids", ",", "deletions", "=", "deletions", ",", "utilized_deletions", "=", "utilized_deletions", ",", "ignore_older_files", "=", "ignore_older_files", ")", "reader_info_list", ".", "extend", "(", "new_reader_info_list", ")", "library_updates", ".", "update", "(", "new_library_updates", ")", "assert", "len", "(", "reader_info_list", ")", "==", "len", "(", "data_item_uuids", ")", "library_storage_properties", "=", "persistent_storage_system", ".", "library_storage_properties", "for", "reader_info", "in", "reader_info_list", ":", "properties", "=", "reader_info", ".", "properties", "properties", "=", "Utility", ".", "clean_dict", "(", "copy", ".", "deepcopy", "(", "properties", ")", "if", "properties", "else", "dict", "(", ")", ")", "version", "=", "properties", ".", "get", "(", "\"version\"", ",", "0", ")", "if", "version", "==", "DataItem", ".", "DataItem", ".", "writer_version", ":", "data_item_uuid", "=", "uuid", ".", "UUID", "(", "properties", ".", "get", "(", "\"uuid\"", ",", "uuid", ".", "uuid4", "(", ")", ")", ")", "library_update", "=", "library_updates", ".", "get", "(", "data_item_uuid", ",", "dict", "(", ")", ")", "library_storage_properties", ".", "setdefault", "(", "\"connections\"", ",", "list", "(", ")", ")", ".", "extend", "(", "library_update", ".", "get", "(", "\"connections\"", ",", "list", "(", ")", ")", ")", "library_storage_properties", ".", "setdefault", "(", "\"computations\"", ",", "list", "(", ")", ")", ".", "extend", "(", "library_update", ".", "get", "(", "\"computations\"", ",", "list", "(", ")", ")", ")", "library_storage_properties", ".", "setdefault", "(", "\"display_items\"", ",", "list", "(", ")", ")", ".", "extend", "(", "library_update", ".", "get", "(", "\"display_items\"", ",", "list", "(", ")", ")", ")", "# mark deletions that need to be tracked because they've been deleted but are also present in older libraries", "# and would be migrated during reading unless they explicitly are prevented from doing so (via data_item_deletions).", "# utilized deletions are the ones that were attempted; if nothing was attempted, then no reason to track it anymore", "# since there is nothing to migrate in the future.", "library_storage_properties", "[", "\"data_item_deletions\"", "]", "=", "[", "str", "(", "uuid_", ")", "for", "uuid_", "in", "utilized_deletions", "]", "connections_list", "=", "library_storage_properties", ".", "get", "(", "\"connections\"", ",", "list", "(", ")", ")", "assert", "len", "(", "connections_list", ")", "==", "len", "(", "{", "connection", ".", "get", "(", "\"uuid\"", ")", "for", "connection", "in", "connections_list", "}", ")", "computations_list", "=", "library_storage_properties", ".", "get", "(", "\"computations\"", ",", "list", "(", ")", ")", "assert", "len", "(", "computations_list", ")", "==", "len", "(", "{", "computation", ".", "get", "(", "\"uuid\"", ")", "for", "computation", "in", "computations_list", "}", ")", "# migrations", "if", "library_storage_properties", ".", "get", "(", "\"version\"", ",", "0", ")", "<", "2", ":", "for", "data_group_properties", "in", "library_storage_properties", ".", "get", "(", "\"data_groups\"", ",", "list", "(", ")", ")", ":", "data_group_properties", ".", "pop", "(", "\"data_groups\"", ")", "display_item_references", "=", "data_group_properties", ".", "setdefault", "(", "\"display_item_references\"", ",", "list", "(", ")", ")", "data_item_uuid_strs", "=", "data_group_properties", ".", "pop", "(", "\"data_item_uuids\"", ",", "list", "(", ")", ")", "for", "data_item_uuid_str", "in", "data_item_uuid_strs", ":", "for", "display_item_properties", "in", "library_storage_properties", ".", "get", "(", "\"display_items\"", ",", "list", "(", ")", ")", ":", "data_item_references", "=", "[", "d", ".", "get", "(", "\"data_item_reference\"", ",", "None", ")", "for", "d", "in", "display_item_properties", ".", "get", "(", "\"display_data_channels\"", ",", "list", "(", ")", ")", "]", "if", "data_item_uuid_str", "in", "data_item_references", ":", "display_item_references", ".", "append", "(", "display_item_properties", "[", "\"uuid\"", "]", ")", "data_item_uuid_to_display_item_uuid_map", "=", "dict", "(", ")", "data_item_uuid_to_display_item_dict_map", "=", "dict", "(", ")", "display_to_display_item_map", "=", "dict", "(", ")", "display_to_display_data_channel_map", "=", "dict", "(", ")", "for", "display_item_properties", "in", "library_storage_properties", ".", "get", "(", "\"display_items\"", ",", "list", "(", ")", ")", ":", "display_to_display_item_map", "[", "display_item_properties", "[", "\"display\"", "]", "[", "\"uuid\"", "]", "]", "=", "display_item_properties", "[", "\"uuid\"", "]", "display_to_display_data_channel_map", "[", "display_item_properties", "[", "\"display\"", "]", "[", "\"uuid\"", "]", "]", "=", "display_item_properties", "[", "\"display_data_channels\"", "]", "[", "0", "]", "[", "\"uuid\"", "]", "data_item_references", "=", "[", "d", ".", "get", "(", "\"data_item_reference\"", ",", "None", ")", "for", "d", "in", "display_item_properties", ".", "get", "(", "\"display_data_channels\"", ",", "list", "(", ")", ")", "]", "for", "data_item_uuid_str", "in", "data_item_references", ":", "data_item_uuid_to_display_item_uuid_map", ".", "setdefault", "(", "data_item_uuid_str", ",", "display_item_properties", "[", "\"uuid\"", "]", ")", "data_item_uuid_to_display_item_dict_map", ".", "setdefault", "(", "data_item_uuid_str", ",", "display_item_properties", ")", "display_item_properties", ".", "pop", "(", "\"display\"", ",", "None", ")", "for", "workspace_properties", "in", "library_storage_properties", ".", "get", "(", "\"workspaces\"", ",", "list", "(", ")", ")", ":", "def", "replace1", "(", "d", ")", ":", "if", "\"children\"", "in", "d", ":", "for", "dd", "in", "d", "[", "\"children\"", "]", ":", "replace1", "(", "dd", ")", "if", "\"data_item_uuid\"", "in", "d", ":", "data_item_uuid_str", "=", "d", ".", "pop", "(", "\"data_item_uuid\"", ")", "display_item_uuid_str", "=", "data_item_uuid_to_display_item_uuid_map", ".", "get", "(", "data_item_uuid_str", ")", "if", "display_item_uuid_str", ":", "d", "[", "\"display_item_uuid\"", "]", "=", "display_item_uuid_str", "replace1", "(", "workspace_properties", "[", "\"layout\"", "]", ")", "for", "connection_dict", "in", "library_storage_properties", ".", "get", "(", "\"connections\"", ",", "list", "(", ")", ")", ":", "source_uuid_str", "=", "connection_dict", "[", "\"source_uuid\"", "]", "if", "connection_dict", "[", "\"type\"", "]", "==", "\"interval-list-connection\"", ":", "connection_dict", "[", "\"source_uuid\"", "]", "=", "display_to_display_item_map", ".", "get", "(", "source_uuid_str", ",", "None", ")", "if", "connection_dict", "[", "\"type\"", "]", "==", "\"property-connection\"", "and", "connection_dict", "[", "\"source_property\"", "]", "==", "\"slice_interval\"", ":", "connection_dict", "[", "\"source_uuid\"", "]", "=", "display_to_display_data_channel_map", ".", "get", "(", "source_uuid_str", ",", "None", ")", "def", "fix_specifier", "(", "specifier_dict", ")", ":", "if", "specifier_dict", ".", "get", "(", "\"type\"", ")", "in", "(", "\"data_item\"", ",", "\"display_xdata\"", ",", "\"cropped_xdata\"", ",", "\"cropped_display_xdata\"", ",", "\"filter_xdata\"", ",", "\"filtered_xdata\"", ")", ":", "if", "specifier_dict", ".", "get", "(", "\"uuid\"", ")", "in", "data_item_uuid_to_display_item_dict_map", ":", "specifier_dict", "[", "\"uuid\"", "]", "=", "data_item_uuid_to_display_item_dict_map", "[", "specifier_dict", "[", "\"uuid\"", "]", "]", "[", "\"display_data_channels\"", "]", "[", "0", "]", "[", "\"uuid\"", "]", "else", ":", "specifier_dict", ".", "pop", "(", "\"uuid\"", ",", "None", ")", "if", "specifier_dict", ".", "get", "(", "\"type\"", ")", "==", "\"data_item\"", ":", "specifier_dict", "[", "\"type\"", "]", "=", "\"data_source\"", "if", "specifier_dict", ".", "get", "(", "\"type\"", ")", "==", "\"data_item_object\"", ":", "specifier_dict", "[", "\"type\"", "]", "=", "\"data_item\"", "if", "specifier_dict", ".", "get", "(", "\"type\"", ")", "==", "\"region\"", ":", "specifier_dict", "[", "\"type\"", "]", "=", "\"graphic\"", "for", "computation_dict", "in", "library_storage_properties", ".", "get", "(", "\"computations\"", ",", "list", "(", ")", ")", ":", "for", "variable_dict", "in", "computation_dict", ".", "get", "(", "\"variables\"", ",", "list", "(", ")", ")", ":", "if", "\"specifier\"", "in", "variable_dict", ":", "specifier_dict", "=", "variable_dict", "[", "\"specifier\"", "]", "if", "specifier_dict", "is", "not", "None", ":", "fix_specifier", "(", "specifier_dict", ")", "if", "\"secondary_specifier\"", "in", "variable_dict", ":", "specifier_dict", "=", "variable_dict", "[", "\"secondary_specifier\"", "]", "if", "specifier_dict", "is", "not", "None", ":", "fix_specifier", "(", "specifier_dict", ")", "for", "result_dict", "in", "computation_dict", ".", "get", "(", "\"results\"", ",", "list", "(", ")", ")", ":", "fix_specifier", "(", "result_dict", "[", "\"specifier\"", "]", ")", "library_storage_properties", "[", "\"version\"", "]", "=", "DocumentModel", ".", "DocumentModel", ".", "library_version", "# TODO: add consistency checks: no duplicated items [by uuid] such as connections or computations or data items", "assert", "library_storage_properties", "[", "\"version\"", "]", "==", "DocumentModel", ".", "DocumentModel", ".", "library_version", "persistent_storage_system", ".", "rewrite_properties", "(", "library_storage_properties", ")", "properties", "=", "copy", ".", "deepcopy", "(", "library_storage_properties", ")", "for", "reader_info", "in", "reader_info_list", ":", "data_item_properties", "=", "Utility", ".", "clean_dict", "(", "reader_info", ".", "properties", "if", "reader_info", ".", "properties", "else", "dict", "(", ")", ")", "if", "data_item_properties", ".", "get", "(", "\"version\"", ",", "0", ")", "==", "DataItem", ".", "DataItem", ".", "writer_version", ":", "data_item_properties", "[", "\"__large_format\"", "]", "=", "reader_info", ".", "large_format", "data_item_properties", "[", "\"__identifier\"", "]", "=", "reader_info", ".", "identifier", "properties", ".", "setdefault", "(", "\"data_items\"", ",", "list", "(", ")", ")", ".", "append", "(", "data_item_properties", ")", "def", "data_item_created", "(", "data_item_properties", ":", "typing", ".", "Mapping", ")", "->", "str", ":", "return", "data_item_properties", ".", "get", "(", "\"created\"", ",", "\"1900-01-01T00:00:00.000000\"", ")", "properties", "[", "\"data_items\"", "]", "=", "sorted", "(", "properties", ".", "get", "(", "\"data_items\"", ",", "list", "(", ")", ")", ",", "key", "=", "data_item_created", ")", "return", "properties" ]
Read data items from the data reference handler and return as a list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items.
[ "Read", "data", "items", "from", "the", "data", "reference", "handler", "and", "return", "as", "a", "list", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/FileStorageSystem.py#L419-L567
train
nion-software/nionswift
nion/swift/model/FileStorageSystem.py
auto_migrate_storage_system
def auto_migrate_storage_system(*, persistent_storage_system=None, new_persistent_storage_system=None, data_item_uuids=None, deletions: typing.List[uuid.UUID] = None, utilized_deletions: typing.Set[uuid.UUID] = None, ignore_older_files: bool = True): """Migrate items from the storage system to the object context. Files in data_item_uuids have already been loaded and are ignored (not migrated). Files in deletes have been deleted in object context and are ignored (not migrated) and then added to the utilized deletions list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items. """ storage_handlers = persistent_storage_system.find_data_items() ReaderInfo = collections.namedtuple("ReaderInfo", ["properties", "changed_ref", "large_format", "storage_handler", "identifier"]) reader_info_list = list() for storage_handler in storage_handlers: try: large_format = isinstance(storage_handler, HDF5Handler.HDF5Handler) properties = Migration.transform_to_latest(storage_handler.read_properties()) reader_info = ReaderInfo(properties, [False], large_format, storage_handler, storage_handler.reference) reader_info_list.append(reader_info) except Exception as e: logging.debug("Error reading %s", storage_handler.reference) import traceback traceback.print_exc() traceback.print_stack() library_storage_properties = persistent_storage_system.library_storage_properties for deletion in copy.deepcopy(library_storage_properties.get("data_item_deletions", list())): if not deletion in deletions: deletions.append(deletion) preliminary_library_updates = dict() library_updates = dict() if not ignore_older_files: Migration.migrate_to_latest(reader_info_list, preliminary_library_updates) good_reader_info_list = list() count = len(reader_info_list) for index, reader_info in enumerate(reader_info_list): storage_handler = reader_info.storage_handler properties = reader_info.properties try: version = properties.get("version", 0) if version == DataItem.DataItem.writer_version: data_item_uuid = uuid.UUID(properties["uuid"]) if not data_item_uuid in data_item_uuids: if str(data_item_uuid) in deletions: utilized_deletions.add(data_item_uuid) else: auto_migrate_data_item(reader_info, persistent_storage_system, new_persistent_storage_system, index, count) good_reader_info_list.append(reader_info) data_item_uuids.add(data_item_uuid) library_update = preliminary_library_updates.get(data_item_uuid) if library_update: library_updates[data_item_uuid] = library_update except Exception as e: logging.debug("Error reading %s", storage_handler.reference) import traceback traceback.print_exc() traceback.print_stack() return good_reader_info_list, library_updates
python
def auto_migrate_storage_system(*, persistent_storage_system=None, new_persistent_storage_system=None, data_item_uuids=None, deletions: typing.List[uuid.UUID] = None, utilized_deletions: typing.Set[uuid.UUID] = None, ignore_older_files: bool = True): """Migrate items from the storage system to the object context. Files in data_item_uuids have already been loaded and are ignored (not migrated). Files in deletes have been deleted in object context and are ignored (not migrated) and then added to the utilized deletions list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items. """ storage_handlers = persistent_storage_system.find_data_items() ReaderInfo = collections.namedtuple("ReaderInfo", ["properties", "changed_ref", "large_format", "storage_handler", "identifier"]) reader_info_list = list() for storage_handler in storage_handlers: try: large_format = isinstance(storage_handler, HDF5Handler.HDF5Handler) properties = Migration.transform_to_latest(storage_handler.read_properties()) reader_info = ReaderInfo(properties, [False], large_format, storage_handler, storage_handler.reference) reader_info_list.append(reader_info) except Exception as e: logging.debug("Error reading %s", storage_handler.reference) import traceback traceback.print_exc() traceback.print_stack() library_storage_properties = persistent_storage_system.library_storage_properties for deletion in copy.deepcopy(library_storage_properties.get("data_item_deletions", list())): if not deletion in deletions: deletions.append(deletion) preliminary_library_updates = dict() library_updates = dict() if not ignore_older_files: Migration.migrate_to_latest(reader_info_list, preliminary_library_updates) good_reader_info_list = list() count = len(reader_info_list) for index, reader_info in enumerate(reader_info_list): storage_handler = reader_info.storage_handler properties = reader_info.properties try: version = properties.get("version", 0) if version == DataItem.DataItem.writer_version: data_item_uuid = uuid.UUID(properties["uuid"]) if not data_item_uuid in data_item_uuids: if str(data_item_uuid) in deletions: utilized_deletions.add(data_item_uuid) else: auto_migrate_data_item(reader_info, persistent_storage_system, new_persistent_storage_system, index, count) good_reader_info_list.append(reader_info) data_item_uuids.add(data_item_uuid) library_update = preliminary_library_updates.get(data_item_uuid) if library_update: library_updates[data_item_uuid] = library_update except Exception as e: logging.debug("Error reading %s", storage_handler.reference) import traceback traceback.print_exc() traceback.print_stack() return good_reader_info_list, library_updates
[ "def", "auto_migrate_storage_system", "(", "*", ",", "persistent_storage_system", "=", "None", ",", "new_persistent_storage_system", "=", "None", ",", "data_item_uuids", "=", "None", ",", "deletions", ":", "typing", ".", "List", "[", "uuid", ".", "UUID", "]", "=", "None", ",", "utilized_deletions", ":", "typing", ".", "Set", "[", "uuid", ".", "UUID", "]", "=", "None", ",", "ignore_older_files", ":", "bool", "=", "True", ")", ":", "storage_handlers", "=", "persistent_storage_system", ".", "find_data_items", "(", ")", "ReaderInfo", "=", "collections", ".", "namedtuple", "(", "\"ReaderInfo\"", ",", "[", "\"properties\"", ",", "\"changed_ref\"", ",", "\"large_format\"", ",", "\"storage_handler\"", ",", "\"identifier\"", "]", ")", "reader_info_list", "=", "list", "(", ")", "for", "storage_handler", "in", "storage_handlers", ":", "try", ":", "large_format", "=", "isinstance", "(", "storage_handler", ",", "HDF5Handler", ".", "HDF5Handler", ")", "properties", "=", "Migration", ".", "transform_to_latest", "(", "storage_handler", ".", "read_properties", "(", ")", ")", "reader_info", "=", "ReaderInfo", "(", "properties", ",", "[", "False", "]", ",", "large_format", ",", "storage_handler", ",", "storage_handler", ".", "reference", ")", "reader_info_list", ".", "append", "(", "reader_info", ")", "except", "Exception", "as", "e", ":", "logging", ".", "debug", "(", "\"Error reading %s\"", ",", "storage_handler", ".", "reference", ")", "import", "traceback", "traceback", ".", "print_exc", "(", ")", "traceback", ".", "print_stack", "(", ")", "library_storage_properties", "=", "persistent_storage_system", ".", "library_storage_properties", "for", "deletion", "in", "copy", ".", "deepcopy", "(", "library_storage_properties", ".", "get", "(", "\"data_item_deletions\"", ",", "list", "(", ")", ")", ")", ":", "if", "not", "deletion", "in", "deletions", ":", "deletions", ".", "append", "(", "deletion", ")", "preliminary_library_updates", "=", "dict", "(", ")", "library_updates", "=", "dict", "(", ")", "if", "not", "ignore_older_files", ":", "Migration", ".", "migrate_to_latest", "(", "reader_info_list", ",", "preliminary_library_updates", ")", "good_reader_info_list", "=", "list", "(", ")", "count", "=", "len", "(", "reader_info_list", ")", "for", "index", ",", "reader_info", "in", "enumerate", "(", "reader_info_list", ")", ":", "storage_handler", "=", "reader_info", ".", "storage_handler", "properties", "=", "reader_info", ".", "properties", "try", ":", "version", "=", "properties", ".", "get", "(", "\"version\"", ",", "0", ")", "if", "version", "==", "DataItem", ".", "DataItem", ".", "writer_version", ":", "data_item_uuid", "=", "uuid", ".", "UUID", "(", "properties", "[", "\"uuid\"", "]", ")", "if", "not", "data_item_uuid", "in", "data_item_uuids", ":", "if", "str", "(", "data_item_uuid", ")", "in", "deletions", ":", "utilized_deletions", ".", "add", "(", "data_item_uuid", ")", "else", ":", "auto_migrate_data_item", "(", "reader_info", ",", "persistent_storage_system", ",", "new_persistent_storage_system", ",", "index", ",", "count", ")", "good_reader_info_list", ".", "append", "(", "reader_info", ")", "data_item_uuids", ".", "add", "(", "data_item_uuid", ")", "library_update", "=", "preliminary_library_updates", ".", "get", "(", "data_item_uuid", ")", "if", "library_update", ":", "library_updates", "[", "data_item_uuid", "]", "=", "library_update", "except", "Exception", "as", "e", ":", "logging", ".", "debug", "(", "\"Error reading %s\"", ",", "storage_handler", ".", "reference", ")", "import", "traceback", "traceback", ".", "print_exc", "(", ")", "traceback", ".", "print_stack", "(", ")", "return", "good_reader_info_list", ",", "library_updates" ]
Migrate items from the storage system to the object context. Files in data_item_uuids have already been loaded and are ignored (not migrated). Files in deletes have been deleted in object context and are ignored (not migrated) and then added to the utilized deletions list. Data items will have persistent_object_context set upon return, but caller will need to call finish_reading on each of the data items.
[ "Migrate", "items", "from", "the", "storage", "system", "to", "the", "object", "context", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/FileStorageSystem.py#L601-L658
train
nion-software/nionswift
nion/swift/model/FileStorageSystem.py
FileStorageSystem.rewrite_properties
def rewrite_properties(self, properties): """Set the properties and write to disk.""" with self.__properties_lock: self.__properties = properties self.__write_properties(None)
python
def rewrite_properties(self, properties): """Set the properties and write to disk.""" with self.__properties_lock: self.__properties = properties self.__write_properties(None)
[ "def", "rewrite_properties", "(", "self", ",", "properties", ")", ":", "with", "self", ".", "__properties_lock", ":", "self", ".", "__properties", "=", "properties", "self", ".", "__write_properties", "(", "None", ")" ]
Set the properties and write to disk.
[ "Set", "the", "properties", "and", "write", "to", "disk", "." ]
d43693eaf057b8683b9638e575000f055fede452
https://github.com/nion-software/nionswift/blob/d43693eaf057b8683b9638e575000f055fede452/nion/swift/model/FileStorageSystem.py#L137-L141
train