repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
listlengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
listlengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
openstax/cnx-archive
cnxarchive/sitemap.py
SitemapIndex.to_string
def to_string(self): """Convert SitemapIndex into a string.""" root = etree.Element('sitemapindex', nsmap={None: SITEMAP_NS}) for sitemap in self.sitemaps: sm = etree.SubElement(root, 'sitemap') etree.SubElement(sm, 'loc').text = sitemap.url if hasattr(sitemap.lastmod, 'strftime'): etree.SubElement(sm, 'lastmod').text = \ sitemap.lastmod.strftime('%Y-%m-%d') elif isinstance(sitemap.lastmod, str): etree.SubElement(sm, 'lastmod').text = sitemap.lastmod return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='utf-8')
python
def to_string(self): """Convert SitemapIndex into a string.""" root = etree.Element('sitemapindex', nsmap={None: SITEMAP_NS}) for sitemap in self.sitemaps: sm = etree.SubElement(root, 'sitemap') etree.SubElement(sm, 'loc').text = sitemap.url if hasattr(sitemap.lastmod, 'strftime'): etree.SubElement(sm, 'lastmod').text = \ sitemap.lastmod.strftime('%Y-%m-%d') elif isinstance(sitemap.lastmod, str): etree.SubElement(sm, 'lastmod').text = sitemap.lastmod return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='utf-8')
[ "def", "to_string", "(", "self", ")", ":", "root", "=", "etree", ".", "Element", "(", "'sitemapindex'", ",", "nsmap", "=", "{", "None", ":", "SITEMAP_NS", "}", ")", "for", "sitemap", "in", "self", ".", "sitemaps", ":", "sm", "=", "etree", ".", "SubEl...
Convert SitemapIndex into a string.
[ "Convert", "SitemapIndex", "into", "a", "string", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/sitemap.py#L37-L49
openstax/cnx-archive
cnxarchive/sitemap.py
Sitemap.add_url
def add_url(self, *args, **kwargs): """Add a new url to the sitemap. This function can either be called with a :class:`UrlEntry` or some keyword and positional arguments that are forwarded to the :class:`UrlEntry` constructor. """ if len(args) == 1 and not kwargs and isinstance(args[0], UrlEntry): self.urls.append(args[0]) else: self.urls.append(UrlEntry(*args, **kwargs))
python
def add_url(self, *args, **kwargs): """Add a new url to the sitemap. This function can either be called with a :class:`UrlEntry` or some keyword and positional arguments that are forwarded to the :class:`UrlEntry` constructor. """ if len(args) == 1 and not kwargs and isinstance(args[0], UrlEntry): self.urls.append(args[0]) else: self.urls.append(UrlEntry(*args, **kwargs))
[ "def", "add_url", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "len", "(", "args", ")", "==", "1", "and", "not", "kwargs", "and", "isinstance", "(", "args", "[", "0", "]", ",", "UrlEntry", ")", ":", "self", ".", "urls...
Add a new url to the sitemap. This function can either be called with a :class:`UrlEntry` or some keyword and positional arguments that are forwarded to the :class:`UrlEntry` constructor.
[ "Add", "a", "new", "url", "to", "the", "sitemap", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/sitemap.py#L74-L84
openstax/cnx-archive
cnxarchive/sitemap.py
Sitemap.to_string
def to_string(self): """Convert the sitemap into a string.""" root = etree.Element('urlset', nsmap={None: SITEMAP_NS}) for url in self.urls: url.generate(root) return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='utf-8')
python
def to_string(self): """Convert the sitemap into a string.""" root = etree.Element('urlset', nsmap={None: SITEMAP_NS}) for url in self.urls: url.generate(root) return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='utf-8')
[ "def", "to_string", "(", "self", ")", ":", "root", "=", "etree", ".", "Element", "(", "'urlset'", ",", "nsmap", "=", "{", "None", ":", "SITEMAP_NS", "}", ")", "for", "url", "in", "self", ".", "urls", ":", "url", ".", "generate", "(", "root", ")", ...
Convert the sitemap into a string.
[ "Convert", "the", "sitemap", "into", "a", "string", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/sitemap.py#L93-L99
openstax/cnx-archive
cnxarchive/sitemap.py
UrlEntry.generate
def generate(self, root_element=None): """Create <url> element under root_element.""" if root_element is not None: url = etree.SubElement(root_element, 'url') else: url = etree.Element('url') etree.SubElement(url, 'loc').text = self.loc if self.lastmod: if hasattr(self.lastmod, 'strftime'): etree.SubElement(url, 'lastmod').text = \ self.lastmod.strftime('%Y-%m-%d') elif isinstance(self.lastmod, str): etree.SubElement(url, 'lastmod').text = self.lastmod if self.changefreq and self.changefreq in self.freq_values: etree.SubElement(url, 'changefreq').text = self.changefreq if self.priority and 0.0 <= self.priority <= 1.0: etree.SubElement(url, 'priority').text = str(self.priority) return url
python
def generate(self, root_element=None): """Create <url> element under root_element.""" if root_element is not None: url = etree.SubElement(root_element, 'url') else: url = etree.Element('url') etree.SubElement(url, 'loc').text = self.loc if self.lastmod: if hasattr(self.lastmod, 'strftime'): etree.SubElement(url, 'lastmod').text = \ self.lastmod.strftime('%Y-%m-%d') elif isinstance(self.lastmod, str): etree.SubElement(url, 'lastmod').text = self.lastmod if self.changefreq and self.changefreq in self.freq_values: etree.SubElement(url, 'changefreq').text = self.changefreq if self.priority and 0.0 <= self.priority <= 1.0: etree.SubElement(url, 'priority').text = str(self.priority) return url
[ "def", "generate", "(", "self", ",", "root_element", "=", "None", ")", ":", "if", "root_element", "is", "not", "None", ":", "url", "=", "etree", ".", "SubElement", "(", "root_element", ",", "'url'", ")", "else", ":", "url", "=", "etree", ".", "Element"...
Create <url> element under root_element.
[ "Create", "<url", ">", "element", "under", "root_element", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/sitemap.py#L153-L170
xenon-middleware/pyxenon
examples/interactive.py
make_input_stream
def make_input_stream(): """Creates a :py:class:`Queue` object and a co-routine yielding from that queue. The queue should be populated with 2-tuples of the form `(command, message)`, where `command` is one of [`msg`, `end`]. When the `end` command is recieved, the co-routine returns, ending the stream. When a `msg` command is received, the accompanying message is encoded and yielded as a ``bytes`` object. :return: tuple of (queue, stream)""" input_queue = Queue() def input_stream(): while True: cmd, msg = input_queue.get() if cmd == 'end': input_queue.task_done() return elif cmd == 'msg': yield msg.encode() input_queue.task_done() return input_queue, input_stream
python
def make_input_stream(): """Creates a :py:class:`Queue` object and a co-routine yielding from that queue. The queue should be populated with 2-tuples of the form `(command, message)`, where `command` is one of [`msg`, `end`]. When the `end` command is recieved, the co-routine returns, ending the stream. When a `msg` command is received, the accompanying message is encoded and yielded as a ``bytes`` object. :return: tuple of (queue, stream)""" input_queue = Queue() def input_stream(): while True: cmd, msg = input_queue.get() if cmd == 'end': input_queue.task_done() return elif cmd == 'msg': yield msg.encode() input_queue.task_done() return input_queue, input_stream
[ "def", "make_input_stream", "(", ")", ":", "input_queue", "=", "Queue", "(", ")", "def", "input_stream", "(", ")", ":", "while", "True", ":", "cmd", ",", "msg", "=", "input_queue", ".", "get", "(", ")", "if", "cmd", "==", "'end'", ":", "input_queue", ...
Creates a :py:class:`Queue` object and a co-routine yielding from that queue. The queue should be populated with 2-tuples of the form `(command, message)`, where `command` is one of [`msg`, `end`]. When the `end` command is recieved, the co-routine returns, ending the stream. When a `msg` command is received, the accompanying message is encoded and yielded as a ``bytes`` object. :return: tuple of (queue, stream)
[ "Creates", "a", ":", "py", ":", "class", ":", "Queue", "object", "and", "a", "co", "-", "routine", "yielding", "from", "that", "queue", ".", "The", "queue", "should", "be", "populated", "with", "2", "-", "tuples", "of", "the", "form", "(", "command", ...
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/examples/interactive.py#L6-L30
openstax/cnx-archive
cnxarchive/views/sitemap.py
notblocked
def notblocked(page): """Determine if given url is a page that should be in sitemap.""" for blocked in PAGES_TO_BLOCK: if blocked[0] != '*': blocked = '*' + blocked rx = re.compile(blocked.replace('*', '[^$]*')) if rx.match(page): return False return True
python
def notblocked(page): """Determine if given url is a page that should be in sitemap.""" for blocked in PAGES_TO_BLOCK: if blocked[0] != '*': blocked = '*' + blocked rx = re.compile(blocked.replace('*', '[^$]*')) if rx.match(page): return False return True
[ "def", "notblocked", "(", "page", ")", ":", "for", "blocked", "in", "PAGES_TO_BLOCK", ":", "if", "blocked", "[", "0", "]", "!=", "'*'", ":", "blocked", "=", "'*'", "+", "blocked", "rx", "=", "re", ".", "compile", "(", "blocked", ".", "replace", "(", ...
Determine if given url is a page that should be in sitemap.
[ "Determine", "if", "given", "url", "is", "a", "page", "that", "should", "be", "in", "sitemap", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/sitemap.py#L39-L47
openstax/cnx-archive
cnxarchive/views/sitemap.py
sitemap
def sitemap(request): """Return a sitemap xml file for search engines.""" xml = Sitemap() author = request.matchdict.get('from_id') if author is not None: match = "AND '{}' = authors[1]".format(author) else: match = '' with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute("""SELECT ident_hash(uuid, major_version, minor_version) AS idver, name, revised FROM latest_modules WHERE portal_type NOT IN ('CompositeModule', 'SubCollection') {} ORDER BY module_ident DESC""".format(match)) res = cursor.fetchall() for ident_hash, page_name, revised in res: # replace punctuation with whitespace page_name = re.sub(NON_WORD, ' ', page_name.decode('utf-8'), re.UNICODE) # remove leading and trailing whitespace page_name = page_name.strip().encode('utf-8') # replace spaces with dashes page_name = re.sub(' +', '-', page_name.decode('utf-8'), re.UNICODE) url = request.route_url('content', ident_hash=ident_hash, ignore=u'/{}'.format(page_name)) if notblocked(url): xml.add_url(url, lastmod=revised) resp = request.response resp.status = '200 OK' resp.content_type = 'text/xml' resp.body = xml() return resp
python
def sitemap(request): """Return a sitemap xml file for search engines.""" xml = Sitemap() author = request.matchdict.get('from_id') if author is not None: match = "AND '{}' = authors[1]".format(author) else: match = '' with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute("""SELECT ident_hash(uuid, major_version, minor_version) AS idver, name, revised FROM latest_modules WHERE portal_type NOT IN ('CompositeModule', 'SubCollection') {} ORDER BY module_ident DESC""".format(match)) res = cursor.fetchall() for ident_hash, page_name, revised in res: # replace punctuation with whitespace page_name = re.sub(NON_WORD, ' ', page_name.decode('utf-8'), re.UNICODE) # remove leading and trailing whitespace page_name = page_name.strip().encode('utf-8') # replace spaces with dashes page_name = re.sub(' +', '-', page_name.decode('utf-8'), re.UNICODE) url = request.route_url('content', ident_hash=ident_hash, ignore=u'/{}'.format(page_name)) if notblocked(url): xml.add_url(url, lastmod=revised) resp = request.response resp.status = '200 OK' resp.content_type = 'text/xml' resp.body = xml() return resp
[ "def", "sitemap", "(", "request", ")", ":", "xml", "=", "Sitemap", "(", ")", "author", "=", "request", ".", "matchdict", ".", "get", "(", "'from_id'", ")", "if", "author", "is", "not", "None", ":", "match", "=", "\"AND '{}' = authors[1]\"", ".", "format"...
Return a sitemap xml file for search engines.
[ "Return", "a", "sitemap", "xml", "file", "for", "search", "engines", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/sitemap.py#L56-L96
openstax/cnx-archive
cnxarchive/views/sitemap.py
sitemap_index
def sitemap_index(request): """Return a sitemap index xml file for search engines.""" sitemaps = [] with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute("""\ SELECT authors[1], max(revised) FROM latest_modules WHERE portal_type NOT IN ('CompositeModule', 'SubCollection') GROUP BY authors[1] """) for author, revised in cursor.fetchall(): sitemaps.append(Sitemap(url=request.route_url( 'sitemap', from_id=author), lastmod=revised)) si = SitemapIndex(sitemaps=sitemaps) resp = request.response resp.status = '200 OK' resp.content_type = 'text/xml' resp.body = si() return resp
python
def sitemap_index(request): """Return a sitemap index xml file for search engines.""" sitemaps = [] with db_connect() as db_connection: with db_connection.cursor() as cursor: cursor.execute("""\ SELECT authors[1], max(revised) FROM latest_modules WHERE portal_type NOT IN ('CompositeModule', 'SubCollection') GROUP BY authors[1] """) for author, revised in cursor.fetchall(): sitemaps.append(Sitemap(url=request.route_url( 'sitemap', from_id=author), lastmod=revised)) si = SitemapIndex(sitemaps=sitemaps) resp = request.response resp.status = '200 OK' resp.content_type = 'text/xml' resp.body = si() return resp
[ "def", "sitemap_index", "(", "request", ")", ":", "sitemaps", "=", "[", "]", "with", "db_connect", "(", ")", "as", "db_connection", ":", "with", "db_connection", ".", "cursor", "(", ")", "as", "cursor", ":", "cursor", ".", "execute", "(", "\"\"\"\\\n ...
Return a sitemap index xml file for search engines.
[ "Return", "a", "sitemap", "index", "xml", "file", "for", "search", "engines", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/sitemap.py#L101-L123
ZELLMECHANIK-DRESDEN/dclab
dclab/features/volume.py
get_volume
def get_volume(cont, pos_x, pos_y, pix): """Calculate the volume of a polygon revolved around an axis The volume estimation assumes rotational symmetry. Green`s theorem and the Gaussian divergence theorem allow to formulate the volume as a line integral. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event [px] e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. pos_x: float or ndarray of length N The x coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_x` pos_y: float or ndarray of length N The y coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_y` px_um: float The detector pixel size in µm. e.g. obtained using: `mm.config["image"]["pix size"]` Returns ------- volume: float or ndarray volume in um^3 Notes ----- The computation of the volume is based on a full rotation of the upper and the lower halves of the contour from which the average is then used. The volume is computed radially from the the center position given by (`pos_x`, `pos_y`). For sufficiently smooth contours, such as densely sampled ellipses, the center position does not play an important role. For contours that are given on a coarse grid, as is the case for RT-DC, the center position must be given. References ---------- - Halpern et al. :cite:`Halpern2002`, chapter 5, Section 5.4 - This is a translation from a `Matlab script <http://de.mathworks.com/matlabcentral/fileexchange/36525-volrevolve>`_ by Geoff Olynyk. """ if np.isscalar(pos_x): cont = [cont] ret_list = False else: ret_list = True # Convert input to 1D arrays pos_x = np.atleast_1d(pos_x) pos_y = np.atleast_1d(pos_y) if pos_x.size != pos_y.size: raise ValueError("Size of `pos_x` and `pos_y` must match!") if pos_x.size > 1 and len(cont) <= 1: raise ValueError("Number of given contours too small!") # results are stored in a separate array initialized with nans v_avg = np.zeros_like(pos_x, dtype=float)*np.nan # v_avg has the shape of `pos_x`. We are iterating over the smallest # length for `cont` and `pos_x`. for ii in range(min(len(cont), pos_x.shape[0])): # If the contour has less than 4 pixels, the computation will fail. # In that case, the value np.nan is already assigned. cc = cont[ii] if cc.shape[0] >= 4: # Center contour coordinates with given centroid contour_x = cc[:, 0] - pos_x[ii] / pix contour_y = cc[:, 1] - pos_y[ii] / pix # Make sure contour is counter-clockwise contour_x, contour_y = counter_clockwise(contour_x, contour_y) # Which points are below the x-axis? (y<0)? ind_low = np.where(contour_y < 0) # These points will be shifted up to y=0 to build an x-axis # (wont contribute to lower volume). contour_y_low = np.copy(contour_y) contour_y_low[ind_low] = 0 # Which points are above the x-axis? (y>0)? ind_upp = np.where(contour_y > 0) # These points will be shifted down to y=0 to build an x-axis # (wont contribute to upper volume). contour_y_upp = np.copy(contour_y) contour_y_upp[ind_upp] = 0 # Move the contour to the left Z = contour_x # Last point of the contour has to overlap with the first point Z = np.hstack([Z, Z[0]]) Zp = Z[0:-1] dZ = Z[1:]-Zp # Last point of the contour has to overlap with the first point contour_y_low = np.hstack([contour_y_low, contour_y_low[0]]) contour_y_upp = np.hstack([contour_y_upp, contour_y_upp[0]]) vol_low = _vol_helper(contour_y_low, Z, Zp, dZ, pix) vol_upp = _vol_helper(contour_y_upp, Z, Zp, dZ, pix) v_avg[ii] = (vol_low + vol_upp) / 2 if not ret_list: # Do not return a list if the input contour was not in a list v_avg = v_avg[0] return v_avg
python
def get_volume(cont, pos_x, pos_y, pix): """Calculate the volume of a polygon revolved around an axis The volume estimation assumes rotational symmetry. Green`s theorem and the Gaussian divergence theorem allow to formulate the volume as a line integral. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event [px] e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. pos_x: float or ndarray of length N The x coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_x` pos_y: float or ndarray of length N The y coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_y` px_um: float The detector pixel size in µm. e.g. obtained using: `mm.config["image"]["pix size"]` Returns ------- volume: float or ndarray volume in um^3 Notes ----- The computation of the volume is based on a full rotation of the upper and the lower halves of the contour from which the average is then used. The volume is computed radially from the the center position given by (`pos_x`, `pos_y`). For sufficiently smooth contours, such as densely sampled ellipses, the center position does not play an important role. For contours that are given on a coarse grid, as is the case for RT-DC, the center position must be given. References ---------- - Halpern et al. :cite:`Halpern2002`, chapter 5, Section 5.4 - This is a translation from a `Matlab script <http://de.mathworks.com/matlabcentral/fileexchange/36525-volrevolve>`_ by Geoff Olynyk. """ if np.isscalar(pos_x): cont = [cont] ret_list = False else: ret_list = True # Convert input to 1D arrays pos_x = np.atleast_1d(pos_x) pos_y = np.atleast_1d(pos_y) if pos_x.size != pos_y.size: raise ValueError("Size of `pos_x` and `pos_y` must match!") if pos_x.size > 1 and len(cont) <= 1: raise ValueError("Number of given contours too small!") # results are stored in a separate array initialized with nans v_avg = np.zeros_like(pos_x, dtype=float)*np.nan # v_avg has the shape of `pos_x`. We are iterating over the smallest # length for `cont` and `pos_x`. for ii in range(min(len(cont), pos_x.shape[0])): # If the contour has less than 4 pixels, the computation will fail. # In that case, the value np.nan is already assigned. cc = cont[ii] if cc.shape[0] >= 4: # Center contour coordinates with given centroid contour_x = cc[:, 0] - pos_x[ii] / pix contour_y = cc[:, 1] - pos_y[ii] / pix # Make sure contour is counter-clockwise contour_x, contour_y = counter_clockwise(contour_x, contour_y) # Which points are below the x-axis? (y<0)? ind_low = np.where(contour_y < 0) # These points will be shifted up to y=0 to build an x-axis # (wont contribute to lower volume). contour_y_low = np.copy(contour_y) contour_y_low[ind_low] = 0 # Which points are above the x-axis? (y>0)? ind_upp = np.where(contour_y > 0) # These points will be shifted down to y=0 to build an x-axis # (wont contribute to upper volume). contour_y_upp = np.copy(contour_y) contour_y_upp[ind_upp] = 0 # Move the contour to the left Z = contour_x # Last point of the contour has to overlap with the first point Z = np.hstack([Z, Z[0]]) Zp = Z[0:-1] dZ = Z[1:]-Zp # Last point of the contour has to overlap with the first point contour_y_low = np.hstack([contour_y_low, contour_y_low[0]]) contour_y_upp = np.hstack([contour_y_upp, contour_y_upp[0]]) vol_low = _vol_helper(contour_y_low, Z, Zp, dZ, pix) vol_upp = _vol_helper(contour_y_upp, Z, Zp, dZ, pix) v_avg[ii] = (vol_low + vol_upp) / 2 if not ret_list: # Do not return a list if the input contour was not in a list v_avg = v_avg[0] return v_avg
[ "def", "get_volume", "(", "cont", ",", "pos_x", ",", "pos_y", ",", "pix", ")", ":", "if", "np", ".", "isscalar", "(", "pos_x", ")", ":", "cont", "=", "[", "cont", "]", "ret_list", "=", "False", "else", ":", "ret_list", "=", "True", "# Convert input t...
Calculate the volume of a polygon revolved around an axis The volume estimation assumes rotational symmetry. Green`s theorem and the Gaussian divergence theorem allow to formulate the volume as a line integral. Parameters ---------- cont: ndarray or list of ndarrays of shape (N,2) A 2D array that holds the contour of an event [px] e.g. obtained using `mm.contour` where `mm` is an instance of `RTDCBase`. The first and second columns of `cont` correspond to the x- and y-coordinates of the contour. pos_x: float or ndarray of length N The x coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_x` pos_y: float or ndarray of length N The y coordinate(s) of the centroid of the event(s) [µm] e.g. obtained using `mm.pos_y` px_um: float The detector pixel size in µm. e.g. obtained using: `mm.config["image"]["pix size"]` Returns ------- volume: float or ndarray volume in um^3 Notes ----- The computation of the volume is based on a full rotation of the upper and the lower halves of the contour from which the average is then used. The volume is computed radially from the the center position given by (`pos_x`, `pos_y`). For sufficiently smooth contours, such as densely sampled ellipses, the center position does not play an important role. For contours that are given on a coarse grid, as is the case for RT-DC, the center position must be given. References ---------- - Halpern et al. :cite:`Halpern2002`, chapter 5, Section 5.4 - This is a translation from a `Matlab script <http://de.mathworks.com/matlabcentral/fileexchange/36525-volrevolve>`_ by Geoff Olynyk.
[ "Calculate", "the", "volume", "of", "a", "polygon", "revolved", "around", "an", "axis" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/volume.py#L9-L121
ZELLMECHANIK-DRESDEN/dclab
dclab/features/volume.py
counter_clockwise
def counter_clockwise(cx, cy): """Put contour coordinates into counter-clockwise order Parameters ---------- cx, cy: 1d ndarrays The x- and y-coordinates of the contour Returns ------- cx_cc, cy_cc: The x- and y-coordinates of the contour in counter-clockwise orientation. """ # test orientation angles = np.unwrap(np.arctan2(cy, cx)) grad = np.gradient(angles) if np.average(grad) > 0: return cx[::-1], cy[::-1] else: return cx, cy
python
def counter_clockwise(cx, cy): """Put contour coordinates into counter-clockwise order Parameters ---------- cx, cy: 1d ndarrays The x- and y-coordinates of the contour Returns ------- cx_cc, cy_cc: The x- and y-coordinates of the contour in counter-clockwise orientation. """ # test orientation angles = np.unwrap(np.arctan2(cy, cx)) grad = np.gradient(angles) if np.average(grad) > 0: return cx[::-1], cy[::-1] else: return cx, cy
[ "def", "counter_clockwise", "(", "cx", ",", "cy", ")", ":", "# test orientation", "angles", "=", "np", ".", "unwrap", "(", "np", ".", "arctan2", "(", "cy", ",", "cx", ")", ")", "grad", "=", "np", ".", "gradient", "(", "angles", ")", "if", "np", "."...
Put contour coordinates into counter-clockwise order Parameters ---------- cx, cy: 1d ndarrays The x- and y-coordinates of the contour Returns ------- cx_cc, cy_cc: The x- and y-coordinates of the contour in counter-clockwise orientation.
[ "Put", "contour", "coordinates", "into", "counter", "-", "clockwise", "order" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/volume.py#L124-L144
robmcmullen/atrcopy
atrcopy/dos33.py
Dos33Dirent.add_metadata_sectors
def add_metadata_sectors(self, vtoc, sector_list, header): """Add track/sector list """ tslist = BaseSectorList(header) for start in range(0, len(sector_list), header.ts_pairs): end = min(start + header.ts_pairs, len(sector_list)) if _xd: log.debug("ts: %d-%d" % (start, end)) s = Dos33TSSector(header, sector_list, start, end) s.ts_start, s.ts_end = start, end tslist.append(s) self.num_tslists = len(tslist) vtoc.assign_sector_numbers(self, tslist) sector_list.extend(tslist) self.track, self.sector = header.track_from_sector(tslist[0].sector_num) if _xd: log.debug("track/sector lists:\n%s" % str(tslist))
python
def add_metadata_sectors(self, vtoc, sector_list, header): """Add track/sector list """ tslist = BaseSectorList(header) for start in range(0, len(sector_list), header.ts_pairs): end = min(start + header.ts_pairs, len(sector_list)) if _xd: log.debug("ts: %d-%d" % (start, end)) s = Dos33TSSector(header, sector_list, start, end) s.ts_start, s.ts_end = start, end tslist.append(s) self.num_tslists = len(tslist) vtoc.assign_sector_numbers(self, tslist) sector_list.extend(tslist) self.track, self.sector = header.track_from_sector(tslist[0].sector_num) if _xd: log.debug("track/sector lists:\n%s" % str(tslist))
[ "def", "add_metadata_sectors", "(", "self", ",", "vtoc", ",", "sector_list", ",", "header", ")", ":", "tslist", "=", "BaseSectorList", "(", "header", ")", "for", "start", "in", "range", "(", "0", ",", "len", "(", "sector_list", ")", ",", "header", ".", ...
Add track/sector list
[ "Add", "track", "/", "sector", "list" ]
train
https://github.com/robmcmullen/atrcopy/blob/dafba8e74c718e95cf81fd72c184fa193ecec730/atrcopy/dos33.py#L268-L282
openstax/cnx-archive
cnxarchive/views/extras.py
_get_subject_list_generator
def _get_subject_list_generator(cursor): """Return all subjects (tags) in the database except "internal" scheme.""" subject = None last_tagid = None cursor.execute(SQL['get-subject-list']) for s in cursor.fetchall(): tagid, tagname, portal_type, count = s if tagid != last_tagid: # It's a new subject, create a new dict and initialize count if subject: yield subject subject = {'id': tagid, 'name': tagname, 'count': {'module': 0, 'collection': 0}, } last_tagid = tagid if tagid == last_tagid and portal_type: # Just need to update the count subject['count'][portal_type.lower()] = count if subject: yield subject
python
def _get_subject_list_generator(cursor): """Return all subjects (tags) in the database except "internal" scheme.""" subject = None last_tagid = None cursor.execute(SQL['get-subject-list']) for s in cursor.fetchall(): tagid, tagname, portal_type, count = s if tagid != last_tagid: # It's a new subject, create a new dict and initialize count if subject: yield subject subject = {'id': tagid, 'name': tagname, 'count': {'module': 0, 'collection': 0}, } last_tagid = tagid if tagid == last_tagid and portal_type: # Just need to update the count subject['count'][portal_type.lower()] = count if subject: yield subject
[ "def", "_get_subject_list_generator", "(", "cursor", ")", ":", "subject", "=", "None", "last_tagid", "=", "None", "cursor", ".", "execute", "(", "SQL", "[", "'get-subject-list'", "]", ")", "for", "s", "in", "cursor", ".", "fetchall", "(", ")", ":", "tagid"...
Return all subjects (tags) in the database except "internal" scheme.
[ "Return", "all", "subjects", "(", "tags", ")", "in", "the", "database", "except", "internal", "scheme", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/extras.py#L32-L54
openstax/cnx-archive
cnxarchive/views/extras.py
extras
def extras(request): """Return a dict with archive metadata for webview.""" key = request.matchdict.get('key', '').lstrip('/') key_map = { 'languages': _get_available_languages_and_count, 'subjects': _get_subject_list, 'featured': _get_featured_links, 'messages': _get_service_state_messages, 'licenses': _get_licenses } with db_connect() as db_connection: with db_connection.cursor() as cursor: if key: proc = key_map[key] metadata = {key: proc(cursor)} else: metadata = {key: proc(cursor) for (key, proc) in key_map.items()} resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(metadata) return resp
python
def extras(request): """Return a dict with archive metadata for webview.""" key = request.matchdict.get('key', '').lstrip('/') key_map = { 'languages': _get_available_languages_and_count, 'subjects': _get_subject_list, 'featured': _get_featured_links, 'messages': _get_service_state_messages, 'licenses': _get_licenses } with db_connect() as db_connection: with db_connection.cursor() as cursor: if key: proc = key_map[key] metadata = {key: proc(cursor)} else: metadata = {key: proc(cursor) for (key, proc) in key_map.items()} resp = request.response resp.status = '200 OK' resp.content_type = 'application/json' resp.body = json.dumps(metadata) return resp
[ "def", "extras", "(", "request", ")", ":", "key", "=", "request", ".", "matchdict", ".", "get", "(", "'key'", ",", "''", ")", ".", "lstrip", "(", "'/'", ")", "key_map", "=", "{", "'languages'", ":", "_get_available_languages_and_count", ",", "'subjects'", ...
Return a dict with archive metadata for webview.
[ "Return", "a", "dict", "with", "archive", "metadata", "for", "webview", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/extras.py#L86-L110
xenon-middleware/pyxenon
examples/timeout.py
timeout
def timeout(delay, call, *args, **kwargs): """Run a function call for `delay` seconds, and raise a RuntimeError if the operation didn't complete.""" return_value = None def target(): nonlocal return_value return_value = call(*args, **kwargs) t = Thread(target=target) t.start() t.join(delay) if t.is_alive(): raise RuntimeError("Operation did not complete within time.") return return_value
python
def timeout(delay, call, *args, **kwargs): """Run a function call for `delay` seconds, and raise a RuntimeError if the operation didn't complete.""" return_value = None def target(): nonlocal return_value return_value = call(*args, **kwargs) t = Thread(target=target) t.start() t.join(delay) if t.is_alive(): raise RuntimeError("Operation did not complete within time.") return return_value
[ "def", "timeout", "(", "delay", ",", "call", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "return_value", "=", "None", "def", "target", "(", ")", ":", "nonlocal", "return_value", "return_value", "=", "call", "(", "*", "args", ",", "*", "*", ...
Run a function call for `delay` seconds, and raise a RuntimeError if the operation didn't complete.
[ "Run", "a", "function", "call", "for", "delay", "seconds", "and", "raise", "a", "RuntimeError", "if", "the", "operation", "didn", "t", "complete", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/examples/timeout.py#L4-L19
openstax/cnx-archive
cnxarchive/scripts/_utils.py
create_parser
def create_parser(name, description=None): """Create an argument parser with the given ``name`` and ``description``. The name is used to make ``cnx-archive-<name>`` program name. This creates and returns a parser with the ``config_uri`` argument declared. """ prog = _gen_prog_name(name) parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('config_uri', help="Configuration INI file.") parser.add_argument('--config-name', action='store', default='main', help="Supply a section name in the configuration") return parser
python
def create_parser(name, description=None): """Create an argument parser with the given ``name`` and ``description``. The name is used to make ``cnx-archive-<name>`` program name. This creates and returns a parser with the ``config_uri`` argument declared. """ prog = _gen_prog_name(name) parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('config_uri', help="Configuration INI file.") parser.add_argument('--config-name', action='store', default='main', help="Supply a section name in the configuration") return parser
[ "def", "create_parser", "(", "name", ",", "description", "=", "None", ")", ":", "prog", "=", "_gen_prog_name", "(", "name", ")", "parser", "=", "argparse", ".", "ArgumentParser", "(", "prog", "=", "prog", ",", "description", "=", "description", ")", "parse...
Create an argument parser with the given ``name`` and ``description``. The name is used to make ``cnx-archive-<name>`` program name. This creates and returns a parser with the ``config_uri`` argument declared.
[ "Create", "an", "argument", "parser", "with", "the", "given", "name", "and", "description", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/_utils.py#L28-L42
openstax/cnx-archive
cnxarchive/scripts/_utils.py
get_app_settings_from_arguments
def get_app_settings_from_arguments(args): """Parse ``argparse`` style arguments into app settings. Given an ``argparse`` set of arguments as ``args`` parse the arguments to return the application settings. This assumes the parser was created using ``create_parser``. """ config_filepath = os.path.abspath(args.config_uri) return get_appsettings(config_filepath, name=args.config_name)
python
def get_app_settings_from_arguments(args): """Parse ``argparse`` style arguments into app settings. Given an ``argparse`` set of arguments as ``args`` parse the arguments to return the application settings. This assumes the parser was created using ``create_parser``. """ config_filepath = os.path.abspath(args.config_uri) return get_appsettings(config_filepath, name=args.config_name)
[ "def", "get_app_settings_from_arguments", "(", "args", ")", ":", "config_filepath", "=", "os", ".", "path", ".", "abspath", "(", "args", ".", "config_uri", ")", "return", "get_appsettings", "(", "config_filepath", ",", "name", "=", "args", ".", "config_name", ...
Parse ``argparse`` style arguments into app settings. Given an ``argparse`` set of arguments as ``args`` parse the arguments to return the application settings. This assumes the parser was created using ``create_parser``.
[ "Parse", "argparse", "style", "arguments", "into", "app", "settings", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/_utils.py#L45-L53
xenon-middleware/pyxenon
xenon/server.py
check_socket
def check_socket(host, port): """Checks if port is open on host. This is used to check if the Xenon-GRPC server is running.""" with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: return sock.connect_ex((host, port)) == 0
python
def check_socket(host, port): """Checks if port is open on host. This is used to check if the Xenon-GRPC server is running.""" with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: return sock.connect_ex((host, port)) == 0
[ "def", "check_socket", "(", "host", ",", "port", ")", ":", "with", "closing", "(", "socket", ".", "socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_STREAM", ")", ")", "as", "sock", ":", "return", "sock", ".", "connect_ex", "(", "(", ...
Checks if port is open on host. This is used to check if the Xenon-GRPC server is running.
[ "Checks", "if", "port", "is", "open", "on", "host", ".", "This", "is", "used", "to", "check", "if", "the", "Xenon", "-", "GRPC", "server", "is", "running", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/server.py#L19-L23
xenon-middleware/pyxenon
xenon/server.py
get_secure_channel
def get_secure_channel(crt_file, key_file, port=50051): """Try to connect over a secure channel.""" creds = grpc.ssl_channel_credentials( root_certificates=open(str(crt_file), 'rb').read(), private_key=open(str(key_file), 'rb').read(), certificate_chain=open(str(crt_file), 'rb').read()) address = "{}:{}".format(socket.gethostname(), port) channel = grpc.secure_channel(address, creds) return channel
python
def get_secure_channel(crt_file, key_file, port=50051): """Try to connect over a secure channel.""" creds = grpc.ssl_channel_credentials( root_certificates=open(str(crt_file), 'rb').read(), private_key=open(str(key_file), 'rb').read(), certificate_chain=open(str(crt_file), 'rb').read()) address = "{}:{}".format(socket.gethostname(), port) channel = grpc.secure_channel(address, creds) return channel
[ "def", "get_secure_channel", "(", "crt_file", ",", "key_file", ",", "port", "=", "50051", ")", ":", "creds", "=", "grpc", ".", "ssl_channel_credentials", "(", "root_certificates", "=", "open", "(", "str", "(", "crt_file", ")", ",", "'rb'", ")", ".", "read"...
Try to connect over a secure channel.
[ "Try", "to", "connect", "over", "a", "secure", "channel", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/server.py#L26-L36
xenon-middleware/pyxenon
xenon/server.py
find_free_port
def find_free_port(): """Finds a free port.""" with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: sock.bind(('', 0)) return sock.getsockname()[1]
python
def find_free_port(): """Finds a free port.""" with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: sock.bind(('', 0)) return sock.getsockname()[1]
[ "def", "find_free_port", "(", ")", ":", "with", "closing", "(", "socket", ".", "socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_STREAM", ")", ")", "as", "sock", ":", "sock", ".", "bind", "(", "(", "''", ",", "0", ")", ")", "return...
Finds a free port.
[ "Finds", "a", "free", "port", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/server.py#L39-L43
xenon-middleware/pyxenon
xenon/server.py
print_stream
def print_stream(file, name): """Print stream from file to logger.""" logger = logging.getLogger('xenon.{}'.format(name)) for line in file: logger.info('[{}] {}'.format(name, line.strip()))
python
def print_stream(file, name): """Print stream from file to logger.""" logger = logging.getLogger('xenon.{}'.format(name)) for line in file: logger.info('[{}] {}'.format(name, line.strip()))
[ "def", "print_stream", "(", "file", ",", "name", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "'xenon.{}'", ".", "format", "(", "name", ")", ")", "for", "line", "in", "file", ":", "logger", ".", "info", "(", "'[{}] {}'", ".", "format", ...
Print stream from file to logger.
[ "Print", "stream", "from", "file", "to", "logger", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/server.py#L46-L50
xenon-middleware/pyxenon
xenon/server.py
init
def init(port=None, do_not_exit=False, disable_tls=False, log_level='WARNING'): """Start the Xenon GRPC server on the specified port, or, if a service is already running on that port, connect to that. If no port is given, a random port is selected. This means that, by default, every python instance will start its own instance of a xenon-grpc process. :param port: the port number :param do_not_exit: by default the GRPC server is shut down after Python exits (through the `atexit` module), setting this value to `True` will prevent that from happening.""" logger = logging.getLogger('xenon') logger.setLevel(logging.INFO) logger_handler = logging.StreamHandler() logger_handler.setFormatter(logging.Formatter(style='{')) logger_handler.setLevel(getattr(logging, log_level)) logger.addHandler(logger_handler) if port is None: port = find_free_port() if __server__.process is not None: logger.warning( "You tried to run init(), but the server is already running.") return __server__ __server__.port = port __server__.disable_tls = disable_tls __server__.__enter__() if not do_not_exit: atexit.register(__server__.__exit__, None, None, None) return __server__
python
def init(port=None, do_not_exit=False, disable_tls=False, log_level='WARNING'): """Start the Xenon GRPC server on the specified port, or, if a service is already running on that port, connect to that. If no port is given, a random port is selected. This means that, by default, every python instance will start its own instance of a xenon-grpc process. :param port: the port number :param do_not_exit: by default the GRPC server is shut down after Python exits (through the `atexit` module), setting this value to `True` will prevent that from happening.""" logger = logging.getLogger('xenon') logger.setLevel(logging.INFO) logger_handler = logging.StreamHandler() logger_handler.setFormatter(logging.Formatter(style='{')) logger_handler.setLevel(getattr(logging, log_level)) logger.addHandler(logger_handler) if port is None: port = find_free_port() if __server__.process is not None: logger.warning( "You tried to run init(), but the server is already running.") return __server__ __server__.port = port __server__.disable_tls = disable_tls __server__.__enter__() if not do_not_exit: atexit.register(__server__.__exit__, None, None, None) return __server__
[ "def", "init", "(", "port", "=", "None", ",", "do_not_exit", "=", "False", ",", "disable_tls", "=", "False", ",", "log_level", "=", "'WARNING'", ")", ":", "logger", "=", "logging", ".", "getLogger", "(", "'xenon'", ")", "logger", ".", "setLevel", "(", ...
Start the Xenon GRPC server on the specified port, or, if a service is already running on that port, connect to that. If no port is given, a random port is selected. This means that, by default, every python instance will start its own instance of a xenon-grpc process. :param port: the port number :param do_not_exit: by default the GRPC server is shut down after Python exits (through the `atexit` module), setting this value to `True` will prevent that from happening.
[ "Start", "the", "Xenon", "GRPC", "server", "on", "the", "specified", "port", "or", "if", "a", "service", "is", "already", "running", "on", "that", "port", "connect", "to", "that", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/server.py#L116-L151
openstax/cnx-archive
cnxarchive/events.py
add_cors_headers
def add_cors_headers(request, response): """Add cors headers needed for web app implementation.""" response.headerlist.append(('Access-Control-Allow-Origin', '*')) response.headerlist.append( ('Access-Control-Allow-Methods', 'GET, OPTIONS')) response.headerlist.append( ('Access-Control-Allow-Headers', ','.join(DEFAULT_ACCESS_CONTROL_ALLOW_HEADERS)))
python
def add_cors_headers(request, response): """Add cors headers needed for web app implementation.""" response.headerlist.append(('Access-Control-Allow-Origin', '*')) response.headerlist.append( ('Access-Control-Allow-Methods', 'GET, OPTIONS')) response.headerlist.append( ('Access-Control-Allow-Headers', ','.join(DEFAULT_ACCESS_CONTROL_ALLOW_HEADERS)))
[ "def", "add_cors_headers", "(", "request", ",", "response", ")", ":", "response", ".", "headerlist", ".", "append", "(", "(", "'Access-Control-Allow-Origin'", ",", "'*'", ")", ")", "response", ".", "headerlist", ".", "append", "(", "(", "'Access-Control-Allow-Me...
Add cors headers needed for web app implementation.
[ "Add", "cors", "headers", "needed", "for", "web", "app", "implementation", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/events.py#L15-L22
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_trace.py
TraceColumn.trace
def trace(self): """Initializes the trace data""" if self._trace is None: self._trace = self.load_trace(self.mname) return self._trace
python
def trace(self): """Initializes the trace data""" if self._trace is None: self._trace = self.load_trace(self.mname) return self._trace
[ "def", "trace", "(", "self", ")", ":", "if", "self", ".", "_trace", "is", "None", ":", "self", ".", "_trace", "=", "self", ".", "load_trace", "(", "self", ".", "mname", ")", "return", "self", ".", "_trace" ]
Initializes the trace data
[ "Initializes", "the", "trace", "data" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_trace.py#L58-L62
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_trace.py
TraceColumn.load_trace
def load_trace(mname): """Loads the traces and returns them as a dictionary Currently, only loading traces from tdms files is supported. This forces us to load the full tdms file into memory which takes some time. """ tname = TraceColumn.find_trace_file(mname) # Initialize empty trace dictionary trace = {} if tname is None: pass elif tname.suffix == ".tdms": # Again load the measurement tdms file. # This might increase memory usage, but it is cleaner # when looking at code structure. mdata = TdmsFile(str(mname)) sampleids = mdata.object("Cell Track", "FL1index").data # Load the trace data. The traces file is usually larger than the # measurement file. tdata = TdmsFile(str(tname)) for trace_key in dfn.FLUOR_TRACES: group, ch = naming.tr_data_map[trace_key] try: trdat = tdata.object(group, ch).data except KeyError: pass else: if trdat is not None and trdat.size != 0: # Only add trace if there is actual data. # Split only needs the position of the sections, # so we remove the first (0) index. trace[trace_key] = np.split(trdat, sampleids[1:]) return trace
python
def load_trace(mname): """Loads the traces and returns them as a dictionary Currently, only loading traces from tdms files is supported. This forces us to load the full tdms file into memory which takes some time. """ tname = TraceColumn.find_trace_file(mname) # Initialize empty trace dictionary trace = {} if tname is None: pass elif tname.suffix == ".tdms": # Again load the measurement tdms file. # This might increase memory usage, but it is cleaner # when looking at code structure. mdata = TdmsFile(str(mname)) sampleids = mdata.object("Cell Track", "FL1index").data # Load the trace data. The traces file is usually larger than the # measurement file. tdata = TdmsFile(str(tname)) for trace_key in dfn.FLUOR_TRACES: group, ch = naming.tr_data_map[trace_key] try: trdat = tdata.object(group, ch).data except KeyError: pass else: if trdat is not None and trdat.size != 0: # Only add trace if there is actual data. # Split only needs the position of the sections, # so we remove the first (0) index. trace[trace_key] = np.split(trdat, sampleids[1:]) return trace
[ "def", "load_trace", "(", "mname", ")", ":", "tname", "=", "TraceColumn", ".", "find_trace_file", "(", "mname", ")", "# Initialize empty trace dictionary", "trace", "=", "{", "}", "if", "tname", "is", "None", ":", "pass", "elif", "tname", ".", "suffix", "=="...
Loads the traces and returns them as a dictionary Currently, only loading traces from tdms files is supported. This forces us to load the full tdms file into memory which takes some time.
[ "Loads", "the", "traces", "and", "returns", "them", "as", "a", "dictionary" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_trace.py#L65-L101
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_trace.py
TraceColumn.find_trace_file
def find_trace_file(mname): """Tries to find the traces tdms file name Returns None if no trace file is found. """ mname = pathlib.Path(mname) tname = None if mname.exists(): cand = mname.with_name(mname.name[:-5] + "_traces.tdms") if cand.exists(): tname = cand return tname
python
def find_trace_file(mname): """Tries to find the traces tdms file name Returns None if no trace file is found. """ mname = pathlib.Path(mname) tname = None if mname.exists(): cand = mname.with_name(mname.name[:-5] + "_traces.tdms") if cand.exists(): tname = cand return tname
[ "def", "find_trace_file", "(", "mname", ")", ":", "mname", "=", "pathlib", ".", "Path", "(", "mname", ")", "tname", "=", "None", "if", "mname", ".", "exists", "(", ")", ":", "cand", "=", "mname", ".", "with_name", "(", "mname", ".", "name", "[", ":...
Tries to find the traces tdms file name Returns None if no trace file is found.
[ "Tries", "to", "find", "the", "traces", "tdms", "file", "name" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_trace.py#L104-L117
Fischerfredl/get-docker-secret
get_docker_secret.py
get_docker_secret
def get_docker_secret(name, default=None, cast_to=str, autocast_name=True, getenv=True, safe=True, secrets_dir=os.path.join(root, 'var', 'run', 'secrets')): """This function fetches a docker secret :param name: the name of the docker secret :param default: the default value if no secret found :param cast_to: casts the value to the given type :param autocast_name: whether the name should be lowercase for secrets and upper case for environment :param getenv: if environment variable should be fetched as fallback :param safe: Whether the function should raise exceptions :param secrets_dir: the directory where the secrets are stored :returns: docker secret or environment variable depending on params :raises TypeError: if cast fails due to wrong type (None) :raises ValueError: if casts fails due to Value """ # cast name if autocast enabled name_secret = name.lower() if autocast_name else name name_env = name.upper() if autocast_name else name # initiallize value value = None # try to read from secret file try: with open(os.path.join(secrets_dir, name_secret), 'r') as secret_file: value = secret_file.read() except IOError as e: # try to read from env if enabled if getenv: value = os.environ.get(name_env) # set default value if no value found if value is None: value = default # try to cast try: # so None wont be cast to 'None' if value is None: raise TypeError('value is None') # special case bool if cast_to == bool: if value not in ('True', 'true', 'False', 'false'): raise ValueError('value %s not of type bool' % value) value = 1 if value in ('True', 'true') else 0 # try to cast return cast_to(value) except (TypeError, ValueError) as e: # whether exception should be thrown if safe: return default raise e
python
def get_docker_secret(name, default=None, cast_to=str, autocast_name=True, getenv=True, safe=True, secrets_dir=os.path.join(root, 'var', 'run', 'secrets')): """This function fetches a docker secret :param name: the name of the docker secret :param default: the default value if no secret found :param cast_to: casts the value to the given type :param autocast_name: whether the name should be lowercase for secrets and upper case for environment :param getenv: if environment variable should be fetched as fallback :param safe: Whether the function should raise exceptions :param secrets_dir: the directory where the secrets are stored :returns: docker secret or environment variable depending on params :raises TypeError: if cast fails due to wrong type (None) :raises ValueError: if casts fails due to Value """ # cast name if autocast enabled name_secret = name.lower() if autocast_name else name name_env = name.upper() if autocast_name else name # initiallize value value = None # try to read from secret file try: with open(os.path.join(secrets_dir, name_secret), 'r') as secret_file: value = secret_file.read() except IOError as e: # try to read from env if enabled if getenv: value = os.environ.get(name_env) # set default value if no value found if value is None: value = default # try to cast try: # so None wont be cast to 'None' if value is None: raise TypeError('value is None') # special case bool if cast_to == bool: if value not in ('True', 'true', 'False', 'false'): raise ValueError('value %s not of type bool' % value) value = 1 if value in ('True', 'true') else 0 # try to cast return cast_to(value) except (TypeError, ValueError) as e: # whether exception should be thrown if safe: return default raise e
[ "def", "get_docker_secret", "(", "name", ",", "default", "=", "None", ",", "cast_to", "=", "str", ",", "autocast_name", "=", "True", ",", "getenv", "=", "True", ",", "safe", "=", "True", ",", "secrets_dir", "=", "os", ".", "path", ".", "join", "(", "...
This function fetches a docker secret :param name: the name of the docker secret :param default: the default value if no secret found :param cast_to: casts the value to the given type :param autocast_name: whether the name should be lowercase for secrets and upper case for environment :param getenv: if environment variable should be fetched as fallback :param safe: Whether the function should raise exceptions :param secrets_dir: the directory where the secrets are stored :returns: docker secret or environment variable depending on params :raises TypeError: if cast fails due to wrong type (None) :raises ValueError: if casts fails due to Value
[ "This", "function", "fetches", "a", "docker", "secret" ]
train
https://github.com/Fischerfredl/get-docker-secret/blob/1fa7f7e2d8b727fd95b6257041e0498fde2d3880/get_docker_secret.py#L6-L61
ZELLMECHANIK-DRESDEN/dclab
dclab/cached.py
Cache._update_hash
def _update_hash(self, arg): """ Takes an argument and updates the hash. The argument can be an np.array, string, or list of things that are convertable to strings. """ if isinstance(arg, np.ndarray): self.ahash.update(arg.view(np.uint8)) elif isinstance(arg, list): [self._update_hash(a) for a in arg] else: self.ahash.update(str(arg).encode('utf-8'))
python
def _update_hash(self, arg): """ Takes an argument and updates the hash. The argument can be an np.array, string, or list of things that are convertable to strings. """ if isinstance(arg, np.ndarray): self.ahash.update(arg.view(np.uint8)) elif isinstance(arg, list): [self._update_hash(a) for a in arg] else: self.ahash.update(str(arg).encode('utf-8'))
[ "def", "_update_hash", "(", "self", ",", "arg", ")", ":", "if", "isinstance", "(", "arg", ",", "np", ".", "ndarray", ")", ":", "self", ".", "ahash", ".", "update", "(", "arg", ".", "view", "(", "np", ".", "uint8", ")", ")", "elif", "isinstance", ...
Takes an argument and updates the hash. The argument can be an np.array, string, or list of things that are convertable to strings.
[ "Takes", "an", "argument", "and", "updates", "the", "hash", ".", "The", "argument", "can", "be", "an", "np", ".", "array", "string", "or", "list", "of", "things", "that", "are", "convertable", "to", "strings", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/cached.py#L81-L91
ZELLMECHANIK-DRESDEN/dclab
dclab/cached.py
Cache.clear_cache
def clear_cache(): """Remove all cached objects""" del Cache._keys for k in list(Cache._cache.keys()): it = Cache._cache.pop(k) del it del Cache._cache Cache._keys = [] Cache._cache = {} gc.collect()
python
def clear_cache(): """Remove all cached objects""" del Cache._keys for k in list(Cache._cache.keys()): it = Cache._cache.pop(k) del it del Cache._cache Cache._keys = [] Cache._cache = {} gc.collect()
[ "def", "clear_cache", "(", ")", ":", "del", "Cache", ".", "_keys", "for", "k", "in", "list", "(", "Cache", ".", "_cache", ".", "keys", "(", ")", ")", ":", "it", "=", "Cache", ".", "_cache", ".", "pop", "(", "k", ")", "del", "it", "del", "Cache"...
Remove all cached objects
[ "Remove", "all", "cached", "objects" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/cached.py#L94-L103
robmcmullen/atrcopy
atrcopy/diskimages.py
BaseHeader.get_pos
def get_pos(self, sector): """Get index (into the raw data of the disk image) of start of sector This base class method assumes the sectors are one after another, in order starting from the beginning of the raw data. """ if not self.sector_is_valid(sector): raise ByteNotInFile166("Sector %d out of range" % sector) pos = sector * self.sector_size + self.header_offset size = self.sector_size return pos, size
python
def get_pos(self, sector): """Get index (into the raw data of the disk image) of start of sector This base class method assumes the sectors are one after another, in order starting from the beginning of the raw data. """ if not self.sector_is_valid(sector): raise ByteNotInFile166("Sector %d out of range" % sector) pos = sector * self.sector_size + self.header_offset size = self.sector_size return pos, size
[ "def", "get_pos", "(", "self", ",", "sector", ")", ":", "if", "not", "self", ".", "sector_is_valid", "(", "sector", ")", ":", "raise", "ByteNotInFile166", "(", "\"Sector %d out of range\"", "%", "sector", ")", "pos", "=", "sector", "*", "self", ".", "secto...
Get index (into the raw data of the disk image) of start of sector This base class method assumes the sectors are one after another, in order starting from the beginning of the raw data.
[ "Get", "index", "(", "into", "the", "raw", "data", "of", "the", "disk", "image", ")", "of", "start", "of", "sector" ]
train
https://github.com/robmcmullen/atrcopy/blob/dafba8e74c718e95cf81fd72c184fa193ecec730/atrcopy/diskimages.py#L64-L74
robmcmullen/atrcopy
atrcopy/diskimages.py
DiskImageBase.get_sector_slice
def get_sector_slice(self, start, end=None): """ Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes """ pos, size = self.header.get_pos(start) if end is None: end = start while start < end: start += 1 _, more = self.header.get_pos(start) size += more return slice(pos, pos + size)
python
def get_sector_slice(self, start, end=None): """ Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes """ pos, size = self.header.get_pos(start) if end is None: end = start while start < end: start += 1 _, more = self.header.get_pos(start) size += more return slice(pos, pos + size)
[ "def", "get_sector_slice", "(", "self", ",", "start", ",", "end", "=", "None", ")", ":", "pos", ",", "size", "=", "self", ".", "header", ".", "get_pos", "(", "start", ")", "if", "end", "is", "None", ":", "end", "=", "start", "while", "start", "<", ...
Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes
[ "Get", "contiguous", "sectors", ":", "param", "start", ":", "first", "sector", "number", "to", "read", "(", "note", ":", "numbering", "starts", "from", "1", ")", ":", "param", "end", ":", "last", "sector", "number", "to", "read", ":", "returns", ":", "...
train
https://github.com/robmcmullen/atrcopy/blob/dafba8e74c718e95cf81fd72c184fa193ecec730/atrcopy/diskimages.py#L225-L239
robmcmullen/atrcopy
atrcopy/diskimages.py
DiskImageBase.get_sectors
def get_sectors(self, start, end=None): """ Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes """ s = self.get_sector_slice(start, end) return self.bytes[s], self.style[s]
python
def get_sectors(self, start, end=None): """ Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes """ s = self.get_sector_slice(start, end) return self.bytes[s], self.style[s]
[ "def", "get_sectors", "(", "self", ",", "start", ",", "end", "=", "None", ")", ":", "s", "=", "self", ".", "get_sector_slice", "(", "start", ",", "end", ")", "return", "self", ".", "bytes", "[", "s", "]", ",", "self", ".", "style", "[", "s", "]" ...
Get contiguous sectors :param start: first sector number to read (note: numbering starts from 1) :param end: last sector number to read :returns: bytes
[ "Get", "contiguous", "sectors", ":", "param", "start", ":", "first", "sector", "number", "to", "read", "(", "note", ":", "numbering", "starts", "from", "1", ")", ":", "param", "end", ":", "last", "sector", "number", "to", "read", ":", "returns", ":", "...
train
https://github.com/robmcmullen/atrcopy/blob/dafba8e74c718e95cf81fd72c184fa193ecec730/atrcopy/diskimages.py#L241-L249
robmcmullen/atrcopy
atrcopy/diskimages.py
DiskImageBase.write_file
def write_file(self, filename, filetype, data): """Write data to a file on disk This throws various exceptions on failures, for instance if there is not enough space on disk or a free entry is not available in the catalog. """ state = self.begin_transaction() try: directory = self.directory_class(self.header) self.get_directory(directory) dirent = directory.add_dirent(filename, filetype) data = to_numpy(data) sector_list = self.build_sectors(data) vtoc = self.get_vtoc_object() directory.save_dirent(self, dirent, vtoc, sector_list) self.write_sector_list(sector_list) self.write_sector_list(vtoc) self.write_sector_list(directory) except errors.AtrError: self.rollback_transaction(state) raise finally: self.get_metadata()
python
def write_file(self, filename, filetype, data): """Write data to a file on disk This throws various exceptions on failures, for instance if there is not enough space on disk or a free entry is not available in the catalog. """ state = self.begin_transaction() try: directory = self.directory_class(self.header) self.get_directory(directory) dirent = directory.add_dirent(filename, filetype) data = to_numpy(data) sector_list = self.build_sectors(data) vtoc = self.get_vtoc_object() directory.save_dirent(self, dirent, vtoc, sector_list) self.write_sector_list(sector_list) self.write_sector_list(vtoc) self.write_sector_list(directory) except errors.AtrError: self.rollback_transaction(state) raise finally: self.get_metadata()
[ "def", "write_file", "(", "self", ",", "filename", ",", "filetype", ",", "data", ")", ":", "state", "=", "self", ".", "begin_transaction", "(", ")", "try", ":", "directory", "=", "self", ".", "directory_class", "(", "self", ".", "header", ")", "self", ...
Write data to a file on disk This throws various exceptions on failures, for instance if there is not enough space on disk or a free entry is not available in the catalog.
[ "Write", "data", "to", "a", "file", "on", "disk" ]
train
https://github.com/robmcmullen/atrcopy/blob/dafba8e74c718e95cf81fd72c184fa193ecec730/atrcopy/diskimages.py#L337-L360
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase._apply_scale
def _apply_scale(self, a, scale, feat): """Helper function for transforming an aray to log-scale Parameters ---------- a: np.ndarray Input array scale: If set to "log", take the logarithm of `a`; if set to "linear" return `a` unchanged. Returns ------- b: np.ndarray The scaled array Notes ----- If the scale is not "linear", then a new array is returned. All warnings are suppressed when computing `np.log(a)`, as `a` may have negative or nan values. """ if scale == "linear": b = a elif scale == "log": with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") b = np.log(a) if len(w): # Tell the user that the log-transformation issued # a warning. warnings.warn("Invalid values encounterd in np.log " "while scaling feature '{}'!".format(feat)) else: raise ValueError("`scale` must be either 'linear' or 'log', " + "got '{}'!".format(scale)) return b
python
def _apply_scale(self, a, scale, feat): """Helper function for transforming an aray to log-scale Parameters ---------- a: np.ndarray Input array scale: If set to "log", take the logarithm of `a`; if set to "linear" return `a` unchanged. Returns ------- b: np.ndarray The scaled array Notes ----- If the scale is not "linear", then a new array is returned. All warnings are suppressed when computing `np.log(a)`, as `a` may have negative or nan values. """ if scale == "linear": b = a elif scale == "log": with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") b = np.log(a) if len(w): # Tell the user that the log-transformation issued # a warning. warnings.warn("Invalid values encounterd in np.log " "while scaling feature '{}'!".format(feat)) else: raise ValueError("`scale` must be either 'linear' or 'log', " + "got '{}'!".format(scale)) return b
[ "def", "_apply_scale", "(", "self", ",", "a", ",", "scale", ",", "feat", ")", ":", "if", "scale", "==", "\"linear\"", ":", "b", "=", "a", "elif", "scale", "==", "\"log\"", ":", "with", "warnings", ".", "catch_warnings", "(", "record", "=", "True", ")...
Helper function for transforming an aray to log-scale Parameters ---------- a: np.ndarray Input array scale: If set to "log", take the logarithm of `a`; if set to "linear" return `a` unchanged. Returns ------- b: np.ndarray The scaled array Notes ----- If the scale is not "linear", then a new array is returned. All warnings are suppressed when computing `np.log(a)`, as `a` may have negative or nan values.
[ "Helper", "function", "for", "transforming", "an", "aray", "to", "log", "-", "scale" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L148-L184
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.features
def features(self): """All available features""" mycols = [] for col in dfn.feature_names: if col in self: mycols.append(col) mycols.sort() return mycols
python
def features(self): """All available features""" mycols = [] for col in dfn.feature_names: if col in self: mycols.append(col) mycols.sort() return mycols
[ "def", "features", "(", "self", ")", ":", "mycols", "=", "[", "]", "for", "col", "in", "dfn", ".", "feature_names", ":", "if", "col", "in", "self", ":", "mycols", ".", "append", "(", "col", ")", "mycols", ".", "sort", "(", ")", "return", "mycols" ]
All available features
[ "All", "available", "features" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L206-L213
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.get_downsampled_scatter
def get_downsampled_scatter(self, xax="area_um", yax="deform", downsample=0, xscale="linear", yscale="linear"): """Downsampling by removing points at dense locations Parameters ---------- xax: str Identifier for x axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for y axis downsample: int Number of points to draw in the down-sampled plot. This number is either - >=1: exactly downsample to this number by randomly adding or removing points - 0 : do not perform downsampling xscale: str If set to "log", take the logarithm of the x-values before performing downsampling. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- xnew, xnew: filtered x and y """ if downsample < 0: raise ValueError("`downsample` must be zero or positive!") downsample = int(downsample) xax = xax.lower() yax = yax.lower() # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) _, _, idx = downsampling.downsample_grid(xs, ys, samples=downsample, ret_idx=True) self._plot_filter = idx return x[idx], y[idx]
python
def get_downsampled_scatter(self, xax="area_um", yax="deform", downsample=0, xscale="linear", yscale="linear"): """Downsampling by removing points at dense locations Parameters ---------- xax: str Identifier for x axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for y axis downsample: int Number of points to draw in the down-sampled plot. This number is either - >=1: exactly downsample to this number by randomly adding or removing points - 0 : do not perform downsampling xscale: str If set to "log", take the logarithm of the x-values before performing downsampling. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- xnew, xnew: filtered x and y """ if downsample < 0: raise ValueError("`downsample` must be zero or positive!") downsample = int(downsample) xax = xax.lower() yax = yax.lower() # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) _, _, idx = downsampling.downsample_grid(xs, ys, samples=downsample, ret_idx=True) self._plot_filter = idx return x[idx], y[idx]
[ "def", "get_downsampled_scatter", "(", "self", ",", "xax", "=", "\"area_um\"", ",", "yax", "=", "\"deform\"", ",", "downsample", "=", "0", ",", "xscale", "=", "\"linear\"", ",", "yscale", "=", "\"linear\"", ")", ":", "if", "downsample", "<", "0", ":", "r...
Downsampling by removing points at dense locations Parameters ---------- xax: str Identifier for x axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for y axis downsample: int Number of points to draw in the down-sampled plot. This number is either - >=1: exactly downsample to this number by randomly adding or removing points - 0 : do not perform downsampling xscale: str If set to "log", take the logarithm of the x-values before performing downsampling. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- xnew, xnew: filtered x and y
[ "Downsampling", "by", "removing", "points", "at", "dense", "locations" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L223-L271
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.get_kde_contour
def get_kde_contour(self, xax="area_um", yax="deform", xacc=None, yacc=None, kde_type="histogram", kde_kwargs={}, xscale="linear", yscale="linear"): """Evaluate the kernel density estimate for contour plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis xacc: float Contour accuracy in x direction yacc: float Contour accuracy in y direction kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- X, Y, Z : coordinates The kernel density Z evaluated on a rectangular grid (X,Y). """ xax = xax.lower() yax = yax.lower() kde_type = kde_type.lower() if kde_type not in kde_methods.methods: raise ValueError("Not a valid kde type: {}!".format(kde_type)) # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) # accuracy (bin width) of KDE estimator if xacc is None: xacc = kde_methods.bin_width_doane(xs) / 5 if yacc is None: yacc = kde_methods.bin_width_doane(ys) / 5 # Ignore infs and nans bad = kde_methods.get_bad_vals(xs, ys) xc = xs[~bad] yc = ys[~bad] xnum = int(np.ceil((xc.max() - xc.min()) / xacc)) ynum = int(np.ceil((yc.max() - yc.min()) / yacc)) xlin = np.linspace(xc.min(), xc.max(), xnum, endpoint=True) ylin = np.linspace(yc.min(), yc.max(), ynum, endpoint=True) xmesh, ymesh = np.meshgrid(xlin, ylin, indexing="ij") kde_fct = kde_methods.methods[kde_type] if len(x): density = kde_fct(events_x=xs, events_y=ys, xout=xmesh, yout=ymesh, **kde_kwargs) else: density = [] # Convert mesh back to linear scale if applicable if xscale == "log": xmesh = np.exp(xmesh) if yscale == "log": ymesh = np.exp(ymesh) return xmesh, ymesh, density
python
def get_kde_contour(self, xax="area_um", yax="deform", xacc=None, yacc=None, kde_type="histogram", kde_kwargs={}, xscale="linear", yscale="linear"): """Evaluate the kernel density estimate for contour plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis xacc: float Contour accuracy in x direction yacc: float Contour accuracy in y direction kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- X, Y, Z : coordinates The kernel density Z evaluated on a rectangular grid (X,Y). """ xax = xax.lower() yax = yax.lower() kde_type = kde_type.lower() if kde_type not in kde_methods.methods: raise ValueError("Not a valid kde type: {}!".format(kde_type)) # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) # accuracy (bin width) of KDE estimator if xacc is None: xacc = kde_methods.bin_width_doane(xs) / 5 if yacc is None: yacc = kde_methods.bin_width_doane(ys) / 5 # Ignore infs and nans bad = kde_methods.get_bad_vals(xs, ys) xc = xs[~bad] yc = ys[~bad] xnum = int(np.ceil((xc.max() - xc.min()) / xacc)) ynum = int(np.ceil((yc.max() - yc.min()) / yacc)) xlin = np.linspace(xc.min(), xc.max(), xnum, endpoint=True) ylin = np.linspace(yc.min(), yc.max(), ynum, endpoint=True) xmesh, ymesh = np.meshgrid(xlin, ylin, indexing="ij") kde_fct = kde_methods.methods[kde_type] if len(x): density = kde_fct(events_x=xs, events_y=ys, xout=xmesh, yout=ymesh, **kde_kwargs) else: density = [] # Convert mesh back to linear scale if applicable if xscale == "log": xmesh = np.exp(xmesh) if yscale == "log": ymesh = np.exp(ymesh) return xmesh, ymesh, density
[ "def", "get_kde_contour", "(", "self", ",", "xax", "=", "\"area_um\"", ",", "yax", "=", "\"deform\"", ",", "xacc", "=", "None", ",", "yacc", "=", "None", ",", "kde_type", "=", "\"histogram\"", ",", "kde_kwargs", "=", "{", "}", ",", "xscale", "=", "\"li...
Evaluate the kernel density estimate for contour plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis xacc: float Contour accuracy in x direction yacc: float Contour accuracy in y direction kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- X, Y, Z : coordinates The kernel density Z evaluated on a rectangular grid (X,Y).
[ "Evaluate", "the", "kernel", "density", "estimate", "for", "contour", "plots" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L273-L351
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.get_kde_scatter
def get_kde_scatter(self, xax="area_um", yax="deform", positions=None, kde_type="histogram", kde_kwargs={}, xscale="linear", yscale="linear"): """Evaluate the kernel density estimate for scatter plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis positions: list of two 1d ndarrays or ndarray of shape (2, N) The positions where the KDE will be computed. Note that the KDE estimate is computed from the the points that are set in `self.filter.all`. kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- density : 1d ndarray The kernel density evaluated for the filtered data points. """ xax = xax.lower() yax = yax.lower() kde_type = kde_type.lower() if kde_type not in kde_methods.methods: raise ValueError("Not a valid kde type: {}!".format(kde_type)) # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) if positions is None: posx = None posy = None else: posx = self._apply_scale(positions[0], xscale, xax) posy = self._apply_scale(positions[1], yscale, yax) kde_fct = kde_methods.methods[kde_type] if len(x): density = kde_fct(events_x=xs, events_y=ys, xout=posx, yout=posy, **kde_kwargs) else: density = [] return density
python
def get_kde_scatter(self, xax="area_um", yax="deform", positions=None, kde_type="histogram", kde_kwargs={}, xscale="linear", yscale="linear"): """Evaluate the kernel density estimate for scatter plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis positions: list of two 1d ndarrays or ndarray of shape (2, N) The positions where the KDE will be computed. Note that the KDE estimate is computed from the the points that are set in `self.filter.all`. kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- density : 1d ndarray The kernel density evaluated for the filtered data points. """ xax = xax.lower() yax = yax.lower() kde_type = kde_type.lower() if kde_type not in kde_methods.methods: raise ValueError("Not a valid kde type: {}!".format(kde_type)) # Get data x = self[xax][self.filter.all] y = self[yax][self.filter.all] # Apply scale (no change for linear scale) xs = self._apply_scale(x, xscale, xax) ys = self._apply_scale(y, yscale, yax) if positions is None: posx = None posy = None else: posx = self._apply_scale(positions[0], xscale, xax) posy = self._apply_scale(positions[1], yscale, yax) kde_fct = kde_methods.methods[kde_type] if len(x): density = kde_fct(events_x=xs, events_y=ys, xout=posx, yout=posy, **kde_kwargs) else: density = [] return density
[ "def", "get_kde_scatter", "(", "self", ",", "xax", "=", "\"area_um\"", ",", "yax", "=", "\"deform\"", ",", "positions", "=", "None", ",", "kde_type", "=", "\"histogram\"", ",", "kde_kwargs", "=", "{", "}", ",", "xscale", "=", "\"linear\"", ",", "yscale", ...
Evaluate the kernel density estimate for scatter plots Parameters ---------- xax: str Identifier for X axis (e.g. "area_um", "aspect", "deform") yax: str Identifier for Y axis positions: list of two 1d ndarrays or ndarray of shape (2, N) The positions where the KDE will be computed. Note that the KDE estimate is computed from the the points that are set in `self.filter.all`. kde_type: str The KDE method to use kde_kwargs: dict Additional keyword arguments to the KDE method xscale: str If set to "log", take the logarithm of the x-values before computing the KDE. This is useful when data are are displayed on a log-scale. Defaults to "linear". yscale: str See `xscale`. Returns ------- density : 1d ndarray The kernel density evaluated for the filtered data points.
[ "Evaluate", "the", "kernel", "density", "estimate", "for", "scatter", "plots" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L353-L413
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.polygon_filter_add
def polygon_filter_add(self, filt): """Associate a Polygon Filter with this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to add """ if not isinstance(filt, (PolygonFilter, int, float)): msg = "`filt` must be a number or instance of PolygonFilter!" raise ValueError(msg) if isinstance(filt, PolygonFilter): uid = filt.unique_id else: uid = int(filt) # append item self.config["filtering"]["polygon filters"].append(uid)
python
def polygon_filter_add(self, filt): """Associate a Polygon Filter with this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to add """ if not isinstance(filt, (PolygonFilter, int, float)): msg = "`filt` must be a number or instance of PolygonFilter!" raise ValueError(msg) if isinstance(filt, PolygonFilter): uid = filt.unique_id else: uid = int(filt) # append item self.config["filtering"]["polygon filters"].append(uid)
[ "def", "polygon_filter_add", "(", "self", ",", "filt", ")", ":", "if", "not", "isinstance", "(", "filt", ",", "(", "PolygonFilter", ",", "int", ",", "float", ")", ")", ":", "msg", "=", "\"`filt` must be a number or instance of PolygonFilter!\"", "raise", "ValueE...
Associate a Polygon Filter with this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to add
[ "Associate", "a", "Polygon", "Filter", "with", "this", "instance" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L415-L432
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/core.py
RTDCBase.polygon_filter_rm
def polygon_filter_rm(self, filt): """Remove a polygon filter from this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to remove """ if not isinstance(filt, (PolygonFilter, int, float)): msg = "`filt` must be a number or instance of PolygonFilter!" raise ValueError(msg) if isinstance(filt, PolygonFilter): uid = filt.unique_id else: uid = int(filt) # remove item self.config["filtering"]["polygon filters"].remove(uid)
python
def polygon_filter_rm(self, filt): """Remove a polygon filter from this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to remove """ if not isinstance(filt, (PolygonFilter, int, float)): msg = "`filt` must be a number or instance of PolygonFilter!" raise ValueError(msg) if isinstance(filt, PolygonFilter): uid = filt.unique_id else: uid = int(filt) # remove item self.config["filtering"]["polygon filters"].remove(uid)
[ "def", "polygon_filter_rm", "(", "self", ",", "filt", ")", ":", "if", "not", "isinstance", "(", "filt", ",", "(", "PolygonFilter", ",", "int", ",", "float", ")", ")", ":", "msg", "=", "\"`filt` must be a number or instance of PolygonFilter!\"", "raise", "ValueEr...
Remove a polygon filter from this instance Parameters ---------- filt: int or instance of `PolygonFilter` The polygon filter to remove
[ "Remove", "a", "polygon", "filter", "from", "this", "instance" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/core.py#L434-L451
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/load.py
check_dataset
def check_dataset(path_or_ds): """Check whether a dataset is complete Parameters ---------- path_or_ds: str or RTDCBase Full path to a dataset on disk or an instance of RTDCBase Returns ------- violations: list of str Dataset format violations (hard) alerts: list of str Dataset format alerts (soft) info: list of str Dataset information """ aler = [] info = [] viol = [] if isinstance(path_or_ds, RTDCBase): ds = path_or_ds else: ds = load_file(path_or_ds) # check for meta data types for sec in ds.config: for key in ds.config[sec]: if sec in dfn.CFG_ANALYSIS: # TODO: # - properly test against analysis keywords # (filtering, calculation) pass elif (sec not in dfn.config_keys or key not in dfn.config_keys[sec]): viol.append("Metadata: Unknown key [{}] '{}'".format(sec, key)) elif not isinstance(ds.config[sec][key], dfn.config_types[sec][key]): viol.append("Metadata: Datatype of [{}] '{}'".format(sec, key) + "must be '{}'".format(dfn.config_types[sec][key]) ) # check existence of meta data keys # These "must" be present: tocheck = IMPORTANT_KEYS # These sections "should" be fully present tocheck_sec_aler = ["experiment", "imaging", "online_contour", "setup"] # should we also check for fluorescence keys? if ("fluorescence" in ds.config or "fl1_max" in ds._events or "fl2_max" in ds._events or "fl3_max" in ds._events): info.append("Fluorescence: True") tocheck = tocheck.copy() tocheck.update(IMPORTANT_KEYS_FL) # check for number of channels if "channel count" in ds.config["fluorescence"]: chc1 = ds.config["fluorescence"]["channel count"] chc2 = 0 for ii in range(1, 4): chn = "channel {} name".format(ii) ecn = "fl{}_max".format(ii) if (chn in ds.config["fluorescence"] and ecn in ds._events): chc2 += 1 if chc1 != chc2: msg = "Metadata: fluorescence channel count inconsistent" viol.append(msg) # check for number of lasers if "laser count" in ds.config["fluorescence"]: lsc1 = ds.config["fluorescence"]["laser count"] lsc2 = 0 for ii in range(1, 4): kl = "laser {} lambda".format(ii) kp = "laser {} power".format(ii) if (kl in ds.config["fluorescence"] and kp in ds.config["fluorescence"]): lsc2 += 1 if lsc1 != lsc2: msg = "Metadata: fluorescence laser count inconsistent" viol.append(msg) # check for samples per event if "samples per event" in ds.config["fluorescence"]: spe = ds.config["fluorescence"]["samples per event"] for key in ds["trace"].keys(): spek = ds["trace"][key][0].size if spek != spe: msg = "Metadata: wrong number of samples per event: " \ + "{} (expected {}, got {}".format(key, spe, spek) viol.append(msg) else: info.append("Fluorescence: False") # search for missing keys (hard) for sec in tocheck: if sec not in ds.config: viol.append("Metadata: Missing section '{}'".format(sec)) else: for key in dfn.config_keys[sec]: if (key in tocheck[sec] and key not in ds.config[sec]): viol.append("Metadata: Missing key [{}] '{}'".format(sec, key)) elif (sec in tocheck_sec_aler and key not in ds.config[sec]): # Note: fluorescence is not treated here. It can be # incomplete (e.g. number of channels installed may vary) aler.append("Metadata: Missing key [{}] '{}'".format(sec, key)) # search again (soft) for sec in tocheck_sec_aler: if sec in tocheck: # already treated above (hard) continue if sec not in ds.config: aler.append("Metadata: Missing section '{}'".format(sec)) else: for key in dfn.config_keys[sec]: if key not in ds.config[sec]: aler.append("Metadata: Missing key [{}] '{}'".format(sec, key)) # check for medium if "medium" in ds.config["setup"]: med = ds.config["setup"]["medium"] if med not in ["CellCarrier", "CellCarrierB", "water", "other"]: msg = "Metadata: Invalid value [setup] medium: '{}'".format(med) viol.append(msg) # check for feature column names for feat in ds._events.keys(): if feat not in dfn.feature_names: viol.append("Features: Unknown key '{}'".format(feat)) info.append("Data file format: {}".format(ds.format)) # hdf5-based checks if ds.format == "hdf5": # check meta data of images if "image" in ds._events: imdat = ds["image"] for key, val in [['CLASS', b'IMAGE'], ['IMAGE_VERSION', b'1.2'], ['IMAGE_SUBCLASS', b'IMAGE_GRAYSCALE']]: if key not in imdat.attrs: aler.append("HDF5: '/image': missing attribute " + "'{}'".format(key)) elif not isinstance(imdat.attrs[key], bytes): aler.append("HDF5: '/image': attribute '{}' ".format(key) + "should be fixed-length ASCII string") elif imdat.attrs[key] != val: aler.append("HDF5: '/image': attribute '{}' ".format(key) + "should have value '{}'".format(val)) # check length of logs with h5py.File(ds.path, mode="r") as h5: logs = h5["logs"] for logname in logs.keys(): log = logs[logname] for ii in range(len(log)): if len(log[ii]) > LOG_MAX_LINE_LENGTH: aler.append("Logs: {} line {} ".format(logname, ii) + "exceeds maximum line length " + "{}".format(LOG_MAX_LINE_LENGTH)) return sorted(viol), sorted(aler), sorted(info)
python
def check_dataset(path_or_ds): """Check whether a dataset is complete Parameters ---------- path_or_ds: str or RTDCBase Full path to a dataset on disk or an instance of RTDCBase Returns ------- violations: list of str Dataset format violations (hard) alerts: list of str Dataset format alerts (soft) info: list of str Dataset information """ aler = [] info = [] viol = [] if isinstance(path_or_ds, RTDCBase): ds = path_or_ds else: ds = load_file(path_or_ds) # check for meta data types for sec in ds.config: for key in ds.config[sec]: if sec in dfn.CFG_ANALYSIS: # TODO: # - properly test against analysis keywords # (filtering, calculation) pass elif (sec not in dfn.config_keys or key not in dfn.config_keys[sec]): viol.append("Metadata: Unknown key [{}] '{}'".format(sec, key)) elif not isinstance(ds.config[sec][key], dfn.config_types[sec][key]): viol.append("Metadata: Datatype of [{}] '{}'".format(sec, key) + "must be '{}'".format(dfn.config_types[sec][key]) ) # check existence of meta data keys # These "must" be present: tocheck = IMPORTANT_KEYS # These sections "should" be fully present tocheck_sec_aler = ["experiment", "imaging", "online_contour", "setup"] # should we also check for fluorescence keys? if ("fluorescence" in ds.config or "fl1_max" in ds._events or "fl2_max" in ds._events or "fl3_max" in ds._events): info.append("Fluorescence: True") tocheck = tocheck.copy() tocheck.update(IMPORTANT_KEYS_FL) # check for number of channels if "channel count" in ds.config["fluorescence"]: chc1 = ds.config["fluorescence"]["channel count"] chc2 = 0 for ii in range(1, 4): chn = "channel {} name".format(ii) ecn = "fl{}_max".format(ii) if (chn in ds.config["fluorescence"] and ecn in ds._events): chc2 += 1 if chc1 != chc2: msg = "Metadata: fluorescence channel count inconsistent" viol.append(msg) # check for number of lasers if "laser count" in ds.config["fluorescence"]: lsc1 = ds.config["fluorescence"]["laser count"] lsc2 = 0 for ii in range(1, 4): kl = "laser {} lambda".format(ii) kp = "laser {} power".format(ii) if (kl in ds.config["fluorescence"] and kp in ds.config["fluorescence"]): lsc2 += 1 if lsc1 != lsc2: msg = "Metadata: fluorescence laser count inconsistent" viol.append(msg) # check for samples per event if "samples per event" in ds.config["fluorescence"]: spe = ds.config["fluorescence"]["samples per event"] for key in ds["trace"].keys(): spek = ds["trace"][key][0].size if spek != spe: msg = "Metadata: wrong number of samples per event: " \ + "{} (expected {}, got {}".format(key, spe, spek) viol.append(msg) else: info.append("Fluorescence: False") # search for missing keys (hard) for sec in tocheck: if sec not in ds.config: viol.append("Metadata: Missing section '{}'".format(sec)) else: for key in dfn.config_keys[sec]: if (key in tocheck[sec] and key not in ds.config[sec]): viol.append("Metadata: Missing key [{}] '{}'".format(sec, key)) elif (sec in tocheck_sec_aler and key not in ds.config[sec]): # Note: fluorescence is not treated here. It can be # incomplete (e.g. number of channels installed may vary) aler.append("Metadata: Missing key [{}] '{}'".format(sec, key)) # search again (soft) for sec in tocheck_sec_aler: if sec in tocheck: # already treated above (hard) continue if sec not in ds.config: aler.append("Metadata: Missing section '{}'".format(sec)) else: for key in dfn.config_keys[sec]: if key not in ds.config[sec]: aler.append("Metadata: Missing key [{}] '{}'".format(sec, key)) # check for medium if "medium" in ds.config["setup"]: med = ds.config["setup"]["medium"] if med not in ["CellCarrier", "CellCarrierB", "water", "other"]: msg = "Metadata: Invalid value [setup] medium: '{}'".format(med) viol.append(msg) # check for feature column names for feat in ds._events.keys(): if feat not in dfn.feature_names: viol.append("Features: Unknown key '{}'".format(feat)) info.append("Data file format: {}".format(ds.format)) # hdf5-based checks if ds.format == "hdf5": # check meta data of images if "image" in ds._events: imdat = ds["image"] for key, val in [['CLASS', b'IMAGE'], ['IMAGE_VERSION', b'1.2'], ['IMAGE_SUBCLASS', b'IMAGE_GRAYSCALE']]: if key not in imdat.attrs: aler.append("HDF5: '/image': missing attribute " + "'{}'".format(key)) elif not isinstance(imdat.attrs[key], bytes): aler.append("HDF5: '/image': attribute '{}' ".format(key) + "should be fixed-length ASCII string") elif imdat.attrs[key] != val: aler.append("HDF5: '/image': attribute '{}' ".format(key) + "should have value '{}'".format(val)) # check length of logs with h5py.File(ds.path, mode="r") as h5: logs = h5["logs"] for logname in logs.keys(): log = logs[logname] for ii in range(len(log)): if len(log[ii]) > LOG_MAX_LINE_LENGTH: aler.append("Logs: {} line {} ".format(logname, ii) + "exceeds maximum line length " + "{}".format(LOG_MAX_LINE_LENGTH)) return sorted(viol), sorted(aler), sorted(info)
[ "def", "check_dataset", "(", "path_or_ds", ")", ":", "aler", "=", "[", "]", "info", "=", "[", "]", "viol", "=", "[", "]", "if", "isinstance", "(", "path_or_ds", ",", "RTDCBase", ")", ":", "ds", "=", "path_or_ds", "else", ":", "ds", "=", "load_file", ...
Check whether a dataset is complete Parameters ---------- path_or_ds: str or RTDCBase Full path to a dataset on disk or an instance of RTDCBase Returns ------- violations: list of str Dataset format violations (hard) alerts: list of str Dataset format alerts (soft) info: list of str Dataset information
[ "Check", "whether", "a", "dataset", "is", "complete" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/load.py#L60-L216
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/load.py
new_dataset
def new_dataset(data, identifier=None): """Initialize a new RT-DC dataset Parameters ---------- data: can be one of the following: - dict - .tdms file - .rtdc file - subclass of `RTDCBase` (will create a hierarchy child) identifier: str A unique identifier for this dataset. If set to `None` an identifier is generated. Returns ------- dataset: subclass of :class:`dclab.rtdc_dataset.RTDCBase` A new dataset instance """ if isinstance(data, dict): return fmt_dict.RTDC_Dict(data, identifier=identifier) elif isinstance(data, (str_types)) or isinstance(data, pathlib.Path): return load_file(data, identifier=identifier) elif isinstance(data, RTDCBase): return fmt_hierarchy.RTDC_Hierarchy(data, identifier=identifier) else: msg = "data type not supported: {}".format(data.__class__) raise NotImplementedError(msg)
python
def new_dataset(data, identifier=None): """Initialize a new RT-DC dataset Parameters ---------- data: can be one of the following: - dict - .tdms file - .rtdc file - subclass of `RTDCBase` (will create a hierarchy child) identifier: str A unique identifier for this dataset. If set to `None` an identifier is generated. Returns ------- dataset: subclass of :class:`dclab.rtdc_dataset.RTDCBase` A new dataset instance """ if isinstance(data, dict): return fmt_dict.RTDC_Dict(data, identifier=identifier) elif isinstance(data, (str_types)) or isinstance(data, pathlib.Path): return load_file(data, identifier=identifier) elif isinstance(data, RTDCBase): return fmt_hierarchy.RTDC_Hierarchy(data, identifier=identifier) else: msg = "data type not supported: {}".format(data.__class__) raise NotImplementedError(msg)
[ "def", "new_dataset", "(", "data", ",", "identifier", "=", "None", ")", ":", "if", "isinstance", "(", "data", ",", "dict", ")", ":", "return", "fmt_dict", ".", "RTDC_Dict", "(", "data", ",", "identifier", "=", "identifier", ")", "elif", "isinstance", "("...
Initialize a new RT-DC dataset Parameters ---------- data: can be one of the following: - dict - .tdms file - .rtdc file - subclass of `RTDCBase` (will create a hierarchy child) identifier: str A unique identifier for this dataset. If set to `None` an identifier is generated. Returns ------- dataset: subclass of :class:`dclab.rtdc_dataset.RTDCBase` A new dataset instance
[ "Initialize", "a", "new", "RT", "-", "DC", "dataset" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/load.py#L229-L259
openstax/cnx-archive
cnxarchive/scripts/hits_counter.py
parse_log
def parse_log(log, url_pattern): """Parse ``log`` buffer based on ``url_pattern``. Given a buffer as ``log``, parse the log buffer into a mapping of ident-hashes to a hit count, the timestamp of the initial log, and the last timestamp in the log. """ hits = {} initial_timestamp = None def clean_timestamp(v): return ' '.join(v).strip('[]') for line in log: data = line.split() if not initial_timestamp: initial_timestamp = clean_timestamp(data[3:5]) match = url_pattern.match(data[6]) if match: ident_hash = '@'.join(match.groups()) if ident_hash: hits[ident_hash] = hits.get(ident_hash, 0) + 1 else: end_timestamp = clean_timestamp(data[3:5]) return hits, initial_timestamp, end_timestamp
python
def parse_log(log, url_pattern): """Parse ``log`` buffer based on ``url_pattern``. Given a buffer as ``log``, parse the log buffer into a mapping of ident-hashes to a hit count, the timestamp of the initial log, and the last timestamp in the log. """ hits = {} initial_timestamp = None def clean_timestamp(v): return ' '.join(v).strip('[]') for line in log: data = line.split() if not initial_timestamp: initial_timestamp = clean_timestamp(data[3:5]) match = url_pattern.match(data[6]) if match: ident_hash = '@'.join(match.groups()) if ident_hash: hits[ident_hash] = hits.get(ident_hash, 0) + 1 else: end_timestamp = clean_timestamp(data[3:5]) return hits, initial_timestamp, end_timestamp
[ "def", "parse_log", "(", "log", ",", "url_pattern", ")", ":", "hits", "=", "{", "}", "initial_timestamp", "=", "None", "def", "clean_timestamp", "(", "v", ")", ":", "return", "' '", ".", "join", "(", "v", ")", ".", "strip", "(", "'[]'", ")", "for", ...
Parse ``log`` buffer based on ``url_pattern``. Given a buffer as ``log``, parse the log buffer into a mapping of ident-hashes to a hit count, the timestamp of the initial log, and the last timestamp in the log.
[ "Parse", "log", "buffer", "based", "on", "url_pattern", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/hits_counter.py#L34-L58
openstax/cnx-archive
cnxarchive/scripts/hits_counter.py
main
def main(argv=None): """Count the hits from logfile.""" parser = create_parser('hits_counter', description=__doc__) parser.add_argument('--hostname', default='cnx.org', help="hostname of the site (default: cnx.org)") parser.add_argument('--log-format', default=LOG_FORMAT_GZ, choices=LOG_FORMATS, help="(default: {})".format(LOG_FORMAT_GZ)) parser.add_argument('log_file', help="path to the logfile.") args = parser.parse_args(argv) opener = LOG_FORMAT_OPENERS_MAPPING[args.log_format] # Build the URL pattern. hostname = args.hostname.replace('.', '\.') url_pattern = URL_PATTERN_TMPLT.format(hostname) url_pattern = re.compile(url_pattern) # Parse the log to structured data. with opener(args.log_file) as log: hits, start_timestamp, end_timestamp = parse_log(log, url_pattern) # Parse the configuration file for the postgres connection string. settings = get_app_settings_from_arguments(args) # Insert the hits into the database. connection_string = settings[config.CONNECTION_STRING] db_connection = psycopg2.connect(connection_string) with db_connection: with db_connection.cursor() as cursor: for ident_hash, hit_count in hits.items(): cursor.execute("""\ INSERT INTO document_hits (documentid, start_timestamp, end_timestamp, hits) SELECT module_ident, %s, %s, %s FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (start_timestamp, end_timestamp, hit_count, ident_hash)) cursor.execute("SELECT update_hit_ranks();") db_connection.close() return 0
python
def main(argv=None): """Count the hits from logfile.""" parser = create_parser('hits_counter', description=__doc__) parser.add_argument('--hostname', default='cnx.org', help="hostname of the site (default: cnx.org)") parser.add_argument('--log-format', default=LOG_FORMAT_GZ, choices=LOG_FORMATS, help="(default: {})".format(LOG_FORMAT_GZ)) parser.add_argument('log_file', help="path to the logfile.") args = parser.parse_args(argv) opener = LOG_FORMAT_OPENERS_MAPPING[args.log_format] # Build the URL pattern. hostname = args.hostname.replace('.', '\.') url_pattern = URL_PATTERN_TMPLT.format(hostname) url_pattern = re.compile(url_pattern) # Parse the log to structured data. with opener(args.log_file) as log: hits, start_timestamp, end_timestamp = parse_log(log, url_pattern) # Parse the configuration file for the postgres connection string. settings = get_app_settings_from_arguments(args) # Insert the hits into the database. connection_string = settings[config.CONNECTION_STRING] db_connection = psycopg2.connect(connection_string) with db_connection: with db_connection.cursor() as cursor: for ident_hash, hit_count in hits.items(): cursor.execute("""\ INSERT INTO document_hits (documentid, start_timestamp, end_timestamp, hits) SELECT module_ident, %s, %s, %s FROM modules WHERE ident_hash(uuid, major_version, minor_version) = %s""", (start_timestamp, end_timestamp, hit_count, ident_hash)) cursor.execute("SELECT update_hit_ranks();") db_connection.close() return 0
[ "def", "main", "(", "argv", "=", "None", ")", ":", "parser", "=", "create_parser", "(", "'hits_counter'", ",", "description", "=", "__doc__", ")", "parser", ".", "add_argument", "(", "'--hostname'", ",", "default", "=", "'cnx.org'", ",", "help", "=", "\"ho...
Count the hits from logfile.
[ "Count", "the", "hits", "from", "logfile", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/scripts/hits_counter.py#L61-L103
ZELLMECHANIK-DRESDEN/dclab
dclab/parse_funcs.py
fbool
def fbool(value): """boolean""" if isinstance(value, str_types): value = value.lower() if value == "false": value = False elif value == "true": value = True elif value: value = bool(float(value)) else: raise ValueError("empty string") else: value = bool(float(value)) return value
python
def fbool(value): """boolean""" if isinstance(value, str_types): value = value.lower() if value == "false": value = False elif value == "true": value = True elif value: value = bool(float(value)) else: raise ValueError("empty string") else: value = bool(float(value)) return value
[ "def", "fbool", "(", "value", ")", ":", "if", "isinstance", "(", "value", ",", "str_types", ")", ":", "value", "=", "value", ".", "lower", "(", ")", "if", "value", "==", "\"false\"", ":", "value", "=", "False", "elif", "value", "==", "\"true\"", ":",...
boolean
[ "boolean" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/parse_funcs.py#L8-L22
ZELLMECHANIK-DRESDEN/dclab
dclab/parse_funcs.py
fint
def fint(value): """integer""" if isinstance(value, str_types): # strings might have been saved wrongly as booleans value = value.lower() if value == "false": value = 0 elif value == "true": value = 1 elif value: value = int(float(value)) else: raise ValueError("empty string") else: value = int(float(value)) return value
python
def fint(value): """integer""" if isinstance(value, str_types): # strings might have been saved wrongly as booleans value = value.lower() if value == "false": value = 0 elif value == "true": value = 1 elif value: value = int(float(value)) else: raise ValueError("empty string") else: value = int(float(value)) return value
[ "def", "fint", "(", "value", ")", ":", "if", "isinstance", "(", "value", ",", "str_types", ")", ":", "# strings might have been saved wrongly as booleans", "value", "=", "value", ".", "lower", "(", ")", "if", "value", "==", "\"false\"", ":", "value", "=", "0...
integer
[ "integer" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/parse_funcs.py#L25-L40
ZELLMECHANIK-DRESDEN/dclab
dclab/parse_funcs.py
fintlist
def fintlist(alist): """A list of integers""" outlist = [] if not isinstance(alist, (list, tuple)): # we have a string (comma-separated integers) alist = alist.strip().strip("[] ").split(",") for it in alist: if it: outlist.append(fint(it)) return outlist
python
def fintlist(alist): """A list of integers""" outlist = [] if not isinstance(alist, (list, tuple)): # we have a string (comma-separated integers) alist = alist.strip().strip("[] ").split(",") for it in alist: if it: outlist.append(fint(it)) return outlist
[ "def", "fintlist", "(", "alist", ")", ":", "outlist", "=", "[", "]", "if", "not", "isinstance", "(", "alist", ",", "(", "list", ",", "tuple", ")", ")", ":", "# we have a string (comma-separated integers)", "alist", "=", "alist", ".", "strip", "(", ")", "...
A list of integers
[ "A", "list", "of", "integers" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/parse_funcs.py#L43-L52
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/export.py
Export.avi
def avi(self, path, filtered=True, override=False): """Exports filtered event images to an avi file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Raises OSError if current dataset does not contain image data """ path = pathlib.Path(path) ds = self.rtdc_ds # Make sure that path ends with .avi if path.suffix != ".avi": path = path.with_name(path.name + ".avi") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Start exporting if "image" in ds: # Open video for writing vout = imageio.get_writer(uri=path, format="FFMPEG", fps=25, codec="rawvideo", pixelformat="yuv420p", macro_block_size=None, ffmpeg_log_level="error") # write the filtered frames to avi file for evid in np.arange(len(ds)): # skip frames that were filtered out if filtered and not ds._filter[evid]: continue try: image = ds["image"][evid] except BaseException: warnings.warn("Could not read image {}!".format(evid), NoImageWarning) continue else: if np.isnan(image[0, 0]): # This is a nan-valued image image = np.zeros_like(image, dtype=np.uint8) # Convert image to RGB image = image.reshape(image.shape[0], image.shape[1], 1) image = np.repeat(image, 3, axis=2) vout.append_data(image) else: msg = "No image data to export: dataset {} !".format(ds.title) raise OSError(msg)
python
def avi(self, path, filtered=True, override=False): """Exports filtered event images to an avi file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Raises OSError if current dataset does not contain image data """ path = pathlib.Path(path) ds = self.rtdc_ds # Make sure that path ends with .avi if path.suffix != ".avi": path = path.with_name(path.name + ".avi") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Start exporting if "image" in ds: # Open video for writing vout = imageio.get_writer(uri=path, format="FFMPEG", fps=25, codec="rawvideo", pixelformat="yuv420p", macro_block_size=None, ffmpeg_log_level="error") # write the filtered frames to avi file for evid in np.arange(len(ds)): # skip frames that were filtered out if filtered and not ds._filter[evid]: continue try: image = ds["image"][evid] except BaseException: warnings.warn("Could not read image {}!".format(evid), NoImageWarning) continue else: if np.isnan(image[0, 0]): # This is a nan-valued image image = np.zeros_like(image, dtype=np.uint8) # Convert image to RGB image = image.reshape(image.shape[0], image.shape[1], 1) image = np.repeat(image, 3, axis=2) vout.append_data(image) else: msg = "No image data to export: dataset {} !".format(ds.title) raise OSError(msg)
[ "def", "avi", "(", "self", ",", "path", ",", "filtered", "=", "True", ",", "override", "=", "False", ")", ":", "path", "=", "pathlib", ".", "Path", "(", "path", ")", "ds", "=", "self", ".", "rtdc_ds", "# Make sure that path ends with .avi", "if", "path",...
Exports filtered event images to an avi file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Raises OSError if current dataset does not contain image data
[ "Exports", "filtered", "event", "images", "to", "an", "avi", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/export.py#L26-L85
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/export.py
Export.fcs
def fcs(self, path, features, filtered=True, override=False): """Export the data of an RT-DC dataset to an .fcs file Parameters ---------- mm: instance of dclab.RTDCBase The dataset that will be exported. path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Due to incompatibility with the .fcs file format, all events with NaN-valued features are not exported. """ features = [c.lower() for c in features] ds = self.rtdc_ds path = pathlib.Path(path) # Make sure that path ends with .fcs if path.suffix != ".fcs": path = path.with_name(path.name + ".fcs") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Check that features are in dfn.scalar_feature_names for c in features: if c not in dfn.scalar_feature_names: msg = "Unknown or unsupported feature name: {}".format(c) raise ValueError(msg) # Collect the header chn_names = [dfn.feature_name2label[c] for c in features] # Collect the data if filtered: data = [ds[c][ds._filter] for c in features] else: data = [ds[c] for c in features] data = np.array(data).transpose() fcswrite.write_fcs(filename=str(path), chn_names=chn_names, data=data)
python
def fcs(self, path, features, filtered=True, override=False): """Export the data of an RT-DC dataset to an .fcs file Parameters ---------- mm: instance of dclab.RTDCBase The dataset that will be exported. path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Due to incompatibility with the .fcs file format, all events with NaN-valued features are not exported. """ features = [c.lower() for c in features] ds = self.rtdc_ds path = pathlib.Path(path) # Make sure that path ends with .fcs if path.suffix != ".fcs": path = path.with_name(path.name + ".fcs") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Check that features are in dfn.scalar_feature_names for c in features: if c not in dfn.scalar_feature_names: msg = "Unknown or unsupported feature name: {}".format(c) raise ValueError(msg) # Collect the header chn_names = [dfn.feature_name2label[c] for c in features] # Collect the data if filtered: data = [ds[c][ds._filter] for c in features] else: data = [ds[c] for c in features] data = np.array(data).transpose() fcswrite.write_fcs(filename=str(path), chn_names=chn_names, data=data)
[ "def", "fcs", "(", "self", ",", "path", ",", "features", ",", "filtered", "=", "True", ",", "override", "=", "False", ")", ":", "features", "=", "[", "c", ".", "lower", "(", ")", "for", "c", "in", "features", "]", "ds", "=", "self", ".", "rtdc_ds...
Export the data of an RT-DC dataset to an .fcs file Parameters ---------- mm: instance of dclab.RTDCBase The dataset that will be exported. path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. Notes ----- Due to incompatibility with the .fcs file format, all events with NaN-valued features are not exported.
[ "Export", "the", "data", "of", "an", "RT", "-", "DC", "dataset", "to", "an", ".", "fcs", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/export.py#L87-L142
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/export.py
Export.hdf5
def hdf5(self, path, features, filtered=True, override=False, compression="gzip"): """Export the data of the current instance to an HDF5 file Parameters ---------- path: str Path to an .rtdc file. The ending .rtdc is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "image". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. compression: str or None Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"]. """ path = pathlib.Path(path) # Make sure that path ends with .rtdc if not path.suffix == ".rtdc": path = path.parent / (path.name + ".rtdc") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format(path) + "Please use the `override=True` option.") elif path.exists(): path.unlink() meta = {} # only export configuration meta data (no user-defined config) for sec in dfn.CFG_METADATA: if sec in ["fmt_tdms"]: # ignored sections continue if sec in self.rtdc_ds.config: meta[sec] = self.rtdc_ds.config[sec].copy() if filtered: filtarr = self.rtdc_ds.filter.all nev = np.sum(filtarr) else: nev = len(self.rtdc_ds) filtarr = np.ones(nev, dtype=bool) # update number of events meta["experiment"]["event count"] = nev # write meta data with write(path_or_h5file=path, meta=meta, mode="append") as h5obj: # write each feature individually for feat in features: # event-wise, because # - tdms-based datasets don't allow indexing with numpy # - there might be memory issues if feat == "contour": cont_list = [] cmax = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: dat = self.rtdc_ds["contour"][ii] cont_list.append(dat) cmax = max(cmax, dat.max()) write(h5obj, data={"contour": cont_list}, mode="append", compression=compression) elif feat in ["mask", "image"]: # store image stacks (reduced file size and save time) m = 64 im0 = self.rtdc_ds[feat][0] imstack = np.zeros((m, im0.shape[0], im0.shape[1]), dtype=im0.dtype) jj = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: dat = self.rtdc_ds[feat][ii] imstack[jj] = dat if (jj + 1) % m == 0: jj = 0 write(h5obj, data={feat: imstack}, mode="append", compression=compression) else: jj += 1 # write rest if jj: write(h5obj, data={feat: imstack[:jj, :, :]}, mode="append", compression=compression) elif feat == "trace": for tr in self.rtdc_ds["trace"].keys(): tr0 = self.rtdc_ds["trace"][tr][0] trdat = np.zeros((nev, tr0.size), dtype=tr0.dtype) jj = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: trdat[jj] = self.rtdc_ds["trace"][tr][ii] jj += 1 write(h5obj, data={"trace": {tr: trdat}}, mode="append", compression=compression) elif feat == "index" and filtered: # re-enumerate data index feature (filtered data) write(h5obj, data={"index": np.arange(1, nev+1)}, mode="append") else: write(h5obj, data={feat: self.rtdc_ds[feat][filtarr]}, mode="append")
python
def hdf5(self, path, features, filtered=True, override=False, compression="gzip"): """Export the data of the current instance to an HDF5 file Parameters ---------- path: str Path to an .rtdc file. The ending .rtdc is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "image". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. compression: str or None Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"]. """ path = pathlib.Path(path) # Make sure that path ends with .rtdc if not path.suffix == ".rtdc": path = path.parent / (path.name + ".rtdc") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format(path) + "Please use the `override=True` option.") elif path.exists(): path.unlink() meta = {} # only export configuration meta data (no user-defined config) for sec in dfn.CFG_METADATA: if sec in ["fmt_tdms"]: # ignored sections continue if sec in self.rtdc_ds.config: meta[sec] = self.rtdc_ds.config[sec].copy() if filtered: filtarr = self.rtdc_ds.filter.all nev = np.sum(filtarr) else: nev = len(self.rtdc_ds) filtarr = np.ones(nev, dtype=bool) # update number of events meta["experiment"]["event count"] = nev # write meta data with write(path_or_h5file=path, meta=meta, mode="append") as h5obj: # write each feature individually for feat in features: # event-wise, because # - tdms-based datasets don't allow indexing with numpy # - there might be memory issues if feat == "contour": cont_list = [] cmax = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: dat = self.rtdc_ds["contour"][ii] cont_list.append(dat) cmax = max(cmax, dat.max()) write(h5obj, data={"contour": cont_list}, mode="append", compression=compression) elif feat in ["mask", "image"]: # store image stacks (reduced file size and save time) m = 64 im0 = self.rtdc_ds[feat][0] imstack = np.zeros((m, im0.shape[0], im0.shape[1]), dtype=im0.dtype) jj = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: dat = self.rtdc_ds[feat][ii] imstack[jj] = dat if (jj + 1) % m == 0: jj = 0 write(h5obj, data={feat: imstack}, mode="append", compression=compression) else: jj += 1 # write rest if jj: write(h5obj, data={feat: imstack[:jj, :, :]}, mode="append", compression=compression) elif feat == "trace": for tr in self.rtdc_ds["trace"].keys(): tr0 = self.rtdc_ds["trace"][tr][0] trdat = np.zeros((nev, tr0.size), dtype=tr0.dtype) jj = 0 for ii in range(len(self.rtdc_ds)): if filtarr[ii]: trdat[jj] = self.rtdc_ds["trace"][tr][ii] jj += 1 write(h5obj, data={"trace": {tr: trdat}}, mode="append", compression=compression) elif feat == "index" and filtered: # re-enumerate data index feature (filtered data) write(h5obj, data={"index": np.arange(1, nev+1)}, mode="append") else: write(h5obj, data={feat: self.rtdc_ds[feat][filtarr]}, mode="append")
[ "def", "hdf5", "(", "self", ",", "path", ",", "features", ",", "filtered", "=", "True", ",", "override", "=", "False", ",", "compression", "=", "\"gzip\"", ")", ":", "path", "=", "pathlib", ".", "Path", "(", "path", ")", "# Make sure that path ends with .r...
Export the data of the current instance to an HDF5 file Parameters ---------- path: str Path to an .rtdc file. The ending .rtdc is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "image". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. compression: str or None Compression method for "contour", "image", and "trace" data as well as logs; one of [None, "lzf", "gzip", "szip"].
[ "Export", "the", "data", "of", "the", "current", "instance", "to", "an", "HDF5", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/export.py#L144-L263
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/export.py
Export.tsv
def tsv(self, path, features, filtered=True, override=False): """Export the data of the current instance to a .tsv file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. """ features = [c.lower() for c in features] path = pathlib.Path(path) ds = self.rtdc_ds # Make sure that path ends with .tsv if path.suffix != ".tsv": path = path.with_name(path.name + ".tsv") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Check that features are in dfn.scalar_feature_names for c in features: if c not in dfn.scalar_feature_names: raise ValueError("Unknown feature name {}".format(c)) # Open file with path.open("w") as fd: # write header header1 = "\t".join([c for c in features]) fd.write("# "+header1+"\n") header2 = "\t".join([dfn.feature_name2label[c] for c in features]) fd.write("# "+header2+"\n") with path.open("ab") as fd: # write data if filtered: data = [ds[c][ds._filter] for c in features] else: data = [ds[c] for c in features] np.savetxt(fd, np.array(data).transpose(), fmt=str("%.10e"), delimiter="\t")
python
def tsv(self, path, features, filtered=True, override=False): """Export the data of the current instance to a .tsv file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists. """ features = [c.lower() for c in features] path = pathlib.Path(path) ds = self.rtdc_ds # Make sure that path ends with .tsv if path.suffix != ".tsv": path = path.with_name(path.name + ".tsv") # Check if file already exist if not override and path.exists(): raise OSError("File already exists: {}\n".format( str(path).encode("ascii", "ignore")) + "Please use the `override=True` option.") # Check that features are in dfn.scalar_feature_names for c in features: if c not in dfn.scalar_feature_names: raise ValueError("Unknown feature name {}".format(c)) # Open file with path.open("w") as fd: # write header header1 = "\t".join([c for c in features]) fd.write("# "+header1+"\n") header2 = "\t".join([dfn.feature_name2label[c] for c in features]) fd.write("# "+header2+"\n") with path.open("ab") as fd: # write data if filtered: data = [ds[c][ds._filter] for c in features] else: data = [ds[c] for c in features] np.savetxt(fd, np.array(data).transpose(), fmt=str("%.10e"), delimiter="\t")
[ "def", "tsv", "(", "self", ",", "path", ",", "features", ",", "filtered", "=", "True", ",", "override", "=", "False", ")", ":", "features", "=", "[", "c", ".", "lower", "(", ")", "for", "c", "in", "features", "]", "path", "=", "pathlib", ".", "Pa...
Export the data of the current instance to a .tsv file Parameters ---------- path: str Path to a .tsv file. The ending .tsv is added automatically. features: list of str The features in the resulting .tsv file. These are strings that are defined in `dclab.definitions.scalar_feature_names`, e.g. "area_cvx", "deform", "frame", "fl1_max", "aspect". filtered: bool If set to `True`, only the filtered data (index in ds._filter) are used. override: bool If set to `True`, an existing file ``path`` will be overridden. If set to `False`, raises `OSError` if ``path`` exists.
[ "Export", "the", "data", "of", "the", "current", "instance", "to", "a", ".", "tsv", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/export.py#L265-L317
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/util.py
hashfile
def hashfile(fname, blocksize=65536, count=0): """Compute md5 hex-hash of a file Parameters ---------- fname: str path to the file blocksize: int block size in bytes read from the file (set to `0` to hash the entire file) count: int number of blocks read from the file """ hasher = hashlib.md5() fname = pathlib.Path(fname) with fname.open('rb') as fd: buf = fd.read(blocksize) ii = 0 while len(buf) > 0: hasher.update(buf) buf = fd.read(blocksize) ii += 1 if count and ii == count: break return hasher.hexdigest()
python
def hashfile(fname, blocksize=65536, count=0): """Compute md5 hex-hash of a file Parameters ---------- fname: str path to the file blocksize: int block size in bytes read from the file (set to `0` to hash the entire file) count: int number of blocks read from the file """ hasher = hashlib.md5() fname = pathlib.Path(fname) with fname.open('rb') as fd: buf = fd.read(blocksize) ii = 0 while len(buf) > 0: hasher.update(buf) buf = fd.read(blocksize) ii += 1 if count and ii == count: break return hasher.hexdigest()
[ "def", "hashfile", "(", "fname", ",", "blocksize", "=", "65536", ",", "count", "=", "0", ")", ":", "hasher", "=", "hashlib", ".", "md5", "(", ")", "fname", "=", "pathlib", ".", "Path", "(", "fname", ")", "with", "fname", ".", "open", "(", "'rb'", ...
Compute md5 hex-hash of a file Parameters ---------- fname: str path to the file blocksize: int block size in bytes read from the file (set to `0` to hash the entire file) count: int number of blocks read from the file
[ "Compute", "md5", "hex", "-", "hash", "of", "a", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/util.py#L15-L39
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/util.py
obj2str
def obj2str(obj): """String representation of an object for hashing""" if isinstance(obj, str_types): return obj.encode("utf-8") elif isinstance(obj, pathlib.Path): return obj2str(str(obj)) elif isinstance(obj, (bool, int, float)): return str(obj).encode("utf-8") elif obj is None: return b"none" elif isinstance(obj, np.ndarray): return obj.tostring() elif isinstance(obj, tuple): return obj2str(list(obj)) elif isinstance(obj, list): return b"".join(obj2str(o) for o in obj) elif isinstance(obj, dict): return obj2str(list(obj.items())) elif hasattr(obj, "identifier"): return obj2str(obj.identifier) elif isinstance(obj, h5py.Dataset): return obj2str(obj[0]) else: raise ValueError("No rule to convert object '{}' to string.". format(obj.__class__))
python
def obj2str(obj): """String representation of an object for hashing""" if isinstance(obj, str_types): return obj.encode("utf-8") elif isinstance(obj, pathlib.Path): return obj2str(str(obj)) elif isinstance(obj, (bool, int, float)): return str(obj).encode("utf-8") elif obj is None: return b"none" elif isinstance(obj, np.ndarray): return obj.tostring() elif isinstance(obj, tuple): return obj2str(list(obj)) elif isinstance(obj, list): return b"".join(obj2str(o) for o in obj) elif isinstance(obj, dict): return obj2str(list(obj.items())) elif hasattr(obj, "identifier"): return obj2str(obj.identifier) elif isinstance(obj, h5py.Dataset): return obj2str(obj[0]) else: raise ValueError("No rule to convert object '{}' to string.". format(obj.__class__))
[ "def", "obj2str", "(", "obj", ")", ":", "if", "isinstance", "(", "obj", ",", "str_types", ")", ":", "return", "obj", ".", "encode", "(", "\"utf-8\"", ")", "elif", "isinstance", "(", "obj", ",", "pathlib", ".", "Path", ")", ":", "return", "obj2str", "...
String representation of an object for hashing
[ "String", "representation", "of", "an", "object", "for", "hashing" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/util.py#L47-L71
xenon-middleware/pyxenon
xenon/create_keys.py
create_self_signed_cert
def create_self_signed_cert(): """Creates a self-signed certificate key pair.""" config_dir = Path(BaseDirectory.xdg_config_home) / 'xenon-grpc' config_dir.mkdir(parents=True, exist_ok=True) key_prefix = gethostname() crt_file = config_dir / ('%s.crt' % key_prefix) key_file = config_dir / ('%s.key' % key_prefix) if crt_file.exists() and key_file.exists(): return crt_file, key_file logger = logging.getLogger('xenon') logger.info("Creating authentication keys for xenon-grpc.") # create a key pair k = crypto.PKey() k.generate_key(crypto.TYPE_RSA, 1024) # create a self-signed cert cert = crypto.X509() cert.get_subject().CN = gethostname() cert.set_serial_number(1000) cert.gmtime_adj_notBefore(0) # valid for almost ten years! cert.gmtime_adj_notAfter(10 * 365 * 24 * 3600) cert.set_issuer(cert.get_subject()) cert.set_pubkey(k) cert.sign(k, 'sha256') open(str(crt_file), "wb").write( crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) open(str(key_file), "wb").write( crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) return crt_file, key_file
python
def create_self_signed_cert(): """Creates a self-signed certificate key pair.""" config_dir = Path(BaseDirectory.xdg_config_home) / 'xenon-grpc' config_dir.mkdir(parents=True, exist_ok=True) key_prefix = gethostname() crt_file = config_dir / ('%s.crt' % key_prefix) key_file = config_dir / ('%s.key' % key_prefix) if crt_file.exists() and key_file.exists(): return crt_file, key_file logger = logging.getLogger('xenon') logger.info("Creating authentication keys for xenon-grpc.") # create a key pair k = crypto.PKey() k.generate_key(crypto.TYPE_RSA, 1024) # create a self-signed cert cert = crypto.X509() cert.get_subject().CN = gethostname() cert.set_serial_number(1000) cert.gmtime_adj_notBefore(0) # valid for almost ten years! cert.gmtime_adj_notAfter(10 * 365 * 24 * 3600) cert.set_issuer(cert.get_subject()) cert.set_pubkey(k) cert.sign(k, 'sha256') open(str(crt_file), "wb").write( crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) open(str(key_file), "wb").write( crypto.dump_privatekey(crypto.FILETYPE_PEM, k)) return crt_file, key_file
[ "def", "create_self_signed_cert", "(", ")", ":", "config_dir", "=", "Path", "(", "BaseDirectory", ".", "xdg_config_home", ")", "/", "'xenon-grpc'", "config_dir", ".", "mkdir", "(", "parents", "=", "True", ",", "exist_ok", "=", "True", ")", "key_prefix", "=", ...
Creates a self-signed certificate key pair.
[ "Creates", "a", "self", "-", "signed", "certificate", "key", "pair", "." ]
train
https://github.com/xenon-middleware/pyxenon/blob/d61109ad339ee9bb9f0723471d532727b0f235ad/xenon/create_keys.py#L14-L49
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/__init__.py
get_project_name_from_path
def get_project_name_from_path(path, append_mx=False): """Get the project name from a path. For a path "/home/peter/hans/HLC12398/online/M1_13.tdms" or For a path "/home/peter/hans/HLC12398/online/data/M1_13.tdms" or without the ".tdms" file, this will return always "HLC12398". Parameters ---------- path: str path to tdms file append_mx: bool append measurement number, e.g. "M1" """ path = pathlib.Path(path) if path.suffix == ".tdms": dirn = path.parent mx = path.name.split("_")[0] elif path.is_dir(): dirn = path mx = "" else: dirn = path.parent mx = "" project = "" if mx: # check para.ini para = dirn / (mx + "_para.ini") if para.exists(): with para.open() as fd: lines = fd.readlines() for line in lines: if line.startswith("Sample Name ="): project = line.split("=")[1].strip() break if not project: # check if the directory contains data or is online root1, trail1 = dirn.parent, dirn.name root2, trail2 = root1.parent, root1.name trail3 = root2.name if trail1.lower() in ["online", "offline"]: # /home/peter/hans/HLC12398/online/ project = trail2 elif (trail1.lower() == "data" and trail2.lower() in ["online", "offline"]): # this is olis new folder sctructure # /home/peter/hans/HLC12398/online/data/ project = trail3 else: project = trail1 if append_mx: project += " - " + mx return project
python
def get_project_name_from_path(path, append_mx=False): """Get the project name from a path. For a path "/home/peter/hans/HLC12398/online/M1_13.tdms" or For a path "/home/peter/hans/HLC12398/online/data/M1_13.tdms" or without the ".tdms" file, this will return always "HLC12398". Parameters ---------- path: str path to tdms file append_mx: bool append measurement number, e.g. "M1" """ path = pathlib.Path(path) if path.suffix == ".tdms": dirn = path.parent mx = path.name.split("_")[0] elif path.is_dir(): dirn = path mx = "" else: dirn = path.parent mx = "" project = "" if mx: # check para.ini para = dirn / (mx + "_para.ini") if para.exists(): with para.open() as fd: lines = fd.readlines() for line in lines: if line.startswith("Sample Name ="): project = line.split("=")[1].strip() break if not project: # check if the directory contains data or is online root1, trail1 = dirn.parent, dirn.name root2, trail2 = root1.parent, root1.name trail3 = root2.name if trail1.lower() in ["online", "offline"]: # /home/peter/hans/HLC12398/online/ project = trail2 elif (trail1.lower() == "data" and trail2.lower() in ["online", "offline"]): # this is olis new folder sctructure # /home/peter/hans/HLC12398/online/data/ project = trail3 else: project = trail1 if append_mx: project += " - " + mx return project
[ "def", "get_project_name_from_path", "(", "path", ",", "append_mx", "=", "False", ")", ":", "path", "=", "pathlib", ".", "Path", "(", "path", ")", "if", "path", ".", "suffix", "==", "\".tdms\"", ":", "dirn", "=", "path", ".", "parent", "mx", "=", "path...
Get the project name from a path. For a path "/home/peter/hans/HLC12398/online/M1_13.tdms" or For a path "/home/peter/hans/HLC12398/online/data/M1_13.tdms" or without the ".tdms" file, this will return always "HLC12398". Parameters ---------- path: str path to tdms file append_mx: bool append measurement number, e.g. "M1"
[ "Get", "the", "project", "name", "from", "a", "path", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/__init__.py#L183-L240
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/__init__.py
get_tdms_files
def get_tdms_files(directory): """Recursively find projects based on '.tdms' file endings Searches the `directory` recursively and return a sorted list of all found '.tdms' project files, except fluorescence data trace files which end with `_traces.tdms`. """ path = pathlib.Path(directory).resolve() # get all tdms files tdmslist = [r for r in path.rglob("*.tdms") if r.is_file()] # exclude traces files tdmslist = [r for r in tdmslist if not r.name.endswith("_traces.tdms")] return sorted(tdmslist)
python
def get_tdms_files(directory): """Recursively find projects based on '.tdms' file endings Searches the `directory` recursively and return a sorted list of all found '.tdms' project files, except fluorescence data trace files which end with `_traces.tdms`. """ path = pathlib.Path(directory).resolve() # get all tdms files tdmslist = [r for r in path.rglob("*.tdms") if r.is_file()] # exclude traces files tdmslist = [r for r in tdmslist if not r.name.endswith("_traces.tdms")] return sorted(tdmslist)
[ "def", "get_tdms_files", "(", "directory", ")", ":", "path", "=", "pathlib", ".", "Path", "(", "directory", ")", ".", "resolve", "(", ")", "# get all tdms files", "tdmslist", "=", "[", "r", "for", "r", "in", "path", ".", "rglob", "(", "\"*.tdms\"", ")", ...
Recursively find projects based on '.tdms' file endings Searches the `directory` recursively and return a sorted list of all found '.tdms' project files, except fluorescence data trace files which end with `_traces.tdms`.
[ "Recursively", "find", "projects", "based", "on", ".", "tdms", "file", "endings" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/__init__.py#L243-L255
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/__init__.py
RTDC_TDMS._init_data_with_tdms
def _init_data_with_tdms(self, tdms_filename): """Initializes the current RT-DC dataset with a tdms file. """ tdms_file = TdmsFile(str(tdms_filename)) # time is always there table = "Cell Track" # Edit naming.dclab2tdms to add features for arg in naming.tdms2dclab: try: data = tdms_file.object(table, arg).data except KeyError: pass else: if data is None or len(data) == 0: # Ignore empty features. npTDMS treats empty # features in the following way: # - in nptdms 0.8.2, `data` is `None` # - in nptdms 0.9.0, `data` is an array of length 0 continue self._events[naming.tdms2dclab[arg]] = data # Set up configuration tdms_config = Configuration( files=[self.path.with_name(self._mid + "_para.ini"), self.path.with_name(self._mid + "_camera.ini")], ) dclab_config = Configuration() for section in naming.configmap: for pname in naming.configmap[section]: meta = naming.configmap[section][pname] typ = dfn.config_funcs[section][pname] if isinstance(meta, tuple): osec, opar = meta if osec in tdms_config and opar in tdms_config[osec]: val = tdms_config[osec].pop(opar) dclab_config[section][pname] = typ(val) else: dclab_config[section][pname] = typ(meta) self.config = dclab_config self._complete_config_tdms(tdms_config) self._init_filters()
python
def _init_data_with_tdms(self, tdms_filename): """Initializes the current RT-DC dataset with a tdms file. """ tdms_file = TdmsFile(str(tdms_filename)) # time is always there table = "Cell Track" # Edit naming.dclab2tdms to add features for arg in naming.tdms2dclab: try: data = tdms_file.object(table, arg).data except KeyError: pass else: if data is None or len(data) == 0: # Ignore empty features. npTDMS treats empty # features in the following way: # - in nptdms 0.8.2, `data` is `None` # - in nptdms 0.9.0, `data` is an array of length 0 continue self._events[naming.tdms2dclab[arg]] = data # Set up configuration tdms_config = Configuration( files=[self.path.with_name(self._mid + "_para.ini"), self.path.with_name(self._mid + "_camera.ini")], ) dclab_config = Configuration() for section in naming.configmap: for pname in naming.configmap[section]: meta = naming.configmap[section][pname] typ = dfn.config_funcs[section][pname] if isinstance(meta, tuple): osec, opar = meta if osec in tdms_config and opar in tdms_config[osec]: val = tdms_config[osec].pop(opar) dclab_config[section][pname] = typ(val) else: dclab_config[section][pname] = typ(meta) self.config = dclab_config self._complete_config_tdms(tdms_config) self._init_filters()
[ "def", "_init_data_with_tdms", "(", "self", ",", "tdms_filename", ")", ":", "tdms_file", "=", "TdmsFile", "(", "str", "(", "tdms_filename", ")", ")", "# time is always there", "table", "=", "\"Cell Track\"", "# Edit naming.dclab2tdms to add features", "for", "arg", "i...
Initializes the current RT-DC dataset with a tdms file.
[ "Initializes", "the", "current", "RT", "-", "DC", "dataset", "with", "a", "tdms", "file", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/__init__.py#L69-L111
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/__init__.py
RTDC_TDMS.hash
def hash(self): """Hash value based on file name and .ini file content""" if self._hash is None: # Only hash _camera.ini and _para.ini fsh = [self.path.with_name(self._mid + "_camera.ini"), self.path.with_name(self._mid + "_para.ini")] tohash = [hashfile(f) for f in fsh] tohash.append(self.path.name) # Hash a maximum of ~1MB of the tdms file tohash.append(hashfile(self.path, blocksize=65536, count=20)) self._hash = hashobj(tohash) return self._hash
python
def hash(self): """Hash value based on file name and .ini file content""" if self._hash is None: # Only hash _camera.ini and _para.ini fsh = [self.path.with_name(self._mid + "_camera.ini"), self.path.with_name(self._mid + "_para.ini")] tohash = [hashfile(f) for f in fsh] tohash.append(self.path.name) # Hash a maximum of ~1MB of the tdms file tohash.append(hashfile(self.path, blocksize=65536, count=20)) self._hash = hashobj(tohash) return self._hash
[ "def", "hash", "(", "self", ")", ":", "if", "self", ".", "_hash", "is", "None", ":", "# Only hash _camera.ini and _para.ini", "fsh", "=", "[", "self", ".", "path", ".", "with_name", "(", "self", ".", "_mid", "+", "\"_camera.ini\"", ")", ",", "self", ".",...
Hash value based on file name and .ini file content
[ "Hash", "value", "based", "on", "file", "name", "and", ".", "ini", "file", "content" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/__init__.py#L169-L180
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_contour.py
ContourColumn.determine_offset
def determine_offset(self): """Determines the offset of the contours w.r.t. other data columns Notes ----- - the "frame" column of `rtdc_dataset` is compared to the first contour in the contour text file to determine an offset by one event - modifies the property `event_offset` and sets `_initialized` to `True` """ # In case of regular RTDC, the first contour is # missing. In case of fRTDC, it is there, so we # might have an offset. We find out if the first # contour frame is missing by comparing it to # the "frame" column of the rtdc dataset. fref = self._contour_data.get_frame(0) f0 = self.frame[0] f1 = self.frame[1] # Use allclose to avoid float/integer comparison problems if np.allclose(fref, f0): self.event_offset = 0 elif np.allclose(fref, f1): self.event_offset = 1 else: msg = "Contour data has unknown offset (frame {})!".format(fref) raise IndexError(msg) self._initialized = True
python
def determine_offset(self): """Determines the offset of the contours w.r.t. other data columns Notes ----- - the "frame" column of `rtdc_dataset` is compared to the first contour in the contour text file to determine an offset by one event - modifies the property `event_offset` and sets `_initialized` to `True` """ # In case of regular RTDC, the first contour is # missing. In case of fRTDC, it is there, so we # might have an offset. We find out if the first # contour frame is missing by comparing it to # the "frame" column of the rtdc dataset. fref = self._contour_data.get_frame(0) f0 = self.frame[0] f1 = self.frame[1] # Use allclose to avoid float/integer comparison problems if np.allclose(fref, f0): self.event_offset = 0 elif np.allclose(fref, f1): self.event_offset = 1 else: msg = "Contour data has unknown offset (frame {})!".format(fref) raise IndexError(msg) self._initialized = True
[ "def", "determine_offset", "(", "self", ")", ":", "# In case of regular RTDC, the first contour is", "# missing. In case of fRTDC, it is there, so we", "# might have an offset. We find out if the first", "# contour frame is missing by comparing it to", "# the \"frame\" column of the rtdc dataset...
Determines the offset of the contours w.r.t. other data columns Notes ----- - the "frame" column of `rtdc_dataset` is compared to the first contour in the contour text file to determine an offset by one event - modifies the property `event_offset` and sets `_initialized` to `True`
[ "Determines", "the", "offset", "of", "the", "contours", "w", ".", "r", ".", "t", ".", "other", "data", "columns" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_contour.py#L61-L89
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_contour.py
ContourColumn.find_contour_file
def find_contour_file(rtdc_dataset): """Tries to find a contour file that belongs to an RTDC dataset Returns None if no contour file is found. """ cont_id = rtdc_dataset.path.stem cands = [c.name for c in rtdc_dataset._fdir.rglob("*_contours.txt")] cands = sorted(cands) # Search for perfect matches, e.g. # - M1_0.240000ul_s.tdms # - M1_0.240000ul_s_contours.txt for c1 in cands: if c1.startswith(cont_id): cfile = rtdc_dataset._fdir / c1 break else: # Search for M* matches with most overlap, e.g. # - M1_0.240000ul_s.tdms # - M1_contours.txt for c2 in cands: if (c2.split("_")[0] == rtdc_dataset._mid): # Do not confuse with M10_contours.txt cfile = rtdc_dataset._fdir / c2 break else: msg = "No contour data found for {}".format(rtdc_dataset) warnings.warn(msg, NoContourDataWarning) cfile = None return cfile
python
def find_contour_file(rtdc_dataset): """Tries to find a contour file that belongs to an RTDC dataset Returns None if no contour file is found. """ cont_id = rtdc_dataset.path.stem cands = [c.name for c in rtdc_dataset._fdir.rglob("*_contours.txt")] cands = sorted(cands) # Search for perfect matches, e.g. # - M1_0.240000ul_s.tdms # - M1_0.240000ul_s_contours.txt for c1 in cands: if c1.startswith(cont_id): cfile = rtdc_dataset._fdir / c1 break else: # Search for M* matches with most overlap, e.g. # - M1_0.240000ul_s.tdms # - M1_contours.txt for c2 in cands: if (c2.split("_")[0] == rtdc_dataset._mid): # Do not confuse with M10_contours.txt cfile = rtdc_dataset._fdir / c2 break else: msg = "No contour data found for {}".format(rtdc_dataset) warnings.warn(msg, NoContourDataWarning) cfile = None return cfile
[ "def", "find_contour_file", "(", "rtdc_dataset", ")", ":", "cont_id", "=", "rtdc_dataset", ".", "path", ".", "stem", "cands", "=", "[", "c", ".", "name", "for", "c", "in", "rtdc_dataset", ".", "_fdir", ".", "rglob", "(", "\"*_contours.txt\"", ")", "]", "...
Tries to find a contour file that belongs to an RTDC dataset Returns None if no contour file is found.
[ "Tries", "to", "find", "a", "contour", "file", "that", "belongs", "to", "an", "RTDC", "dataset" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_contour.py#L92-L120
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_contour.py
ContourData._index_file
def _index_file(self): """Open and index the contour file This function populates the internal list of contours as strings which will be available as `self.data`. """ with self.filename.open() as fd: data = fd.read() ident = "Contour in frame" self._data = data.split(ident)[1:] self._initialized = True
python
def _index_file(self): """Open and index the contour file This function populates the internal list of contours as strings which will be available as `self.data`. """ with self.filename.open() as fd: data = fd.read() ident = "Contour in frame" self._data = data.split(ident)[1:] self._initialized = True
[ "def", "_index_file", "(", "self", ")", ":", "with", "self", ".", "filename", ".", "open", "(", ")", "as", "fd", ":", "data", "=", "fd", ".", "read", "(", ")", "ident", "=", "\"Contour in frame\"", "self", ".", "_data", "=", "data", ".", "split", "...
Open and index the contour file This function populates the internal list of contours as strings which will be available as `self.data`.
[ "Open", "and", "index", "the", "contour", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_contour.py#L152-L163
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/fmt_tdms/event_contour.py
ContourData.get_frame
def get_frame(self, idx): """Return the frame number of a contour""" cont = self.data[idx] frame = int(cont.strip().split(" ", 1)[0]) return frame
python
def get_frame(self, idx): """Return the frame number of a contour""" cont = self.data[idx] frame = int(cont.strip().split(" ", 1)[0]) return frame
[ "def", "get_frame", "(", "self", ",", "idx", ")", ":", "cont", "=", "self", ".", "data", "[", "idx", "]", "frame", "=", "int", "(", "cont", ".", "strip", "(", ")", ".", "split", "(", "\" \"", ",", "1", ")", "[", "0", "]", ")", "return", "fram...
Return the frame number of a contour
[ "Return", "the", "frame", "number", "of", "a", "contour" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/fmt_tdms/event_contour.py#L175-L179
ZELLMECHANIK-DRESDEN/dclab
dclab/features/emodulus_viscosity.py
get_viscosity
def get_viscosity(medium="CellCarrier", channel_width=20.0, flow_rate=0.16, temperature=23.0): """Returns the viscosity for RT-DC-specific media Parameters ---------- medium: str The medium to compute the viscosity for. One of ["CellCarrier", "CellCarrier B", "water"]. channel_width: float The channel width in µm flow_rate: float Flow rate in µl/s temperature: float or ndarray Temperature in °C Returns ------- viscosity: float or ndarray Viscosity in mPa*s Notes ----- - CellCarrier and CellCarrier B media are optimized for RT-DC measurements. - Values for the viscosity of water are computed using equation (15) from :cite:`Kestin_1978`. """ if medium.lower() not in ["cellcarrier", "cellcarrier b", "water"]: raise ValueError("Invalid medium: {}".format(medium)) # convert flow_rate from µl/s to m³/s # convert channel_width from µm to m term1 = 1.1856 * 6 * flow_rate * 1e-9 / (channel_width * 1e-6)**3 * 2 / 3 if medium == "CellCarrier": temp_corr = (temperature / 23.2)**-0.866 term2 = 0.6771 / 0.5928 + 0.2121 / (0.5928 * 0.677) eta = 0.179 * (term1 * term2)**(0.677 - 1) * temp_corr * 1e3 elif medium == "CellCarrier B": temp_corr = (temperature / 23.6)**-0.866 term2 = 0.6771 / 0.5928 + 0.2121 / (0.5928 * 0.634) eta = 0.360 * (term1 * term2)**(0.634 - 1) * temp_corr * 1e3 elif medium == "water": # see equation (15) in Kestin et al, J. Phys. Chem. 7(3) 1978 if np.min(temperature) < 0 or np.max(temperature) > 40: msg = "For water, the temperature must be in [0, 40] degC! " \ "Got min/max values of '{}'.".format(np.min(temperature), np.max(temperature)) raise ValueError(msg) eta0 = 1.002 # [mPa] right = (20-temperature) / (temperature + 96) \ * (+ 1.2364 - 1.37e-3 * (20 - temperature) + 5.7e-6 * (20 - temperature)**2 ) eta = eta0 * 10**right return eta
python
def get_viscosity(medium="CellCarrier", channel_width=20.0, flow_rate=0.16, temperature=23.0): """Returns the viscosity for RT-DC-specific media Parameters ---------- medium: str The medium to compute the viscosity for. One of ["CellCarrier", "CellCarrier B", "water"]. channel_width: float The channel width in µm flow_rate: float Flow rate in µl/s temperature: float or ndarray Temperature in °C Returns ------- viscosity: float or ndarray Viscosity in mPa*s Notes ----- - CellCarrier and CellCarrier B media are optimized for RT-DC measurements. - Values for the viscosity of water are computed using equation (15) from :cite:`Kestin_1978`. """ if medium.lower() not in ["cellcarrier", "cellcarrier b", "water"]: raise ValueError("Invalid medium: {}".format(medium)) # convert flow_rate from µl/s to m³/s # convert channel_width from µm to m term1 = 1.1856 * 6 * flow_rate * 1e-9 / (channel_width * 1e-6)**3 * 2 / 3 if medium == "CellCarrier": temp_corr = (temperature / 23.2)**-0.866 term2 = 0.6771 / 0.5928 + 0.2121 / (0.5928 * 0.677) eta = 0.179 * (term1 * term2)**(0.677 - 1) * temp_corr * 1e3 elif medium == "CellCarrier B": temp_corr = (temperature / 23.6)**-0.866 term2 = 0.6771 / 0.5928 + 0.2121 / (0.5928 * 0.634) eta = 0.360 * (term1 * term2)**(0.634 - 1) * temp_corr * 1e3 elif medium == "water": # see equation (15) in Kestin et al, J. Phys. Chem. 7(3) 1978 if np.min(temperature) < 0 or np.max(temperature) > 40: msg = "For water, the temperature must be in [0, 40] degC! " \ "Got min/max values of '{}'.".format(np.min(temperature), np.max(temperature)) raise ValueError(msg) eta0 = 1.002 # [mPa] right = (20-temperature) / (temperature + 96) \ * (+ 1.2364 - 1.37e-3 * (20 - temperature) + 5.7e-6 * (20 - temperature)**2 ) eta = eta0 * 10**right return eta
[ "def", "get_viscosity", "(", "medium", "=", "\"CellCarrier\"", ",", "channel_width", "=", "20.0", ",", "flow_rate", "=", "0.16", ",", "temperature", "=", "23.0", ")", ":", "if", "medium", ".", "lower", "(", ")", "not", "in", "[", "\"cellcarrier\"", ",", ...
Returns the viscosity for RT-DC-specific media Parameters ---------- medium: str The medium to compute the viscosity for. One of ["CellCarrier", "CellCarrier B", "water"]. channel_width: float The channel width in µm flow_rate: float Flow rate in µl/s temperature: float or ndarray Temperature in °C Returns ------- viscosity: float or ndarray Viscosity in mPa*s Notes ----- - CellCarrier and CellCarrier B media are optimized for RT-DC measurements. - Values for the viscosity of water are computed using equation (15) from :cite:`Kestin_1978`.
[ "Returns", "the", "viscosity", "for", "RT", "-", "DC", "-", "specific", "media" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/emodulus_viscosity.py#L9-L66
ZELLMECHANIK-DRESDEN/dclab
dclab/statistics.py
get_statistics
def get_statistics(ds, methods=None, features=None): """Compute statistics for an RT-DC dataset Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset for which to compute the statistics. methods: list of str or None The methods wih which to compute the statistics. The list of available methods is given with `dclab.statistics.Statistics.available_methods.keys()` If set to `None`, statistics for all methods are computed. features: list of str Feature name identifiers are defined in `dclab.definitions.scalar_feature_names`. If set to `None`, statistics for all axes are computed. Returns ------- header: list of str The header (feature + method names) of the computed statistics. values: list of float The computed statistics. """ if methods is None: cls = list(Statistics.available_methods.keys()) # sort the features in a usable way avm = Statistics.available_methods me1 = [m for m in cls if not avm[m].req_feature] me2 = [m for m in cls if avm[m].req_feature] methods = me1 + me2 if features is None: features = dfn.scalar_feature_names else: features = [a.lower() for a in features] header = [] values = [] # To make sure that all methods are computed for each feature in a block, # we loop over all features. It would be easier to loop over the methods, # but the resulting statistics would not be human-friendly. for ft in features: for mt in methods: meth = Statistics.available_methods[mt] if meth.req_feature: if ft in ds: values.append(meth(ds=ds, feature=ft)) else: values.append(np.nan) header.append(" ".join([mt, dfn.feature_name2label[ft]])) else: # Prevent multiple entries of this method. if not header.count(mt): values.append(meth(ds=ds)) header.append(mt) return header, values
python
def get_statistics(ds, methods=None, features=None): """Compute statistics for an RT-DC dataset Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset for which to compute the statistics. methods: list of str or None The methods wih which to compute the statistics. The list of available methods is given with `dclab.statistics.Statistics.available_methods.keys()` If set to `None`, statistics for all methods are computed. features: list of str Feature name identifiers are defined in `dclab.definitions.scalar_feature_names`. If set to `None`, statistics for all axes are computed. Returns ------- header: list of str The header (feature + method names) of the computed statistics. values: list of float The computed statistics. """ if methods is None: cls = list(Statistics.available_methods.keys()) # sort the features in a usable way avm = Statistics.available_methods me1 = [m for m in cls if not avm[m].req_feature] me2 = [m for m in cls if avm[m].req_feature] methods = me1 + me2 if features is None: features = dfn.scalar_feature_names else: features = [a.lower() for a in features] header = [] values = [] # To make sure that all methods are computed for each feature in a block, # we loop over all features. It would be easier to loop over the methods, # but the resulting statistics would not be human-friendly. for ft in features: for mt in methods: meth = Statistics.available_methods[mt] if meth.req_feature: if ft in ds: values.append(meth(ds=ds, feature=ft)) else: values.append(np.nan) header.append(" ".join([mt, dfn.feature_name2label[ft]])) else: # Prevent multiple entries of this method. if not header.count(mt): values.append(meth(ds=ds)) header.append(mt) return header, values
[ "def", "get_statistics", "(", "ds", ",", "methods", "=", "None", ",", "features", "=", "None", ")", ":", "if", "methods", "is", "None", ":", "cls", "=", "list", "(", "Statistics", ".", "available_methods", ".", "keys", "(", ")", ")", "# sort the features...
Compute statistics for an RT-DC dataset Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset for which to compute the statistics. methods: list of str or None The methods wih which to compute the statistics. The list of available methods is given with `dclab.statistics.Statistics.available_methods.keys()` If set to `None`, statistics for all methods are computed. features: list of str Feature name identifiers are defined in `dclab.definitions.scalar_feature_names`. If set to `None`, statistics for all axes are computed. Returns ------- header: list of str The header (feature + method names) of the computed statistics. values: list of float The computed statistics.
[ "Compute", "statistics", "for", "an", "RT", "-", "DC", "dataset" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/statistics.py#L92-L150
ZELLMECHANIK-DRESDEN/dclab
dclab/statistics.py
mode
def mode(data): """Compute an intelligent value for the mode The most common value in experimental is not very useful if there are a lot of digits after the comma. This method approaches this issue by rounding to bin size that is determined by the Freedman–Diaconis rule. Parameters ---------- data: 1d ndarray The data for which the mode should be computed. Returns ------- mode: float The mode computed with the Freedman-Diaconis rule. """ # size n = data.shape[0] # interquartile range iqr = np.percentile(data, 75)-np.percentile(data, 25) # Freedman–Diaconis bin_size = 2 * iqr / n**(1/3) if bin_size == 0: return np.nan # Add bin_size/2, because we want the center of the bin and # not the left corner of the bin. databin = np.round(data/bin_size)*bin_size + bin_size/2 u, indices = np.unique(databin, return_inverse=True) mode = u[np.argmax(np.bincount(indices))] return mode
python
def mode(data): """Compute an intelligent value for the mode The most common value in experimental is not very useful if there are a lot of digits after the comma. This method approaches this issue by rounding to bin size that is determined by the Freedman–Diaconis rule. Parameters ---------- data: 1d ndarray The data for which the mode should be computed. Returns ------- mode: float The mode computed with the Freedman-Diaconis rule. """ # size n = data.shape[0] # interquartile range iqr = np.percentile(data, 75)-np.percentile(data, 25) # Freedman–Diaconis bin_size = 2 * iqr / n**(1/3) if bin_size == 0: return np.nan # Add bin_size/2, because we want the center of the bin and # not the left corner of the bin. databin = np.round(data/bin_size)*bin_size + bin_size/2 u, indices = np.unique(databin, return_inverse=True) mode = u[np.argmax(np.bincount(indices))] return mode
[ "def", "mode", "(", "data", ")", ":", "# size", "n", "=", "data", ".", "shape", "[", "0", "]", "# interquartile range", "iqr", "=", "np", ".", "percentile", "(", "data", ",", "75", ")", "-", "np", ".", "percentile", "(", "data", ",", "25", ")", "...
Compute an intelligent value for the mode The most common value in experimental is not very useful if there are a lot of digits after the comma. This method approaches this issue by rounding to bin size that is determined by the Freedman–Diaconis rule. Parameters ---------- data: 1d ndarray The data for which the mode should be computed. Returns ------- mode: float The mode computed with the Freedman-Diaconis rule.
[ "Compute", "an", "intelligent", "value", "for", "the", "mode" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/statistics.py#L153-L187
ZELLMECHANIK-DRESDEN/dclab
dclab/statistics.py
Statistics._get_data
def _get_data(self, kwargs): """Convenience wrapper to get statistics data""" if "ds" not in kwargs: raise ValueError("Keyword argument 'ds' missing.") ds = kwargs["ds"] if self.req_feature: if "feature" not in kwargs: raise ValueError("Keyword argument 'feature' missing.") return self.get_feature(ds, kwargs["feature"]) else: return ds
python
def _get_data(self, kwargs): """Convenience wrapper to get statistics data""" if "ds" not in kwargs: raise ValueError("Keyword argument 'ds' missing.") ds = kwargs["ds"] if self.req_feature: if "feature" not in kwargs: raise ValueError("Keyword argument 'feature' missing.") return self.get_feature(ds, kwargs["feature"]) else: return ds
[ "def", "_get_data", "(", "self", ",", "kwargs", ")", ":", "if", "\"ds\"", "not", "in", "kwargs", ":", "raise", "ValueError", "(", "\"Keyword argument 'ds' missing.\"", ")", "ds", "=", "kwargs", "[", "\"ds\"", "]", "if", "self", ".", "req_feature", ":", "if...
Convenience wrapper to get statistics data
[ "Convenience", "wrapper", "to", "get", "statistics", "data" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/statistics.py#L46-L58
ZELLMECHANIK-DRESDEN/dclab
dclab/statistics.py
Statistics.get_feature
def get_feature(self, ds, feat): """Return filtered feature data The features are filtered according to the user-defined filters, using the information in `ds._filter`. In addition, all `nan` and `inf` values are purged. Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset containing the feature feat: str The name of the feature; must be a scalar feature """ if ds.config["filtering"]["enable filters"]: x = ds[feat][ds._filter] else: x = ds[feat] bad = np.isnan(x) | np.isinf(x) xout = x[~bad] return xout
python
def get_feature(self, ds, feat): """Return filtered feature data The features are filtered according to the user-defined filters, using the information in `ds._filter`. In addition, all `nan` and `inf` values are purged. Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset containing the feature feat: str The name of the feature; must be a scalar feature """ if ds.config["filtering"]["enable filters"]: x = ds[feat][ds._filter] else: x = ds[feat] bad = np.isnan(x) | np.isinf(x) xout = x[~bad] return xout
[ "def", "get_feature", "(", "self", ",", "ds", ",", "feat", ")", ":", "if", "ds", ".", "config", "[", "\"filtering\"", "]", "[", "\"enable filters\"", "]", ":", "x", "=", "ds", "[", "feat", "]", "[", "ds", ".", "_filter", "]", "else", ":", "x", "=...
Return filtered feature data The features are filtered according to the user-defined filters, using the information in `ds._filter`. In addition, all `nan` and `inf` values are purged. Parameters ---------- ds: dclab.rtdc_dataset.RTDCBase The dataset containing the feature feat: str The name of the feature; must be a scalar feature
[ "Return", "filtered", "feature", "data" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/statistics.py#L60-L80
kpn-digital/py-timeexecution
time_execution/decorator.py
time_execution.get_exception
def get_exception(self): """Retrieve the exception""" if self.exc_info: try: six.reraise(*self.exc_info) except Exception as e: return e
python
def get_exception(self): """Retrieve the exception""" if self.exc_info: try: six.reraise(*self.exc_info) except Exception as e: return e
[ "def", "get_exception", "(", "self", ")", ":", "if", "self", ".", "exc_info", ":", "try", ":", "six", ".", "reraise", "(", "*", "self", ".", "exc_info", ")", "except", "Exception", "as", "e", ":", "return", "e" ]
Retrieve the exception
[ "Retrieve", "the", "exception" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/decorator.py#L72-L78
capless/sammy
sammy/__init__.py
SAM.check_global_valid
def check_global_valid(self): """ Makes sure there aren't any SAM resources in a template that will be used in a CloudFormation StackSet :return: bool """ serverless_cnt = len(list(filter(lambda x: x._serverless_type, self._data['resources']))) if serverless_cnt > 0: return False return True
python
def check_global_valid(self): """ Makes sure there aren't any SAM resources in a template that will be used in a CloudFormation StackSet :return: bool """ serverless_cnt = len(list(filter(lambda x: x._serverless_type, self._data['resources']))) if serverless_cnt > 0: return False return True
[ "def", "check_global_valid", "(", "self", ")", ":", "serverless_cnt", "=", "len", "(", "list", "(", "filter", "(", "lambda", "x", ":", "x", ".", "_serverless_type", ",", "self", ".", "_data", "[", "'resources'", "]", ")", ")", ")", "if", "serverless_cnt"...
Makes sure there aren't any SAM resources in a template that will be used in a CloudFormation StackSet :return: bool
[ "Makes", "sure", "there", "aren", "t", "any", "SAM", "resources", "in", "a", "template", "that", "will", "be", "used", "in", "a", "CloudFormation", "StackSet", ":", "return", ":", "bool" ]
train
https://github.com/capless/sammy/blob/4fa9680ccfad108537de7de08e70644ac13bc8e8/sammy/__init__.py#L433-L441
capless/sammy
sammy/__init__.py
SAM.has_stack
def has_stack(self, stack_name): """ Checks if a CloudFormation stack with given name exists :param stack_name: Name or ID of the stack :return: True if stack exists. False otherwise """ cf = self.cf_client try: resp = cf.describe_stacks(StackName=stack_name) if len(resp["Stacks"]) != 1: return False # When you run CreateChangeSet on a a stack that does not exist, # CloudFormation will create a stack and set it's status # REVIEW_IN_PROGRESS. However this stack is cannot be manipulated # by "update" commands. Under this circumstances, we treat like # this stack does not exist and call CreateChangeSet will # ChangeSetType set to CREATE and not UPDATE. stack = resp["Stacks"][0] return stack["StackStatus"] != "REVIEW_IN_PROGRESS" except botocore.exceptions.ClientError as e: # If a stack does not exist, describe_stacks will throw an # exception. Unfortunately we don't have a better way than parsing # the exception msg to understand the nature of this exception. msg = str(e) if "Stack with id {0} does not exist".format(stack_name) in msg: LOG.debug("Stack with id {0} does not exist".format( stack_name)) return False else: # We don't know anything about this exception. Don't handle LOG.debug("Unable to get stack details.", exc_info=e) raise e
python
def has_stack(self, stack_name): """ Checks if a CloudFormation stack with given name exists :param stack_name: Name or ID of the stack :return: True if stack exists. False otherwise """ cf = self.cf_client try: resp = cf.describe_stacks(StackName=stack_name) if len(resp["Stacks"]) != 1: return False # When you run CreateChangeSet on a a stack that does not exist, # CloudFormation will create a stack and set it's status # REVIEW_IN_PROGRESS. However this stack is cannot be manipulated # by "update" commands. Under this circumstances, we treat like # this stack does not exist and call CreateChangeSet will # ChangeSetType set to CREATE and not UPDATE. stack = resp["Stacks"][0] return stack["StackStatus"] != "REVIEW_IN_PROGRESS" except botocore.exceptions.ClientError as e: # If a stack does not exist, describe_stacks will throw an # exception. Unfortunately we don't have a better way than parsing # the exception msg to understand the nature of this exception. msg = str(e) if "Stack with id {0} does not exist".format(stack_name) in msg: LOG.debug("Stack with id {0} does not exist".format( stack_name)) return False else: # We don't know anything about this exception. Don't handle LOG.debug("Unable to get stack details.", exc_info=e) raise e
[ "def", "has_stack", "(", "self", ",", "stack_name", ")", ":", "cf", "=", "self", ".", "cf_client", "try", ":", "resp", "=", "cf", ".", "describe_stacks", "(", "StackName", "=", "stack_name", ")", "if", "len", "(", "resp", "[", "\"Stacks\"", "]", ")", ...
Checks if a CloudFormation stack with given name exists :param stack_name: Name or ID of the stack :return: True if stack exists. False otherwise
[ "Checks", "if", "a", "CloudFormation", "stack", "with", "given", "name", "exists", ":", "param", "stack_name", ":", "Name", "or", "ID", "of", "the", "stack", ":", "return", ":", "True", "if", "stack", "exists", ".", "False", "otherwise" ]
train
https://github.com/capless/sammy/blob/4fa9680ccfad108537de7de08e70644ac13bc8e8/sammy/__init__.py#L480-L514
mhostetter/nhl
nhl/flyweight.py
Flyweight.has_key
def has_key(cls, *args): """ Check whether flyweight object with specified key has already been created. Returns: bool: True if already created, False if not """ key = args if len(args) > 1 else args[0] return key in cls._instances
python
def has_key(cls, *args): """ Check whether flyweight object with specified key has already been created. Returns: bool: True if already created, False if not """ key = args if len(args) > 1 else args[0] return key in cls._instances
[ "def", "has_key", "(", "cls", ",", "*", "args", ")", ":", "key", "=", "args", "if", "len", "(", "args", ")", ">", "1", "else", "args", "[", "0", "]", "return", "key", "in", "cls", ".", "_instances" ]
Check whether flyweight object with specified key has already been created. Returns: bool: True if already created, False if not
[ "Check", "whether", "flyweight", "object", "with", "specified", "key", "has", "already", "been", "created", "." ]
train
https://github.com/mhostetter/nhl/blob/32c91cc392826e9de728563d57ab527421734ee1/nhl/flyweight.py#L42-L50
mhostetter/nhl
nhl/flyweight.py
Flyweight.from_key
def from_key(cls, *args): """ Return flyweight object with specified key, if it has already been created. Returns: cls or None: Previously constructed flyweight object with given key or None if key not found """ key = args if len(args) > 1 else args[0] return cls._instances.get(key, None)
python
def from_key(cls, *args): """ Return flyweight object with specified key, if it has already been created. Returns: cls or None: Previously constructed flyweight object with given key or None if key not found """ key = args if len(args) > 1 else args[0] return cls._instances.get(key, None)
[ "def", "from_key", "(", "cls", ",", "*", "args", ")", ":", "key", "=", "args", "if", "len", "(", "args", ")", ">", "1", "else", "args", "[", "0", "]", "return", "cls", ".", "_instances", ".", "get", "(", "key", ",", "None", ")" ]
Return flyweight object with specified key, if it has already been created. Returns: cls or None: Previously constructed flyweight object with given key or None if key not found
[ "Return", "flyweight", "object", "with", "specified", "key", "if", "it", "has", "already", "been", "created", "." ]
train
https://github.com/mhostetter/nhl/blob/32c91cc392826e9de728563d57ab527421734ee1/nhl/flyweight.py#L53-L62
kpn-digital/py-timeexecution
time_execution/backends/elasticsearch.py
ElasticsearchBackend.write
def write(self, name, **data): """ Write the metric to elasticsearch Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric """ data["name"] = name if not ("timestamp" in data): data["timestamp"] = datetime.utcnow() try: self.client.index( index=self.get_index(), doc_type=self.doc_type, id=None, body=data ) except TransportError as exc: logger.warning('writing metric %r failure %r', data, exc)
python
def write(self, name, **data): """ Write the metric to elasticsearch Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric """ data["name"] = name if not ("timestamp" in data): data["timestamp"] = datetime.utcnow() try: self.client.index( index=self.get_index(), doc_type=self.doc_type, id=None, body=data ) except TransportError as exc: logger.warning('writing metric %r failure %r', data, exc)
[ "def", "write", "(", "self", ",", "name", ",", "*", "*", "data", ")", ":", "data", "[", "\"name\"", "]", "=", "name", "if", "not", "(", "\"timestamp\"", "in", "data", ")", ":", "data", "[", "\"timestamp\"", "]", "=", "datetime", ".", "utcnow", "(",...
Write the metric to elasticsearch Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric
[ "Write", "the", "metric", "to", "elasticsearch" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/backends/elasticsearch.py#L87-L108
kpn-digital/py-timeexecution
time_execution/backends/elasticsearch.py
ElasticsearchBackend.bulk_write
def bulk_write(self, metrics): """ Write multiple metrics to elasticsearch in one request Args: metrics (list): data with mappings to send to elasticsearch """ actions = [] index = self.get_index() for metric in metrics: actions.append({'index': {'_index': index, '_type': self.doc_type}}) actions.append(metric) try: self.client.bulk(actions) except TransportError as exc: logger.warning('bulk_write metrics %r failure %r', metrics, exc)
python
def bulk_write(self, metrics): """ Write multiple metrics to elasticsearch in one request Args: metrics (list): data with mappings to send to elasticsearch """ actions = [] index = self.get_index() for metric in metrics: actions.append({'index': {'_index': index, '_type': self.doc_type}}) actions.append(metric) try: self.client.bulk(actions) except TransportError as exc: logger.warning('bulk_write metrics %r failure %r', metrics, exc)
[ "def", "bulk_write", "(", "self", ",", "metrics", ")", ":", "actions", "=", "[", "]", "index", "=", "self", ".", "get_index", "(", ")", "for", "metric", "in", "metrics", ":", "actions", ".", "append", "(", "{", "'index'", ":", "{", "'_index'", ":", ...
Write multiple metrics to elasticsearch in one request Args: metrics (list): data with mappings to send to elasticsearch
[ "Write", "multiple", "metrics", "to", "elasticsearch", "in", "one", "request" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/backends/elasticsearch.py#L110-L125
ZELLMECHANIK-DRESDEN/dclab
dclab/features/bright.py
get_bright
def get_bright(mask, image, ret_data="avg,sd"): """Compute avg and/or std of the event brightness The event brightness is defined by the gray-scale values of the image data within the event mask area. Parameters ---------- mask: ndarray or list of ndarrays of shape (M,N) and dtype bool The mask values, True where the event is located in `image`. image: ndarray or list of ndarrays of shape (M,N) A 2D array that holds the image in form of grayscale values of an event. ret_data: str A comma-separated list of metrices to compute - "avg": compute the average - "sd": compute the standard deviation Selected metrics are returned in alphabetical order. Returns ------- bright_avg: float or ndarray of size N Average image data within the contour bright_std: float or ndarray of size N Standard deviation of image data within the contour """ # This method is based on a pull request by Maik Herbig. ret_avg = "avg" in ret_data ret_std = "sd" in ret_data if ret_avg + ret_std == 0: raise ValueError("No valid metrices selected!") if isinstance(mask, np.ndarray) and len(mask.shape) == 2: # We have a single image image = [image] mask = [mask] ret_list = False else: ret_list = True length = min(len(mask), len(image)) # Results are stored in a separate array initialized with nans if ret_avg: avg = np.zeros(length, dtype=float) * np.nan if ret_std: std = np.zeros(length, dtype=float) * np.nan for ii in range(length): imgi = image[ii] mski = mask[ii] # Assign results if ret_avg: avg[ii] = np.mean(imgi[mski]) if ret_std: std[ii] = np.std(imgi[mski]) results = [] # Keep alphabetical order if ret_avg: results.append(avg) if ret_std: results.append(std) if not ret_list: # Only return scalars results = [r[0] for r in results] if ret_avg+ret_std == 1: # Only return one column return results[0] return results
python
def get_bright(mask, image, ret_data="avg,sd"): """Compute avg and/or std of the event brightness The event brightness is defined by the gray-scale values of the image data within the event mask area. Parameters ---------- mask: ndarray or list of ndarrays of shape (M,N) and dtype bool The mask values, True where the event is located in `image`. image: ndarray or list of ndarrays of shape (M,N) A 2D array that holds the image in form of grayscale values of an event. ret_data: str A comma-separated list of metrices to compute - "avg": compute the average - "sd": compute the standard deviation Selected metrics are returned in alphabetical order. Returns ------- bright_avg: float or ndarray of size N Average image data within the contour bright_std: float or ndarray of size N Standard deviation of image data within the contour """ # This method is based on a pull request by Maik Herbig. ret_avg = "avg" in ret_data ret_std = "sd" in ret_data if ret_avg + ret_std == 0: raise ValueError("No valid metrices selected!") if isinstance(mask, np.ndarray) and len(mask.shape) == 2: # We have a single image image = [image] mask = [mask] ret_list = False else: ret_list = True length = min(len(mask), len(image)) # Results are stored in a separate array initialized with nans if ret_avg: avg = np.zeros(length, dtype=float) * np.nan if ret_std: std = np.zeros(length, dtype=float) * np.nan for ii in range(length): imgi = image[ii] mski = mask[ii] # Assign results if ret_avg: avg[ii] = np.mean(imgi[mski]) if ret_std: std[ii] = np.std(imgi[mski]) results = [] # Keep alphabetical order if ret_avg: results.append(avg) if ret_std: results.append(std) if not ret_list: # Only return scalars results = [r[0] for r in results] if ret_avg+ret_std == 1: # Only return one column return results[0] return results
[ "def", "get_bright", "(", "mask", ",", "image", ",", "ret_data", "=", "\"avg,sd\"", ")", ":", "# This method is based on a pull request by Maik Herbig.", "ret_avg", "=", "\"avg\"", "in", "ret_data", "ret_std", "=", "\"sd\"", "in", "ret_data", "if", "ret_avg", "+", ...
Compute avg and/or std of the event brightness The event brightness is defined by the gray-scale values of the image data within the event mask area. Parameters ---------- mask: ndarray or list of ndarrays of shape (M,N) and dtype bool The mask values, True where the event is located in `image`. image: ndarray or list of ndarrays of shape (M,N) A 2D array that holds the image in form of grayscale values of an event. ret_data: str A comma-separated list of metrices to compute - "avg": compute the average - "sd": compute the standard deviation Selected metrics are returned in alphabetical order. Returns ------- bright_avg: float or ndarray of size N Average image data within the contour bright_std: float or ndarray of size N Standard deviation of image data within the contour
[ "Compute", "avg", "and", "/", "or", "std", "of", "the", "event", "brightness" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/features/bright.py#L12-L85
openstax/cnx-archive
cnxarchive/cache.py
search
def search(query, query_type, nocache=False): """Search archive contents. Look up search results in cache, if not in cache, do a database search and cache the result """ settings = get_current_registry().settings memcache_servers = settings['memcache-servers'].split() if not memcache_servers: # memcache is not enabled, do a database search directly return database_search(query, query_type) # sort query params and create a key for the search search_params = [] search_params += copy.deepcopy(query.terms) search_params += copy.deepcopy(query.filters) search_params += [('sort', i) for i in query.sorts] search_params.sort(key=lambda record: (record[0], record[1])) search_params.append(('query_type', query_type)) # search_key should look something like: # '"sort:pubDate" "text:college physics" "query_type:weakAND"' search_key = u' '.join([u'"{}"'.format(u':'.join(param)) for param in search_params]) # hash the search_key so it never exceeds the key length limit (250) in # memcache mc_search_key = binascii.hexlify( hashlib.pbkdf2_hmac('sha1', search_key.encode('utf-8'), b'', 1)) # look for search results in memcache first, unless nocache mc = memcache.Client(memcache_servers, server_max_value_length=128*1024*1024, debug=0) if not nocache: search_results = mc.get(mc_search_key) else: search_results = None if not search_results: # search results is not in memcache, do a database search search_results = database_search(query, query_type) cache_length = int(settings['search-cache-expiration']) # for particular searches, store in memcache for longer if (len(search_params) == 2 and # search by subject search_params[0][0] == 'subject' or # search single terms search_params[0][0] == 'text' and ' ' not in search_params[0][1]): # search with one term or one filter, plus query_type cache_length = int(settings['search-long-cache-expiration']) # store in memcache mc.set(mc_search_key, search_results, time=cache_length, min_compress_len=1024*1024) # compress when > 1MB # return search results return search_results
python
def search(query, query_type, nocache=False): """Search archive contents. Look up search results in cache, if not in cache, do a database search and cache the result """ settings = get_current_registry().settings memcache_servers = settings['memcache-servers'].split() if not memcache_servers: # memcache is not enabled, do a database search directly return database_search(query, query_type) # sort query params and create a key for the search search_params = [] search_params += copy.deepcopy(query.terms) search_params += copy.deepcopy(query.filters) search_params += [('sort', i) for i in query.sorts] search_params.sort(key=lambda record: (record[0], record[1])) search_params.append(('query_type', query_type)) # search_key should look something like: # '"sort:pubDate" "text:college physics" "query_type:weakAND"' search_key = u' '.join([u'"{}"'.format(u':'.join(param)) for param in search_params]) # hash the search_key so it never exceeds the key length limit (250) in # memcache mc_search_key = binascii.hexlify( hashlib.pbkdf2_hmac('sha1', search_key.encode('utf-8'), b'', 1)) # look for search results in memcache first, unless nocache mc = memcache.Client(memcache_servers, server_max_value_length=128*1024*1024, debug=0) if not nocache: search_results = mc.get(mc_search_key) else: search_results = None if not search_results: # search results is not in memcache, do a database search search_results = database_search(query, query_type) cache_length = int(settings['search-cache-expiration']) # for particular searches, store in memcache for longer if (len(search_params) == 2 and # search by subject search_params[0][0] == 'subject' or # search single terms search_params[0][0] == 'text' and ' ' not in search_params[0][1]): # search with one term or one filter, plus query_type cache_length = int(settings['search-long-cache-expiration']) # store in memcache mc.set(mc_search_key, search_results, time=cache_length, min_compress_len=1024*1024) # compress when > 1MB # return search results return search_results
[ "def", "search", "(", "query", ",", "query_type", ",", "nocache", "=", "False", ")", ":", "settings", "=", "get_current_registry", "(", ")", ".", "settings", "memcache_servers", "=", "settings", "[", "'memcache-servers'", "]", ".", "split", "(", ")", "if", ...
Search archive contents. Look up search results in cache, if not in cache, do a database search and cache the result
[ "Search", "archive", "contents", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/cache.py#L20-L79
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/ancillaries/ancillary_feature.py
AncillaryFeature.available_features
def available_features(rtdc_ds): """Determine available features for an RT-DC dataset Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- features: dict Dictionary with feature names as keys and instances of `AncillaryFeature` as values. """ cols = {} for inst in AncillaryFeature.features: if inst.is_available(rtdc_ds): cols[inst.feature_name] = inst return cols
python
def available_features(rtdc_ds): """Determine available features for an RT-DC dataset Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- features: dict Dictionary with feature names as keys and instances of `AncillaryFeature` as values. """ cols = {} for inst in AncillaryFeature.features: if inst.is_available(rtdc_ds): cols[inst.feature_name] = inst return cols
[ "def", "available_features", "(", "rtdc_ds", ")", ":", "cols", "=", "{", "}", "for", "inst", "in", "AncillaryFeature", ".", "features", ":", "if", "inst", ".", "is_available", "(", "rtdc_ds", ")", ":", "cols", "[", "inst", ".", "feature_name", "]", "=", ...
Determine available features for an RT-DC dataset Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- features: dict Dictionary with feature names as keys and instances of `AncillaryFeature` as values.
[ "Determine", "available", "features", "for", "an", "RT", "-", "DC", "dataset" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/ancillaries/ancillary_feature.py#L85-L103
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/ancillaries/ancillary_feature.py
AncillaryFeature.compute
def compute(self, rtdc_ds): """Compute the feature with self.method Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to compute the feature for Returns ------- feature: array- or list-like The computed data feature (read-only). """ data = self.method(rtdc_ds) dsize = len(rtdc_ds) - len(data) if dsize > 0: msg = "Growing feature {} in {} by {} to match event number!" warnings.warn(msg.format(self.feature_name, rtdc_ds, abs(dsize)), BadFeatureSizeWarning) data.resize(len(rtdc_ds), refcheck=False) data[-dsize:] = np.nan elif dsize < 0: msg = "Shrinking feature {} in {} by {} to match event number!" warnings.warn(msg.format(self.feature_name, rtdc_ds, abs(dsize)), BadFeatureSizeWarning) data.resize(len(rtdc_ds), refcheck=False) if isinstance(data, np.ndarray): data.setflags(write=False) elif isinstance(data, list): for item in data: if isinstance(item, np.ndarray): item.setflags(write=False) return data
python
def compute(self, rtdc_ds): """Compute the feature with self.method Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to compute the feature for Returns ------- feature: array- or list-like The computed data feature (read-only). """ data = self.method(rtdc_ds) dsize = len(rtdc_ds) - len(data) if dsize > 0: msg = "Growing feature {} in {} by {} to match event number!" warnings.warn(msg.format(self.feature_name, rtdc_ds, abs(dsize)), BadFeatureSizeWarning) data.resize(len(rtdc_ds), refcheck=False) data[-dsize:] = np.nan elif dsize < 0: msg = "Shrinking feature {} in {} by {} to match event number!" warnings.warn(msg.format(self.feature_name, rtdc_ds, abs(dsize)), BadFeatureSizeWarning) data.resize(len(rtdc_ds), refcheck=False) if isinstance(data, np.ndarray): data.setflags(write=False) elif isinstance(data, list): for item in data: if isinstance(item, np.ndarray): item.setflags(write=False) return data
[ "def", "compute", "(", "self", ",", "rtdc_ds", ")", ":", "data", "=", "self", ".", "method", "(", "rtdc_ds", ")", "dsize", "=", "len", "(", "rtdc_ds", ")", "-", "len", "(", "data", ")", "if", "dsize", ">", "0", ":", "msg", "=", "\"Growing feature {...
Compute the feature with self.method Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to compute the feature for Returns ------- feature: array- or list-like The computed data feature (read-only).
[ "Compute", "the", "feature", "with", "self", ".", "method" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/ancillaries/ancillary_feature.py#L105-L140
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/ancillaries/ancillary_feature.py
AncillaryFeature.get_instances
def get_instances(feature_name): """Return all all instances that compute `feature_name`""" feats = [] for ft in AncillaryFeature.features: if ft.feature_name == feature_name: feats.append(ft) return feats
python
def get_instances(feature_name): """Return all all instances that compute `feature_name`""" feats = [] for ft in AncillaryFeature.features: if ft.feature_name == feature_name: feats.append(ft) return feats
[ "def", "get_instances", "(", "feature_name", ")", ":", "feats", "=", "[", "]", "for", "ft", "in", "AncillaryFeature", ".", "features", ":", "if", "ft", ".", "feature_name", "==", "feature_name", ":", "feats", ".", "append", "(", "ft", ")", "return", "fea...
Return all all instances that compute `feature_name`
[ "Return", "all", "all", "instances", "that", "compute", "feature_name" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/ancillaries/ancillary_feature.py#L143-L149
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/ancillaries/ancillary_feature.py
AncillaryFeature.hash
def hash(self, rtdc_ds): """Used for identifying an ancillary computation The data columns and the used configuration keys/values are hashed. """ hasher = hashlib.md5() # data columns for col in self.req_features: hasher.update(obj2str(rtdc_ds[col])) # config keys for sec, keys in self.req_config: for key in keys: val = rtdc_ds.config[sec][key] data = "{}:{}={}".format(sec, key, val) hasher.update(obj2str(data)) return hasher.hexdigest()
python
def hash(self, rtdc_ds): """Used for identifying an ancillary computation The data columns and the used configuration keys/values are hashed. """ hasher = hashlib.md5() # data columns for col in self.req_features: hasher.update(obj2str(rtdc_ds[col])) # config keys for sec, keys in self.req_config: for key in keys: val = rtdc_ds.config[sec][key] data = "{}:{}={}".format(sec, key, val) hasher.update(obj2str(data)) return hasher.hexdigest()
[ "def", "hash", "(", "self", ",", "rtdc_ds", ")", ":", "hasher", "=", "hashlib", ".", "md5", "(", ")", "# data columns", "for", "col", "in", "self", ".", "req_features", ":", "hasher", ".", "update", "(", "obj2str", "(", "rtdc_ds", "[", "col", "]", ")...
Used for identifying an ancillary computation The data columns and the used configuration keys/values are hashed.
[ "Used", "for", "identifying", "an", "ancillary", "computation" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/ancillaries/ancillary_feature.py#L151-L167
ZELLMECHANIK-DRESDEN/dclab
dclab/rtdc_dataset/ancillaries/ancillary_feature.py
AncillaryFeature.is_available
def is_available(self, rtdc_ds, verbose=False): """Check whether the feature is available Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- available: bool `True`, if feature can be computed with `compute` Notes ----- This method returns `False` for a feature if there is a feature defined with the same name but with higher priority (even if the feature would be available otherwise). """ # Check config keys for item in self.req_config: section, keys = item if section not in rtdc_ds.config: if verbose: print("{} not in config".format(section)) return False else: for key in keys: if key not in rtdc_ds.config[section]: if verbose: print("{} not in config['{}']".format(key, section)) return False # Check features for col in self.req_features: if col not in rtdc_ds: return False # Check priorities of other features for of in AncillaryFeature.features: if of == self: # nothing to compare continue elif of.feature_name == self.feature_name: # same feature name if of.priority <= self.priority: # lower priority, ignore continue else: # higher priority if of.is_available(rtdc_ds): # higher priority is available, thus # this feature is not available return False else: # higher priority not available continue else: # other feature continue return True
python
def is_available(self, rtdc_ds, verbose=False): """Check whether the feature is available Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- available: bool `True`, if feature can be computed with `compute` Notes ----- This method returns `False` for a feature if there is a feature defined with the same name but with higher priority (even if the feature would be available otherwise). """ # Check config keys for item in self.req_config: section, keys = item if section not in rtdc_ds.config: if verbose: print("{} not in config".format(section)) return False else: for key in keys: if key not in rtdc_ds.config[section]: if verbose: print("{} not in config['{}']".format(key, section)) return False # Check features for col in self.req_features: if col not in rtdc_ds: return False # Check priorities of other features for of in AncillaryFeature.features: if of == self: # nothing to compare continue elif of.feature_name == self.feature_name: # same feature name if of.priority <= self.priority: # lower priority, ignore continue else: # higher priority if of.is_available(rtdc_ds): # higher priority is available, thus # this feature is not available return False else: # higher priority not available continue else: # other feature continue return True
[ "def", "is_available", "(", "self", ",", "rtdc_ds", ",", "verbose", "=", "False", ")", ":", "# Check config keys", "for", "item", "in", "self", ".", "req_config", ":", "section", ",", "keys", "=", "item", "if", "section", "not", "in", "rtdc_ds", ".", "co...
Check whether the feature is available Parameters ---------- rtdc_ds: instance of RTDCBase The dataset to check availability for Returns ------- available: bool `True`, if feature can be computed with `compute` Notes ----- This method returns `False` for a feature if there is a feature defined with the same name but with higher priority (even if the feature would be available otherwise).
[ "Check", "whether", "the", "feature", "is", "available" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/rtdc_dataset/ancillaries/ancillary_feature.py#L169-L229
kpn-digital/py-timeexecution
time_execution/backends/kafka.py
KafkaBackend.producer
def producer(self): """ :raises: kafka.errors.NoBrokersAvailable if the connection is broken """ if self._producer: return self._producer self._producer = KafkaProducer( bootstrap_servers=self.hosts, value_serializer=lambda v: self._serializer_class().dumps(v).encode('utf-8'), **self._kwargs ) return self._producer
python
def producer(self): """ :raises: kafka.errors.NoBrokersAvailable if the connection is broken """ if self._producer: return self._producer self._producer = KafkaProducer( bootstrap_servers=self.hosts, value_serializer=lambda v: self._serializer_class().dumps(v).encode('utf-8'), **self._kwargs ) return self._producer
[ "def", "producer", "(", "self", ")", ":", "if", "self", ".", "_producer", ":", "return", "self", ".", "_producer", "self", ".", "_producer", "=", "KafkaProducer", "(", "bootstrap_servers", "=", "self", ".", "hosts", ",", "value_serializer", "=", "lambda", ...
:raises: kafka.errors.NoBrokersAvailable if the connection is broken
[ ":", "raises", ":", "kafka", ".", "errors", ".", "NoBrokersAvailable", "if", "the", "connection", "is", "broken" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/backends/kafka.py#L35-L48
kpn-digital/py-timeexecution
time_execution/backends/kafka.py
KafkaBackend.write
def write(self, name, **data): """ Write the metric to kafka Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric """ data["name"] = name if not ("timestamp" in data): data["timestamp"] = datetime.utcnow() try: self.producer.send(topic=self.topic, value=data) self.producer.flush() except (KafkaTimeoutError, NoBrokersAvailable) as exc: logger.warning('writing metric %r failure %r', data, exc)
python
def write(self, name, **data): """ Write the metric to kafka Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric """ data["name"] = name if not ("timestamp" in data): data["timestamp"] = datetime.utcnow() try: self.producer.send(topic=self.topic, value=data) self.producer.flush() except (KafkaTimeoutError, NoBrokersAvailable) as exc: logger.warning('writing metric %r failure %r', data, exc)
[ "def", "write", "(", "self", ",", "name", ",", "*", "*", "data", ")", ":", "data", "[", "\"name\"", "]", "=", "name", "if", "not", "(", "\"timestamp\"", "in", "data", ")", ":", "data", "[", "\"timestamp\"", "]", "=", "datetime", ".", "utcnow", "(",...
Write the metric to kafka Args: name (str): The name of the metric to write data (dict): Additional data to store with the metric
[ "Write", "the", "metric", "to", "kafka" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/backends/kafka.py#L50-L67
kpn-digital/py-timeexecution
time_execution/backends/kafka.py
KafkaBackend.bulk_write
def bulk_write(self, metrics): """ Write multiple metrics to kafka in one request Args: metrics (list): """ try: for metric in metrics: self.producer.send(self.topic, metric) self.producer.flush() except (KafkaTimeoutError, NoBrokersAvailable) as exc: logger.warning('bulk_write metrics %r failure %r', metrics, exc)
python
def bulk_write(self, metrics): """ Write multiple metrics to kafka in one request Args: metrics (list): """ try: for metric in metrics: self.producer.send(self.topic, metric) self.producer.flush() except (KafkaTimeoutError, NoBrokersAvailable) as exc: logger.warning('bulk_write metrics %r failure %r', metrics, exc)
[ "def", "bulk_write", "(", "self", ",", "metrics", ")", ":", "try", ":", "for", "metric", "in", "metrics", ":", "self", ".", "producer", ".", "send", "(", "self", ".", "topic", ",", "metric", ")", "self", ".", "producer", ".", "flush", "(", ")", "ex...
Write multiple metrics to kafka in one request Args: metrics (list):
[ "Write", "multiple", "metrics", "to", "kafka", "in", "one", "request" ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/time_execution/backends/kafka.py#L69-L81
kpn-digital/py-timeexecution
docs/apidoc.py
create_module_file
def create_module_file(package, module, opts): """Build the text of the file and write the file.""" if not opts.noheadings: text = format_heading(1, '%s module' % module) else: text = '' # text += format_heading(2, ':mod:`%s` Module' % module) text += format_directive(module, package) write_file(makename(package, module), text, opts)
python
def create_module_file(package, module, opts): """Build the text of the file and write the file.""" if not opts.noheadings: text = format_heading(1, '%s module' % module) else: text = '' # text += format_heading(2, ':mod:`%s` Module' % module) text += format_directive(module, package) write_file(makename(package, module), text, opts)
[ "def", "create_module_file", "(", "package", ",", "module", ",", "opts", ")", ":", "if", "not", "opts", ".", "noheadings", ":", "text", "=", "format_heading", "(", "1", ",", "'%s module'", "%", "module", ")", "else", ":", "text", "=", "''", "# text += fo...
Build the text of the file and write the file.
[ "Build", "the", "text", "of", "the", "file", "and", "write", "the", "file", "." ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/docs/apidoc.py#L85-L93
kpn-digital/py-timeexecution
docs/apidoc.py
create_package_file
def create_package_file(root, master_package, subroot, py_files, opts, subs): """Build the text of the file and write the file.""" text = format_heading(1, '%s' % makename(master_package, subroot)) if opts.modulefirst: text += format_directive(subroot, master_package) text += '\n' # build a list of directories that are szvpackages (contain an INITPY file) subs = [sub for sub in subs if path.isfile(path.join(root, sub, INITPY))] # if there are some package directories, add a TOC for theses subpackages if subs: # text += format_heading(2, 'Subpackages') text += '.. toctree::\n\n' for sub in subs: text += ' %s.%s\n' % (makename(master_package, subroot), sub) text += '\n' submods = [path.splitext(sub)[0] for sub in py_files if not shall_skip(path.join(root, sub), opts) and sub != INITPY] if submods: #text += format_heading(2, 'Submodules') if opts.separatemodules: text += '.. toctree::\n\n' for submod in submods: modfile = makename(master_package, makename(subroot, submod)) text += ' %s\n' % modfile # generate separate file for this module if not opts.noheadings: filetext = format_heading(1, '%s module' % modfile) else: filetext = '' filetext += format_directive(makename(subroot, submod), master_package) write_file(modfile, filetext, opts) else: for submod in submods: modfile = makename(master_package, makename(subroot, submod)) if not opts.noheadings: text += format_heading(2, '%s module' % modfile) text += format_directive(makename(subroot, submod), master_package) text += '\n' text += '\n' if not opts.modulefirst: text += format_heading(2, 'Module contents') text += format_directive(subroot, master_package) write_file(makename(master_package, subroot), text, opts)
python
def create_package_file(root, master_package, subroot, py_files, opts, subs): """Build the text of the file and write the file.""" text = format_heading(1, '%s' % makename(master_package, subroot)) if opts.modulefirst: text += format_directive(subroot, master_package) text += '\n' # build a list of directories that are szvpackages (contain an INITPY file) subs = [sub for sub in subs if path.isfile(path.join(root, sub, INITPY))] # if there are some package directories, add a TOC for theses subpackages if subs: # text += format_heading(2, 'Subpackages') text += '.. toctree::\n\n' for sub in subs: text += ' %s.%s\n' % (makename(master_package, subroot), sub) text += '\n' submods = [path.splitext(sub)[0] for sub in py_files if not shall_skip(path.join(root, sub), opts) and sub != INITPY] if submods: #text += format_heading(2, 'Submodules') if opts.separatemodules: text += '.. toctree::\n\n' for submod in submods: modfile = makename(master_package, makename(subroot, submod)) text += ' %s\n' % modfile # generate separate file for this module if not opts.noheadings: filetext = format_heading(1, '%s module' % modfile) else: filetext = '' filetext += format_directive(makename(subroot, submod), master_package) write_file(modfile, filetext, opts) else: for submod in submods: modfile = makename(master_package, makename(subroot, submod)) if not opts.noheadings: text += format_heading(2, '%s module' % modfile) text += format_directive(makename(subroot, submod), master_package) text += '\n' text += '\n' if not opts.modulefirst: text += format_heading(2, 'Module contents') text += format_directive(subroot, master_package) write_file(makename(master_package, subroot), text, opts)
[ "def", "create_package_file", "(", "root", ",", "master_package", ",", "subroot", ",", "py_files", ",", "opts", ",", "subs", ")", ":", "text", "=", "format_heading", "(", "1", ",", "'%s'", "%", "makename", "(", "master_package", ",", "subroot", ")", ")", ...
Build the text of the file and write the file.
[ "Build", "the", "text", "of", "the", "file", "and", "write", "the", "file", "." ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/docs/apidoc.py#L96-L147
kpn-digital/py-timeexecution
docs/apidoc.py
shall_skip
def shall_skip(module, opts): """Check if we want to skip this module.""" # skip it if there is nothing (or just \n or \r\n) in the file if path.getsize(module) <= 2: return True # skip if it has a "private" name and this is selected filename = path.basename(module) if filename != '__init__.py' and filename.startswith('_') and \ not opts.includeprivate: return True return False
python
def shall_skip(module, opts): """Check if we want to skip this module.""" # skip it if there is nothing (or just \n or \r\n) in the file if path.getsize(module) <= 2: return True # skip if it has a "private" name and this is selected filename = path.basename(module) if filename != '__init__.py' and filename.startswith('_') and \ not opts.includeprivate: return True return False
[ "def", "shall_skip", "(", "module", ",", "opts", ")", ":", "# skip it if there is nothing (or just \\n or \\r\\n) in the file", "if", "path", ".", "getsize", "(", "module", ")", "<=", "2", ":", "return", "True", "# skip if it has a \"private\" name and this is selected", ...
Check if we want to skip this module.
[ "Check", "if", "we", "want", "to", "skip", "this", "module", "." ]
train
https://github.com/kpn-digital/py-timeexecution/blob/79b991e83f783196c41b830d0acef21ac5462596/docs/apidoc.py#L168-L178
openstax/cnx-archive
cnxarchive/utils/safe.py
safe_stat
def safe_stat(path, timeout=1, cmd=None): "Use threads and a subproc to bodge a timeout on top of filesystem access" global safe_stat_process if cmd is None: cmd = ['/usr/bin/stat'] cmd.append(path) def target(): global safe_stat_process logger.debug('Stat thread started') safe_stat_process = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE) _results = safe_stat_process.communicate() # noqa logger.debug('Stat thread finished') thread = threading.Thread(target=target) thread.start() thread.join(timeout) if thread.is_alive(): # stat took longer than timeout safe_stat_process.terminate() thread.join() return safe_stat_process.returncode == 0
python
def safe_stat(path, timeout=1, cmd=None): "Use threads and a subproc to bodge a timeout on top of filesystem access" global safe_stat_process if cmd is None: cmd = ['/usr/bin/stat'] cmd.append(path) def target(): global safe_stat_process logger.debug('Stat thread started') safe_stat_process = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE) _results = safe_stat_process.communicate() # noqa logger.debug('Stat thread finished') thread = threading.Thread(target=target) thread.start() thread.join(timeout) if thread.is_alive(): # stat took longer than timeout safe_stat_process.terminate() thread.join() return safe_stat_process.returncode == 0
[ "def", "safe_stat", "(", "path", ",", "timeout", "=", "1", ",", "cmd", "=", "None", ")", ":", "global", "safe_stat_process", "if", "cmd", "is", "None", ":", "cmd", "=", "[", "'/usr/bin/stat'", "]", "cmd", ".", "append", "(", "path", ")", "def", "targ...
Use threads and a subproc to bodge a timeout on top of filesystem access
[ "Use", "threads", "and", "a", "subproc", "to", "bodge", "a", "timeout", "on", "top", "of", "filesystem", "access" ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/utils/safe.py#L12-L36
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
get_polygon_filter_names
def get_polygon_filter_names(): """Get the names of all polygon filters in the order of creation""" names = [] for p in PolygonFilter.instances: names.append(p.name) return names
python
def get_polygon_filter_names(): """Get the names of all polygon filters in the order of creation""" names = [] for p in PolygonFilter.instances: names.append(p.name) return names
[ "def", "get_polygon_filter_names", "(", ")", ":", "names", "=", "[", "]", "for", "p", "in", "PolygonFilter", ".", "instances", ":", "names", ".", "append", "(", "p", ".", "name", ")", "return", "names" ]
Get the names of all polygon filters in the order of creation
[ "Get", "the", "names", "of", "all", "polygon", "filters", "in", "the", "order", "of", "creation" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L366-L371
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter._check_data
def _check_data(self): """Check if the data given is valid""" if self.axes is None: raise PolygonFilterError("`axes` parm not set.") if self.points is None: raise PolygonFilterError("`points` parm not set.") self.points = np.array(self.points) if self.points.shape[1] != 2: raise PolygonFilterError("data points' shape[1] must be 2.") if self.name is None: self.name = "polygon filter {}".format(self.unique_id) if not isinstance(self.inverted, bool): raise PolygonFilterError("`inverted` must be boolean.")
python
def _check_data(self): """Check if the data given is valid""" if self.axes is None: raise PolygonFilterError("`axes` parm not set.") if self.points is None: raise PolygonFilterError("`points` parm not set.") self.points = np.array(self.points) if self.points.shape[1] != 2: raise PolygonFilterError("data points' shape[1] must be 2.") if self.name is None: self.name = "polygon filter {}".format(self.unique_id) if not isinstance(self.inverted, bool): raise PolygonFilterError("`inverted` must be boolean.")
[ "def", "_check_data", "(", "self", ")", ":", "if", "self", ".", "axes", "is", "None", ":", "raise", "PolygonFilterError", "(", "\"`axes` parm not set.\"", ")", "if", "self", ".", "points", "is", "None", ":", "raise", "PolygonFilterError", "(", "\"`points` parm...
Check if the data given is valid
[ "Check", "if", "the", "data", "given", "is", "valid" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L92-L104
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter._load
def _load(self, filename): """Import all filters from a text file""" filename = pathlib.Path(filename) with filename.open() as fd: data = fd.readlines() # Get the strings that correspond to self.fileid bool_head = [l.strip().startswith("[") for l in data] int_head = np.squeeze(np.where(bool_head)) int_head = np.atleast_1d(int_head) start = int_head[self.fileid]+1 if len(int_head) > self.fileid+1: end = int_head[self.fileid+1] else: end = len(data) subdata = data[start:end] # separate all elements and strip them subdata = [[it.strip() for it in l.split("=")] for l in subdata] points = [] for var, val in subdata: if var.lower() == "x axis": xaxis = val.lower() elif var.lower() == "y axis": yaxis = val.lower() elif var.lower() == "name": self.name = val elif var.lower() == "inverted": if val == "True": self.inverted = True elif var.lower().startswith("point"): val = np.array(val.strip("[]").split(), dtype=float) points.append([int(var[5:]), val]) else: raise KeyError("Unknown variable: {} = {}". format(var, val)) self.axes = (xaxis, yaxis) # sort points points.sort() # get only coordinates from points self.points = np.array([p[1] for p in points]) # overwrite unique id unique_id = int(data[start-1].strip().strip("Polygon []")) self._set_unique_id(unique_id)
python
def _load(self, filename): """Import all filters from a text file""" filename = pathlib.Path(filename) with filename.open() as fd: data = fd.readlines() # Get the strings that correspond to self.fileid bool_head = [l.strip().startswith("[") for l in data] int_head = np.squeeze(np.where(bool_head)) int_head = np.atleast_1d(int_head) start = int_head[self.fileid]+1 if len(int_head) > self.fileid+1: end = int_head[self.fileid+1] else: end = len(data) subdata = data[start:end] # separate all elements and strip them subdata = [[it.strip() for it in l.split("=")] for l in subdata] points = [] for var, val in subdata: if var.lower() == "x axis": xaxis = val.lower() elif var.lower() == "y axis": yaxis = val.lower() elif var.lower() == "name": self.name = val elif var.lower() == "inverted": if val == "True": self.inverted = True elif var.lower().startswith("point"): val = np.array(val.strip("[]").split(), dtype=float) points.append([int(var[5:]), val]) else: raise KeyError("Unknown variable: {} = {}". format(var, val)) self.axes = (xaxis, yaxis) # sort points points.sort() # get only coordinates from points self.points = np.array([p[1] for p in points]) # overwrite unique id unique_id = int(data[start-1].strip().strip("Polygon []")) self._set_unique_id(unique_id)
[ "def", "_load", "(", "self", ",", "filename", ")", ":", "filename", "=", "pathlib", ".", "Path", "(", "filename", ")", "with", "filename", ".", "open", "(", ")", "as", "fd", ":", "data", "=", "fd", ".", "readlines", "(", ")", "# Get the strings that co...
Import all filters from a text file
[ "Import", "all", "filters", "from", "a", "text", "file" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L106-L156
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter._set_unique_id
def _set_unique_id(self, unique_id): """Define a unique id""" assert isinstance(unique_id, int), "unique_id must be an integer" if PolygonFilter.instace_exists(unique_id): newid = max(PolygonFilter._instance_counter, unique_id+1) msg = "PolygonFilter with unique_id '{}' exists.".format(unique_id) msg += " Using new unique id '{}'.".format(newid) warnings.warn(msg, FilterIdExistsWarning) unique_id = newid ic = max(PolygonFilter._instance_counter, unique_id+1) PolygonFilter._instance_counter = ic self.unique_id = unique_id
python
def _set_unique_id(self, unique_id): """Define a unique id""" assert isinstance(unique_id, int), "unique_id must be an integer" if PolygonFilter.instace_exists(unique_id): newid = max(PolygonFilter._instance_counter, unique_id+1) msg = "PolygonFilter with unique_id '{}' exists.".format(unique_id) msg += " Using new unique id '{}'.".format(newid) warnings.warn(msg, FilterIdExistsWarning) unique_id = newid ic = max(PolygonFilter._instance_counter, unique_id+1) PolygonFilter._instance_counter = ic self.unique_id = unique_id
[ "def", "_set_unique_id", "(", "self", ",", "unique_id", ")", ":", "assert", "isinstance", "(", "unique_id", ",", "int", ")", ",", "\"unique_id must be an integer\"", "if", "PolygonFilter", ".", "instace_exists", "(", "unique_id", ")", ":", "newid", "=", "max", ...
Define a unique id
[ "Define", "a", "unique", "id" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L158-L171
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.copy
def copy(self, invert=False): """Return a copy of the current instance Parameters ---------- invert: bool The copy will be inverted w.r.t. the original """ if invert: inverted = not self.inverted else: inverted = self.inverted return PolygonFilter(axes=self.axes, points=self.points, name=self.name, inverted=inverted)
python
def copy(self, invert=False): """Return a copy of the current instance Parameters ---------- invert: bool The copy will be inverted w.r.t. the original """ if invert: inverted = not self.inverted else: inverted = self.inverted return PolygonFilter(axes=self.axes, points=self.points, name=self.name, inverted=inverted)
[ "def", "copy", "(", "self", ",", "invert", "=", "False", ")", ":", "if", "invert", ":", "inverted", "=", "not", "self", ".", "inverted", "else", ":", "inverted", "=", "self", ".", "inverted", "return", "PolygonFilter", "(", "axes", "=", "self", ".", ...
Return a copy of the current instance Parameters ---------- invert: bool The copy will be inverted w.r.t. the original
[ "Return", "a", "copy", "of", "the", "current", "instance" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L179-L195
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.filter
def filter(self, datax, datay): """Filter a set of datax and datay according to `self.points`""" f = np.ones(datax.shape, dtype=bool) for i, p in enumerate(zip(datax, datay)): f[i] = PolygonFilter.point_in_poly(p, self.points) if self.inverted: np.invert(f, f) return f
python
def filter(self, datax, datay): """Filter a set of datax and datay according to `self.points`""" f = np.ones(datax.shape, dtype=bool) for i, p in enumerate(zip(datax, datay)): f[i] = PolygonFilter.point_in_poly(p, self.points) if self.inverted: np.invert(f, f) return f
[ "def", "filter", "(", "self", ",", "datax", ",", "datay", ")", ":", "f", "=", "np", ".", "ones", "(", "datax", ".", "shape", ",", "dtype", "=", "bool", ")", "for", "i", ",", "p", "in", "enumerate", "(", "zip", "(", "datax", ",", "datay", ")", ...
Filter a set of datax and datay according to `self.points`
[ "Filter", "a", "set", "of", "datax", "and", "datay", "according", "to", "self", ".", "points" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L197-L206
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.get_instance_from_id
def get_instance_from_id(unique_id): """Get an instance of the `PolygonFilter` using a unique id""" for instance in PolygonFilter.instances: if instance.unique_id == unique_id: return instance # if this does not work: raise KeyError("PolygonFilter with unique_id {} not found.". format(unique_id))
python
def get_instance_from_id(unique_id): """Get an instance of the `PolygonFilter` using a unique id""" for instance in PolygonFilter.instances: if instance.unique_id == unique_id: return instance # if this does not work: raise KeyError("PolygonFilter with unique_id {} not found.". format(unique_id))
[ "def", "get_instance_from_id", "(", "unique_id", ")", ":", "for", "instance", "in", "PolygonFilter", ".", "instances", ":", "if", "instance", ".", "unique_id", "==", "unique_id", ":", "return", "instance", "# if this does not work:", "raise", "KeyError", "(", "\"P...
Get an instance of the `PolygonFilter` using a unique id
[ "Get", "an", "instance", "of", "the", "PolygonFilter", "using", "a", "unique", "id" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L209-L216
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.import_all
def import_all(path): """Import all polygons from a .poly file. Returns a list of the imported polygon filters """ plist = [] fid = 0 while True: try: p = PolygonFilter(filename=path, fileid=fid) plist.append(p) fid += 1 except IndexError: break return plist
python
def import_all(path): """Import all polygons from a .poly file. Returns a list of the imported polygon filters """ plist = [] fid = 0 while True: try: p = PolygonFilter(filename=path, fileid=fid) plist.append(p) fid += 1 except IndexError: break return plist
[ "def", "import_all", "(", "path", ")", ":", "plist", "=", "[", "]", "fid", "=", "0", "while", "True", ":", "try", ":", "p", "=", "PolygonFilter", "(", "filename", "=", "path", ",", "fileid", "=", "fid", ")", "plist", ".", "append", "(", "p", ")",...
Import all polygons from a .poly file. Returns a list of the imported polygon filters
[ "Import", "all", "polygons", "from", "a", ".", "poly", "file", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L219-L233
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.point_in_poly
def point_in_poly(p, poly): """Determine whether a point is within a polygon area Uses the ray casting algorithm. Parameters ---------- p: float Coordinates of the point poly: array_like of shape (N, 2) Polygon (`PolygonFilter.points`) Returns ------- inside: bool `True`, if point is inside. Notes ----- If `p` lies on a side of the polygon, it is defined as - "inside" if it is on the top or right - "outside" if it is on the lower or left """ poly = np.array(poly) n = poly.shape[0] inside = False x, y = p # Coarse bounding box exclusion: if (x <= poly[:, 0].max() and x > poly[:, 0].min() and y <= poly[:, 1].max() and y > poly[:, 1].min()): # The point is within the coarse bounding box. p1x, p1y = poly[0] # point i in contour for ii in range(n): # also covers (n-1, 0) (circular) p2x, p2y = poly[(ii+1) % n] # point ii+1 in contour (circular) # Edge-wise fine bounding-ray exclusion. # Determine whether point is in the current ray, # defined by the y-range of p1 and p2 and whether # it is left of p1 and p2. if (y > min(p1y, p2y) and y <= max(p1y, p2y) # in y-range and x <= max(p1x, p2x)): # left of p1 and p2 # Note that always p1y!=p2y due to the above test. # Only Compute the x-coordinate of the intersection # between line p1-p2 and the horizontal ray, # ((y-p1y)*(p2x-p1x)/(p2y-p1y) + p1x), # if x is not already known to be left of it # (p1x==p2x in combination with x<=max(p1x, p2x) above). if p1x == p2x or x <= (y-p1y)*(p2x-p1x)/(p2y-p1y) + p1x: # Toggle `inside` if the ray intersects # with the current edge. inside = not inside # Move on to the next edge of the polygon. p1x, p1y = p2x, p2y return inside
python
def point_in_poly(p, poly): """Determine whether a point is within a polygon area Uses the ray casting algorithm. Parameters ---------- p: float Coordinates of the point poly: array_like of shape (N, 2) Polygon (`PolygonFilter.points`) Returns ------- inside: bool `True`, if point is inside. Notes ----- If `p` lies on a side of the polygon, it is defined as - "inside" if it is on the top or right - "outside" if it is on the lower or left """ poly = np.array(poly) n = poly.shape[0] inside = False x, y = p # Coarse bounding box exclusion: if (x <= poly[:, 0].max() and x > poly[:, 0].min() and y <= poly[:, 1].max() and y > poly[:, 1].min()): # The point is within the coarse bounding box. p1x, p1y = poly[0] # point i in contour for ii in range(n): # also covers (n-1, 0) (circular) p2x, p2y = poly[(ii+1) % n] # point ii+1 in contour (circular) # Edge-wise fine bounding-ray exclusion. # Determine whether point is in the current ray, # defined by the y-range of p1 and p2 and whether # it is left of p1 and p2. if (y > min(p1y, p2y) and y <= max(p1y, p2y) # in y-range and x <= max(p1x, p2x)): # left of p1 and p2 # Note that always p1y!=p2y due to the above test. # Only Compute the x-coordinate of the intersection # between line p1-p2 and the horizontal ray, # ((y-p1y)*(p2x-p1x)/(p2y-p1y) + p1x), # if x is not already known to be left of it # (p1x==p2x in combination with x<=max(p1x, p2x) above). if p1x == p2x or x <= (y-p1y)*(p2x-p1x)/(p2y-p1y) + p1x: # Toggle `inside` if the ray intersects # with the current edge. inside = not inside # Move on to the next edge of the polygon. p1x, p1y = p2x, p2y return inside
[ "def", "point_in_poly", "(", "p", ",", "poly", ")", ":", "poly", "=", "np", ".", "array", "(", "poly", ")", "n", "=", "poly", ".", "shape", "[", "0", "]", "inside", "=", "False", "x", ",", "y", "=", "p", "# Coarse bounding box exclusion:", "if", "(...
Determine whether a point is within a polygon area Uses the ray casting algorithm. Parameters ---------- p: float Coordinates of the point poly: array_like of shape (N, 2) Polygon (`PolygonFilter.points`) Returns ------- inside: bool `True`, if point is inside. Notes ----- If `p` lies on a side of the polygon, it is defined as - "inside" if it is on the top or right - "outside" if it is on the lower or left
[ "Determine", "whether", "a", "point", "is", "within", "a", "polygon", "area" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L246-L301
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.remove
def remove(unique_id): """Remove a polygon filter from `PolygonFilter.instances`""" for p in PolygonFilter.instances: if p.unique_id == unique_id: PolygonFilter.instances.remove(p)
python
def remove(unique_id): """Remove a polygon filter from `PolygonFilter.instances`""" for p in PolygonFilter.instances: if p.unique_id == unique_id: PolygonFilter.instances.remove(p)
[ "def", "remove", "(", "unique_id", ")", ":", "for", "p", "in", "PolygonFilter", ".", "instances", ":", "if", "p", ".", "unique_id", "==", "unique_id", ":", "PolygonFilter", ".", "instances", ".", "remove", "(", "p", ")" ]
Remove a polygon filter from `PolygonFilter.instances`
[ "Remove", "a", "polygon", "filter", "from", "PolygonFilter", ".", "instances" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L304-L308
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.save
def save(self, polyfile, ret_fobj=False): """Save all data to a text file (appends data if file exists). Polyfile can be either a path to a file or a file object that was opened with the write "w" parameter. By using the file object, multiple instances of this class can write their data. If `ret_fobj` is `True`, then the file object will not be closed and returned. """ if is_file_obj(polyfile): fobj = polyfile else: fobj = pathlib.Path(polyfile).open("a") # Who the hell would use more then 10 million polygons or # polygon points? -> 08d (easier if other people want to import) data2write = [] data2write.append("[Polygon {:08d}]".format(self.unique_id)) data2write.append("X Axis = {}".format(self.axes[0])) data2write.append("Y Axis = {}".format(self.axes[1])) data2write.append("Name = {}".format(self.name)) data2write.append("Inverted = {}".format(self.inverted)) for i, point in enumerate(self.points): data2write.append("point{:08d} = {:.15e} {:.15e}".format(i, point[0], point[1])) # Add new lines for i in range(len(data2write)): data2write[i] += "\n" # begin writing to fobj fobj.writelines(data2write) if ret_fobj: return fobj else: fobj.close()
python
def save(self, polyfile, ret_fobj=False): """Save all data to a text file (appends data if file exists). Polyfile can be either a path to a file or a file object that was opened with the write "w" parameter. By using the file object, multiple instances of this class can write their data. If `ret_fobj` is `True`, then the file object will not be closed and returned. """ if is_file_obj(polyfile): fobj = polyfile else: fobj = pathlib.Path(polyfile).open("a") # Who the hell would use more then 10 million polygons or # polygon points? -> 08d (easier if other people want to import) data2write = [] data2write.append("[Polygon {:08d}]".format(self.unique_id)) data2write.append("X Axis = {}".format(self.axes[0])) data2write.append("Y Axis = {}".format(self.axes[1])) data2write.append("Name = {}".format(self.name)) data2write.append("Inverted = {}".format(self.inverted)) for i, point in enumerate(self.points): data2write.append("point{:08d} = {:.15e} {:.15e}".format(i, point[0], point[1])) # Add new lines for i in range(len(data2write)): data2write[i] += "\n" # begin writing to fobj fobj.writelines(data2write) if ret_fobj: return fobj else: fobj.close()
[ "def", "save", "(", "self", ",", "polyfile", ",", "ret_fobj", "=", "False", ")", ":", "if", "is_file_obj", "(", "polyfile", ")", ":", "fobj", "=", "polyfile", "else", ":", "fobj", "=", "pathlib", ".", "Path", "(", "polyfile", ")", ".", "open", "(", ...
Save all data to a text file (appends data if file exists). Polyfile can be either a path to a file or a file object that was opened with the write "w" parameter. By using the file object, multiple instances of this class can write their data. If `ret_fobj` is `True`, then the file object will not be closed and returned.
[ "Save", "all", "data", "to", "a", "text", "file", "(", "appends", "data", "if", "file", "exists", ")", "." ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L310-L347
ZELLMECHANIK-DRESDEN/dclab
dclab/polygon_filter.py
PolygonFilter.save_all
def save_all(polyfile): """Save all polygon filters""" nump = len(PolygonFilter.instances) if nump == 0: raise PolygonFilterError("There are not polygon filters to save.") for p in PolygonFilter.instances: # we return the ret_obj, so we don't need to open and # close the file multiple times. polyobj = p.save(polyfile, ret_fobj=True) polyobj.close()
python
def save_all(polyfile): """Save all polygon filters""" nump = len(PolygonFilter.instances) if nump == 0: raise PolygonFilterError("There are not polygon filters to save.") for p in PolygonFilter.instances: # we return the ret_obj, so we don't need to open and # close the file multiple times. polyobj = p.save(polyfile, ret_fobj=True) polyobj.close()
[ "def", "save_all", "(", "polyfile", ")", ":", "nump", "=", "len", "(", "PolygonFilter", ".", "instances", ")", "if", "nump", "==", "0", ":", "raise", "PolygonFilterError", "(", "\"There are not polygon filters to save.\"", ")", "for", "p", "in", "PolygonFilter",...
Save all polygon filters
[ "Save", "all", "polygon", "filters" ]
train
https://github.com/ZELLMECHANIK-DRESDEN/dclab/blob/79002c4356e7020c2ba73ab0a3819c9abd4affec/dclab/polygon_filter.py#L350-L359
openstax/cnx-archive
cnxarchive/views/resource.py
get_resource
def get_resource(request): """Retrieve a file's data.""" hash = request.matchdict['hash'] # Do the file lookup with db_connect() as db_connection: with db_connection.cursor() as cursor: args = dict(hash=hash) cursor.execute(SQL['get-resource'], args) try: mimetype, file = cursor.fetchone() except TypeError: # None returned raise httpexceptions.HTTPNotFound() resp = request.response resp.status = "200 OK" resp.content_type = mimetype resp.body = file[:] return resp
python
def get_resource(request): """Retrieve a file's data.""" hash = request.matchdict['hash'] # Do the file lookup with db_connect() as db_connection: with db_connection.cursor() as cursor: args = dict(hash=hash) cursor.execute(SQL['get-resource'], args) try: mimetype, file = cursor.fetchone() except TypeError: # None returned raise httpexceptions.HTTPNotFound() resp = request.response resp.status = "200 OK" resp.content_type = mimetype resp.body = file[:] return resp
[ "def", "get_resource", "(", "request", ")", ":", "hash", "=", "request", ".", "matchdict", "[", "'hash'", "]", "# Do the file lookup", "with", "db_connect", "(", ")", "as", "db_connection", ":", "with", "db_connection", ".", "cursor", "(", ")", "as", "cursor...
Retrieve a file's data.
[ "Retrieve", "a", "file", "s", "data", "." ]
train
https://github.com/openstax/cnx-archive/blob/d31d34aa8bbc8a9fde6cd4227a0df92726e8daf4/cnxarchive/views/resource.py#L32-L50