code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
''' Convert decimal position to degrees, minutes, seconds in a fromat supported by EXIF ''' deg = math.floor(value) min = math.floor((value - deg) * 60) sec = math.floor((value - deg - min / 60) * 3600 * precision) return ((deg, 1), (min, 1), (sec, precision))
def decimal_to_dms(value, precision)
Convert decimal position to degrees, minutes, seconds in a fromat supported by EXIF
3.641902
2.257309
1.613382
''' Convert GPS coordinate in GPGGA format to degree/minute/second Reference: http://us.cactii.net/~bb/gps.py ''' deg_min, dmin = gpgga.split('.') degrees = int(deg_min[:-2]) minutes = float('%s.%s' % (deg_min[-2:], dmin)) decimal = degrees + (minutes / 60) return decimal
def gpgga_to_dms(gpgga)
Convert GPS coordinate in GPGGA format to degree/minute/second Reference: http://us.cactii.net/~bb/gps.py
6.807413
2.870228
2.371733
''' Get the compass bearing from start to end. Formula from http://www.movable-type.co.uk/scripts/latlong.html ''' # make sure everything is in radians start_lat = math.radians(start_lat) start_lon = math.radians(start_lon) end_lat = math.radians(end_lat) end_lon = math.radians(...
def compute_bearing(start_lat, start_lon, end_lat, end_lon)
Get the compass bearing from start to end. Formula from http://www.movable-type.co.uk/scripts/latlong.html
1.407012
1.304159
1.078866
''' Compute difference between two bearings ''' d = abs(b2 - b1) d = 360 - d if d > 180 else d return d
def diff_bearing(b1, b2)
Compute difference between two bearings
3.68974
3.264681
1.130199
''' Normalize bearing and convert from hex if ''' if bearing > 360 and check_hex: # fix negative value wrongly parsed in exifread # -360 degree -> 4294966935 when converting from hex bearing = bin(int(bearing))[2:] bearing = ''.join([str(int(int(a) == 0)) for a in bearing...
def normalize_bearing(bearing, check_hex=False)
Normalize bearing and convert from hex if
6.901423
6.158039
1.120718
''' Return interpolated lat, lon and compass bearing for time t. Points is a list of tuples (time, lat, lon, elevation), t a datetime object. ''' # find the enclosing points in sorted list if (t <= points[0][0]) or (t >= points[-1][0]): if t <= points[0][0]: dt = abs((points...
def interpolate_lat_lon(points, t, max_dt=1)
Return interpolated lat, lon and compass bearing for time t. Points is a list of tuples (time, lat, lon, elevation), t a datetime object.
2.278971
2.039654
1.117332
if self._ef is not None: self._ef['0th'][piexif.ImageIFD.ImageDescription] = json.dumps( dict)
def add_image_description(self, dict)
Add a dict to image description.
9.575993
7.979022
1.200146
if not orientation in range(1, 9): print_error( "Error value for orientation, value must be in range(1,9), setting to default 1") self._ef['0th'][piexif.ImageIFD.Orientation] = 1 else: self._ef['0th'][piexif.ImageIFD.Orientation] = orientation
def add_orientation(self, orientation)
Add image orientation to image.
5.630543
4.940797
1.139602
try: DateTimeOriginal = date_time.strftime(time_format)[:-3] self._ef['Exif'][piexif.ExifIFD.DateTimeOriginal] = DateTimeOriginal except Exception as e: print_error("Error writing DateTimeOriginal, due to " + str(e))
def add_date_time_original(self, date_time, time_format='%Y:%m:%d %H:%M:%S.%f')
Add date time original.
4.47974
4.333383
1.033774
self._ef["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N" if lat > 0 else "S" self._ef["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "E" if lon > 0 else "W" self._ef["GPS"][piexif.GPSIFD.GPSLongitude] = decimal_to_dms( abs(lon), int(precision)) self._ef["GPS"][piexif.GPSIFD.GPSL...
def add_lat_lon(self, lat, lon, precision=1e7)
Add lat, lon to gps (lat, lon in float).
1.969506
1.911325
1.03044
self._ef['0th'][piexif.ImageIFD.ImageHistory] = json.dumps(data)
def add_image_history(self, data)
Add arbitrary string to ImageHistory tag.
18.50355
14.384504
1.286353
''' Add camera make and model.''' self._ef['0th'][piexif.ImageIFD.Make] = make self._ef['0th'][piexif.ImageIFD.Model] = model
def add_camera_make_model(self, make, model)
Add camera make and model.
5.753018
5.907116
0.973913
self._ef["GPS"][piexif.GPSIFD.GPSDOP] = ( int(abs(dop) * precision), precision)
def add_dop(self, dop, precision=100)
Add GPSDOP (float).
17.86615
10.562222
1.691514
ref = 0 if altitude > 0 else 1 self._ef["GPS"][piexif.GPSIFD.GPSAltitude] = ( int(abs(altitude) * precision), precision) self._ef["GPS"][piexif.GPSIFD.GPSAltitudeRef] = ref
def add_altitude(self, altitude, precision=100)
Add altitude (pre is the precision).
4.997169
4.889501
1.02202
# normalize direction direction = direction % 360.0 self._ef["GPS"][piexif.GPSIFD.GPSImgDirection] = ( int(abs(direction) * precision), precision) self._ef["GPS"][piexif.GPSIFD.GPSImgDirectionRef] = ref
def add_direction(self, direction, ref="T", precision=100)
Add image direction.
5.57247
5.634546
0.988983
if filename is None: filename = self._filename exif_bytes = piexif.dump(self._ef) with open(self._filename, "rb") as fin: img = fin.read() try: piexif.insert(exif_bytes, img, filename) except IOError: type, value, traceb...
def write(self, filename=None)
Save exif data to file.
4.092144
3.64685
1.122104
''' Estimate the capture time of a sequence with sub-second precision EXIF times are only given up to a second of precision. This function uses the given interval between shots to estimate the time inside that second that each picture was taken. ''' if interval <= 0.0: return [exif_t...
def estimate_sub_second_time(files, interval=0.0)
Estimate the capture time of a sequence with sub-second precision EXIF times are only given up to a second of precision. This function uses the given interval between shots to estimate the time inside that second that each picture was taken.
4.328075
2.757647
1.569481
''' Interpolate time stamps in case of identical timestamps ''' timestamps = [] num_file = len(capture_times) time_dict = OrderedDict() if num_file < 2: return capture_times # trace identical timestamps (always assume capture_times is sorted) time_dict = OrderedDict() ...
def interpolate_timestamp(capture_times)
Interpolate time stamps in case of identical timestamps
2.967832
2.803255
1.058709
''' Gets information about a media file TODO: use the class in ffprobe.py - why doesn't it use json output? ''' try: with open(os.devnull, 'w') as tempf: subprocess.check_call( ['ffprobe', '-h'], stdout=tempf, stderr=tempf) except Exception as e: raise...
def get_ffprobe(path)
Gets information about a media file TODO: use the class in ffprobe.py - why doesn't it use json output?
3.071551
2.141665
1.434189
''' Get the data out of the file using ffmpeg @param filename: mp4 filename ''' if not os.path.isfile(source): raise IOError('No such file: ' + source) subprocess.check_output([ 'ffmpeg', '-i', source, '-y', # overwrite - potentially dangerous '-nostats'...
def extract_stream(source, dest, stream_id)
Get the data out of the file using ffmpeg @param filename: mp4 filename
4.112438
2.963717
1.387595
''' Check that image file has the required EXIF fields. Incompatible files will be ignored server side. ''' # required tags in IFD name convention required_exif = required_fields() exif = ExifRead(filename) required_exif_exist = exif.fields_exist(required_exif) return required_exif_e...
def verify_exif(filename)
Check that image file has the required EXIF fields. Incompatible files will be ignored server side.
9.476157
4.991936
1.898293
filepath_keep_original = processing.processed_images_rootpath(filepath) if os.path.isfile(filepath_keep_original): filepath = filepath_keep_original ''' Check that image file has the required Mapillary tag ''' return ExifRead(filepath).mapillary_tag_exists()
def verify_mapillary_tag(filepath)
Check that image file has the required Mapillary tag
10.876287
6.207256
1.752189
val=False if self.__dict__['codec_type']: if str(self.__dict__['codec_type']) == 'audio': val=True return val
def isAudio(self)
Is this stream labelled as an audio stream?
5.198025
4.07713
1.274923
val=False if self.__dict__['codec_type']: if self.codec_type == 'video': val=True return val
def isVideo(self)
Is the stream labelled as a video stream.
6.439754
5.329398
1.208346
val=False if self.__dict__['codec_type']: if str(self.codec_type)=='subtitle': val=True return val
def isSubtitle(self)
Is the stream labelled as a subtitle stream.
6.623434
5.711026
1.159763
size=None if self.isVideo(): if self.__dict__['width'] and self.__dict__['height']: try: size=(int(self.__dict__['width']),int(self.__dict__['height'])) except Exception as e: print "None integer size %s:%s" %(s...
def frameSize(self)
Returns the pixel frame size as an integer tuple (width,height) if the stream is a video stream. Returns None if it is not a video stream.
3.850215
3.404329
1.130976
f=None if self.isVideo(): if self.__dict__['pix_fmt']: f=self.__dict__['pix_fmt'] return f
def pixelFormat(self)
Returns a string representing the pixel format of the video stream. e.g. yuv420p. Returns none is it is not a video stream.
6.418909
4.010839
1.600391
f=0 if self.isVideo() or self.isAudio(): if self.__dict__['nb_frames']: try: f=int(self.__dict__['nb_frames']) except Exception as e: print "None integer frame count" return f
def frames(self)
Returns the length of a video stream in frames. Returns 0 if not a video stream.
5.823268
4.677674
1.244907
f=0.0 if self.isVideo() or self.isAudio(): if self.__dict__['duration']: try: f=float(self.__dict__['duration']) except Exception as e: print "None numeric duration" return f
def durationSeconds(self)
Returns the runtime duration of the video stream as a floating point number of seconds. Returns 0.0 if not a video stream.
5.843992
5.079145
1.150586
b=0 if self.__dict__['bit_rate']: try: b=int(self.__dict__['bit_rate']) except Exception as e: print "None integer bitrate" return b
def bitrate(self)
Returns bitrate as an integer in bps
5.212346
4.700572
1.108875
''' Returns upload URL using new upload API ''' request_url = "https://a.mapillary.com/v3/users/{}/upload_secrets?client_id={}".format( credentials["MAPSettingsUserKey"], CLIENT_ID) request = urllib2.Request(request_url) request.add_header('Authorization', 'Bearer {}'.format( cre...
def get_upload_url(credentials)
Returns upload URL using new upload API
4.570963
3.953732
1.156114
''' Get upload token ''' try: params = urllib.urlencode({"email": mail, "password": pwd}) response = urllib.urlopen(LOGIN_URL, params) except: return None resp = json.loads(response.read()) if not resp or 'token' not in resp: return None return resp['token...
def get_upload_token(mail, pwd)
Get upload token
2.953753
2.783448
1.061185
''' Display progress bar sources: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3 ''' bar_len = 60 filled_len = int(round(bar_len * count / float(total))) percents = round(100.0 * count / float(total), 1) bar = '=' * filled_len + '-' * (bar_len - filled_len) sys.stdout.writ...
def progress(count, total, suffix='')
Display progress bar sources: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
1.518656
1.324917
1.146228
''' Authenticate the user by passing the email and password. This function avoids prompting the command line for user credentials and is useful for calling tools programmatically ''' if user_email is None or user_password is None: raise ValueError( 'Could not authenticate user. M...
def authenticate_with_email_and_pwd(user_email, user_password)
Authenticate the user by passing the email and password. This function avoids prompting the command line for user credentials and is useful for calling tools programmatically
4.154841
3.099245
1.340598
''' Upload file at filepath. ''' if max_attempts == None: max_attempts = MAX_ATTEMPTS filename = os.path.basename(filepath) s3_filename = filename try: s3_filename = ExifRead(filepath).exif_name() except: pass filepath_keep_original = processing.processed_...
def upload_file(filepath, max_attempts, url, permission, signature, key=None, aws_key=None)
Upload file at filepath.
2.858375
2.817455
1.014524
c1 = 0xcc9e2d51 c2 = 0x1b873593 length = len(data) h1 = seed roundedEnd = (length & 0xfffffffc) # round down to 4 byte block for i in range(0, roundedEnd, 4): # little endian load order k1 = (ord(data[i]) & 0xff) | ((ord(data[i + 1]) & 0xff) << 8) | \ ((ord(d...
def murmur3_32(data, seed=0)
MurmurHash3 was written by Austin Appleby, and is placed in the public domain. The author hereby disclaims copyright to this source code.
1.382336
1.393729
0.991826
if allow_unicode_keys: if isinstance(key, six.text_type): key = key.encode('utf8') elif isinstance(key, VALID_STRING_TYPES): try: if isinstance(key, bytes): key = key.decode().encode('ascii') else: key = key.encode('ascii')...
def _check_key(key, allow_unicode_keys, key_prefix=b'')
Checks key and add key_prefix.
2.818385
2.768676
1.017954
chunks = [] last_char = b'' while True: # We're reading in chunks, so "\r\n" could appear in one chunk, # or across the boundary of two chunks, so we check for both # cases. # This case must appear first, since the buffer could have # later \r\n characters in i...
def _readline(sock, buf)
Read line of text from the socket. Read a line of text (delimited by "\r\n") from the socket, and return that line along with any trailing characters read from the socket. Args: sock: Socket object, should be connected. buf: String, zero or more characters, returned from an earlier ...
4.233118
4.234252
0.999732
chunks = [] rlen = size + 2 while rlen - len(buf) > 0: if buf: rlen -= len(buf) chunks.append(buf) buf = _recv(sock, RECV_SIZE) if not buf: raise MemcacheUnexpectedCloseError() # Now we need to remove the \r\n from the end. There are two ...
def _readvalue(sock, buf, size)
Read specified amount of bytes from the socket. Read size bytes, followed by the "\r\n" characters, from the socket, and return those bytes and any trailing bytes read after the "\r\n". Args: sock: Socket object, should be connected. buf: String, zero or more characters, returned from an e...
4.766297
4.781837
0.99675
while True: try: return sock.recv(size) except IOError as e: if e.errno != errno.EINTR: raise
def _recv(sock, size)
sock.recv() with retry on EINTR
2.447394
1.965207
1.245362
return _check_key(key, allow_unicode_keys=self.allow_unicode_keys, key_prefix=self.key_prefix)
def check_key(self, key)
Checks key and add key_prefix.
6.085316
4.332012
1.404732
if self.sock is not None: try: self.sock.close() except Exception: pass finally: self.sock = None
def close(self)
Close the connection to memcached, if it is open. The next call to a method that requires a connection will re-open it.
2.803717
2.550186
1.099417
if noreply is None: noreply = self.default_noreply return self._store_cmd(b'set', {key: value}, expire, noreply)[key]
def set(self, key, value, expire=0, noreply=None)
The memcached "set" command. Args: key: str, see class docs for details. value: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional boo...
4.329058
5.38205
0.804351
if noreply is None: noreply = self.default_noreply result = self._store_cmd(b'set', values, expire, noreply) return [k for k, v in six.iteritems(result) if not v]
def set_many(self, values, expire=0, noreply=None)
A convenience function for setting multiple values. Args: values: dict(str, str), a dict of keys and values, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default)....
3.514362
3.579031
0.981931
if noreply is None: noreply = self.default_noreply return self._store_cmd(b'add', {key: value}, expire, noreply)[key]
def add(self, key, value, expire=0, noreply=None)
The memcached "add" command. Args: key: str, see class docs for details. value: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional boo...
4.480626
5.123139
0.874586
if noreply is None: noreply = self.default_noreply return self._store_cmd(b'replace', {key: value}, expire, noreply)[key]
def replace(self, key, value, expire=0, noreply=None)
The memcached "replace" command. Args: key: str, see class docs for details. value: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional...
4.329761
5.261148
0.822969
if noreply is None: noreply = self.default_noreply return self._store_cmd(b'append', {key: value}, expire, noreply)[key]
def append(self, key, value, expire=0, noreply=None)
The memcached "append" command. Args: key: str, see class docs for details. value: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional ...
4.520548
4.908887
0.920891
if noreply is None: noreply = self.default_noreply return self._store_cmd(b'prepend', {key: value}, expire, noreply)[key]
def prepend(self, key, value, expire=0, noreply=None)
The memcached "prepend" command. Args: key: str, see class docs for details. value: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional...
4.59942
4.992721
0.921225
return self._store_cmd(b'cas', {key: value}, expire, noreply, cas)[key]
def cas(self, key, value, cas, expire=0, noreply=False)
The memcached "cas" command. Args: key: str, see class docs for details. value: str, see class docs for details. cas: int or str that only contains the characters '0'-'9'. expire: optional int, number of seconds until the item is expired from the cache,...
7.838087
13.211751
0.593266
return self._fetch_cmd(b'get', [key], False).get(key, default)
def get(self, key, default=None)
The memcached "get" command, but only for one key, as a convenience. Args: key: str, see class docs for details. default: value that will be returned if the key was not found. Returns: The value for the key, or default if the key wasn't found.
14.359983
14.572873
0.985391
defaults = (default, cas_default) return self._fetch_cmd(b'gets', [key], True).get(key, defaults)
def gets(self, key, default=None, cas_default=None)
The memcached "gets" command for one key, as a convenience. Args: key: str, see class docs for details. default: value that will be returned if the key was not found. cas_default: same behaviour as default argument. Returns: A tuple of (value, cas) or ...
9.274706
11.615953
0.798446
if noreply is None: noreply = self.default_noreply cmd = b'delete ' + self.check_key(key) if noreply: cmd += b' noreply' cmd += b'\r\n' results = self._misc_cmd([cmd], b'delete', noreply) if noreply: return True return ...
def delete(self, key, noreply=None)
The memcached "delete" command. Args: key: str, see class docs for details. noreply: optional bool, True to not wait for the reply (defaults to self.default_noreply). Returns: If noreply is True, always returns True. Otherwise returns True if ...
3.207321
3.034126
1.057082
if not keys: return True if noreply is None: noreply = self.default_noreply cmds = [] for key in keys: cmds.append( b'delete ' + self.check_key(key) + (b' noreply' if noreply else b'') + b'\r\n...
def delete_many(self, keys, noreply=None)
A convenience function to delete multiple keys. Args: keys: list(str), the list of keys to delete. noreply: optional bool, True to not wait for the reply (defaults to self.default_noreply). Returns: True. If an exception is raised then all, some or none...
3.37721
3.406281
0.991466
key = self.check_key(key) cmd = b'incr ' + key + b' ' + six.text_type(value).encode('ascii') if noreply: cmd += b' noreply' cmd += b'\r\n' results = self._misc_cmd([cmd], b'incr', noreply) if noreply: return None if results[0] == b...
def incr(self, key, value, noreply=False)
The memcached "incr" command. Args: key: str, see class docs for details. value: int, the amount by which to increment the value. noreply: optional bool, False to wait for the reply (the default). Returns: If noreply is True, always returns None. Otherwise retur...
2.883059
3.057772
0.942863
key = self.check_key(key) cmd = b'decr ' + key + b' ' + six.text_type(value).encode('ascii') if noreply: cmd += b' noreply' cmd += b'\r\n' results = self._misc_cmd([cmd], b'decr', noreply) if noreply: return None if results[0] == b...
def decr(self, key, value, noreply=False)
The memcached "decr" command. Args: key: str, see class docs for details. value: int, the amount by which to increment the value. noreply: optional bool, False to wait for the reply (the default). Returns: If noreply is True, always returns None. Otherwise retur...
2.978593
3.062739
0.972526
if noreply is None: noreply = self.default_noreply key = self.check_key(key) cmd = b'touch ' + key + b' ' + six.text_type(expire).encode('ascii') if noreply: cmd += b' noreply' cmd += b'\r\n' results = self._misc_cmd([cmd], b'touch', norep...
def touch(self, key, expire=0, noreply=None)
The memcached "touch" command. Args: key: str, see class docs for details. expire: optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default). noreply: optional bool, True to not wait for the reply (defaults to ...
2.959997
2.977453
0.994137
result = self._fetch_cmd(b'stats', args, False) for key, value in six.iteritems(result): converter = STAT_TYPES.get(key, int) try: result[key] = converter(value) except Exception: pass return result
def stats(self, *args)
The memcached "stats" command. The returned keys depend on what the "stats" command returns. A best effort is made to convert values to appropriate Python types, defaulting to strings when a conversion cannot be made. Args: *arg: extra string arguments to the "stats" command....
4.544432
5.007862
0.90746
self._fetch_cmd(b'cache_memlimit', [str(int(memlimit))], False) return True
def cache_memlimit(self, memlimit)
The memcached "cache_memlimit" command. Args: memlimit: int, the number of megabytes to set as the new cache memory limit. Returns: If no exception is raised, always returns True.
9.871665
9.290656
1.062537
cmd = b"version\r\n" results = self._misc_cmd([cmd], b'version', False) before, _, after = results[0].partition(b' ') if before != b'VERSION': raise MemcacheUnknownError( "Received unexpected response: %s" % results[0]) return after
def version(self)
The memcached "version" command. Returns: A string of the memcached version.
8.373879
7.006271
1.195198
if noreply is None: noreply = self.default_noreply cmd = b'flush_all ' + six.text_type(delay).encode('ascii') if noreply: cmd += b' noreply' cmd += b'\r\n' results = self._misc_cmd([cmd], b'flush_all', noreply) if noreply: retu...
def flush_all(self, delay=0, noreply=None)
The memcached "flush_all" command. Args: delay: optional int, the number of seconds to wait before flushing, or zero to flush immediately (the default). noreply: optional bool, True to not wait for the reply (defaults to self.default_noreply). Re...
2.966578
2.704057
1.097084
cmd = b"quit\r\n" self._misc_cmd([cmd], b'quit', True) self.close()
def quit(self)
The memcached "quit" command. This will close the connection with memcached. Calling any other method on this object will re-open the connection, so this object can be re-used after quit.
13.323461
10.21391
1.304443
if isinstance(value, list): return value elif isinstance(value, string_type): if allow_filename and os.path.isfile(value): with codecs.open(value, 'r', encoding="utf-8") as handle: return handle.read().splitlines() return value.split(',') else: ...
def _create_list(value, allow_filename=False)
Create a list from the input value. If the input is a list already, return it. If the input is a comma-separated string, split it.
2.526924
2.514705
1.004859
configured_ncbi_strings = [self._LEVELS[level] for level in self.assembly_level] return ncbi_assembly_level in configured_ncbi_strings
def is_compatible_assembly_level(self, ncbi_assembly_level)
Check if a given ncbi assembly level string matches the configured assembly levels.
7.650985
5.91451
1.293596
config = cls() for slot in cls.__slots__: if slot.startswith('_'): slot = slot[1:] setattr(config, slot, kwargs.pop(slot, cls.get_default(slot))) if kwargs: raise ValueError("Unrecognized option(s): {}".format(kwargs.keys())) ...
def from_kwargs(cls, **kwargs)
Initialise configuration from kwargs.
3.351326
2.966699
1.129648
config = cls() for slot in cls.__slots__: if slot.startswith('_'): slot = slot[1:] if not hasattr(namespace, slot): continue setattr(config, slot, getattr(namespace, slot)) return config
def from_namespace(cls, namespace)
Initialise from argparser Namespace object.
2.995347
2.77295
1.080202
value = cls._DEFAULTS[category] if not value or not isinstance(value, list): return value return value[0]
def get_default(cls, category)
Get the default value of a given category.
4.618651
4.058483
1.138024
value = cls._DEFAULTS[category] if not isinstance(value, list): raise ValueError("{} does not offer choices".format(category)) return value
def get_choices(cls, category)
Get all available options for a category.
6.656843
6.12661
1.086546
parser = argument_parser(version=__version__) args = parser.parse_args() if args.debug: log_level = logging.DEBUG elif args.verbose: log_level = logging.INFO else: log_level = logging.WARNING logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)...
def main()
Build and parse command line.
2.883933
2.777596
1.038284
if columns is None: columns = _DEFAULT_COLUMNS global _METADATA if not _METADATA: _METADATA = MetaData(columns) return _METADATA
def get(columns=None)
Get or create MetaData singleton.
5.363253
3.046304
1.760577
row = self.rowClass() for key, val in entry.items(): if key in self.columns: setattr(row, key, val) row.local_filename = os.path.join('.', os.path.relpath(local_file)) self.rows.append(row)
def add(self, entry, local_file)
Add a metadata row.
3.583122
3.079926
1.163379
handle.write(u"\t".join(self.columns)) handle.write(u"\n") for row in self.rows: row.write(handle)
def write(self, handle)
Write metadata to handle.
3.184872
3.026035
1.05249
try: download_candidates = select_candidates(config) if len(download_candidates) < 1: logging.error("No downloads matched your filter. Please check your options.") return 1 if config.dry_run: print("Considering the following {} assemblies for downlo...
def config_download(config)
Run the actual download from NCBI with parameters in a config object. Parameters ---------- config: NgdConfig A configuration object with the download settings Returns ------- int success code
4.788812
4.758882
1.006289
download_candidates = [] for group in config.group: summary_file = get_summary(config.section, group, config.uri, config.use_cache) entries = parse_summary(summary_file) for entry in filter_entries(entries, config): download_candidates.append((entry, group)) retur...
def select_candidates(config)
Select candidates to download. Parameters ---------- config: NgdConfig Runtime configuration object Returns ------- list of (<candidate entry>, <taxonomic group>)
5.16418
4.938971
1.045599
def in_genus_list(species, genus_list): for genus in genus_list: if species.startswith(genus.capitalize()): return True return False new_entries = [] for entry in entries: if config.type_material and config.type_material != ['any']: reque...
def filter_entries(entries, config)
Narrrow down which entries to download.
2.498733
2.473573
1.010172
ret = False try: if job.full_url is not None: req = requests.get(job.full_url, stream=True) ret = save_and_check(req, job.local_file, job.expected_checksum) if not ret: return ret ret = create_symlink(job.local_file, job.symlink_path) ...
def worker(job)
Run a single download job.
4.727975
4.535498
1.042438
logging.debug('Checking for a cached summary file') cachefile = "{section}_{domain}_assembly_summary.txt".format(section=section, domain=domain) full_cachefile = os.path.join(CACHE_DIR, cachefile) if use_cache and os.path.exists(full_cachefile) and \ datetime.utcnow() - datetime.fromtimesta...
def get_summary(section, domain, uri, use_cache)
Get the assembly_summary.txt file from NCBI and return a StringIO object for it.
2.34573
2.184162
1.073973
logging.info('Checking record %r', entry['assembly_accession']) full_output_dir = create_dir(entry, config.section, domain, config.output) symlink_path = None if config.human_readable: symlink_path = create_readable_dir(entry, config.section, domain, config.output) checksums = grab_ch...
def create_downloadjob(entry, domain, config)
Create download jobs for all file formats from a summary file entry.
3.338383
3.234579
1.032092
full_output_dir = os.path.join(output, section, domain, entry['assembly_accession']) try: os.makedirs(full_output_dir) except OSError as err: if err.errno == errno.EEXIST and os.path.isdir(full_output_dir): pass else: raise return full_output_dir
def create_dir(entry, section, domain, output)
Create the output directory for the entry if needed.
2.008845
2.029243
0.989948
if domain != 'viral': full_output_dir = os.path.join(output, 'human_readable', section, domain, get_genus_label(entry), get_species_label(entry), get_strain_label(entry)) else: ...
def create_readable_dir(entry, section, domain, output)
Create the a human-readable directory to link the entry to if needed.
2.291102
2.211946
1.035786
http_url = convert_ftp_url(entry['ftp_path']) full_url = '{}/md5checksums.txt'.format(http_url) req = requests.get(full_url) return req.text
def grab_checksums_file(entry)
Grab the checksum file for a given entry.
4.964792
4.946477
1.003703
checksums_list = [] for line in checksums_string.split('\n'): try: # skip empty lines if line == '': continue checksum, filename = line.split() # strip leading ./ if filename.startswith('./'): filename = fi...
def parse_checksums(checksums_string)
Parse a file containing checksums and filenames.
3.090489
2.943904
1.049793
pattern = NgdConfig.get_fileending(filetype) filename, expected_checksum = get_name_and_checksum(checksums, pattern) full_filename = os.path.join(directory, filename) # if file doesn't exist, it has changed if not os.path.isfile(full_filename): return True actual_checksum = md5sum(...
def has_file_changed(directory, checksums, filetype='genbank')
Check if the checksum of a given file has changed.
3.952118
3.964271
0.996934
# If we don't have a symlink path, we don't need to create a symlink if symlink_path is None: return False pattern = NgdConfig.get_fileending(filetype) filename, _ = get_name_and_checksum(checksums, pattern) full_filename = os.path.join(directory, filename) symlink_name = os.path.j...
def need_to_create_symlink(directory, checksums, filetype, symlink_path)
Check if we need to create a symlink for an existing file.
3.155238
3.119377
1.011496
for entry in checksums: if not entry['file'].endswith(end): # wrong file continue # workaround for ..cds_from_genomic.fna.gz and ..rna_from_genomic.fna.gz also # ending in _genomic.fna.gz, causing bogus matches for the plain fasta if '_from_' not in end a...
def get_name_and_checksum(checksums, end)
Extract a full filename and checksum from the checksums list for a file ending in given end.
6.646365
6.390867
1.039979
hash_md5 = hashlib.md5() with open(filename, 'rb') as handle: for chunk in iter(lambda: handle.read(4096), b''): hash_md5.update(chunk) return hash_md5.hexdigest()
def md5sum(filename)
Calculate the md5sum of a file and return the hexdigest.
1.499925
1.511652
0.992242
pattern = NgdConfig.get_fileending(filetype) filename, expected_checksum = get_name_and_checksum(checksums, pattern) base_url = convert_ftp_url(entry['ftp_path']) full_url = '{}/{}'.format(base_url, filename) local_file = os.path.join(directory, filename) full_symlink = None if symlink_...
def download_file_job(entry, directory, checksums, filetype='genbank', symlink_path=None)
Generate a DownloadJob that actually triggers a file download.
4.359784
4.118509
1.058583
pattern = NgdConfig.get_fileending(filetype) filename, _ = get_name_and_checksum(checksums, pattern) local_file = os.path.join(directory, filename) full_symlink = os.path.join(symlink_path, filename) return DownloadJob(None, local_file, None, full_symlink)
def create_symlink_job(directory, checksums, filetype, symlink_path)
Create a symlink-creating DownloadJob for an already downloaded file.
4.92347
4.28906
1.147914
with open(local_file, 'wb') as handle: for chunk in response.iter_content(4096): handle.write(chunk) actual_checksum = md5sum(local_file) if actual_checksum != expected_checksum: logging.error('Checksum mismatch for %r. Expected %r, got %r', local_file...
def save_and_check(response, local_file, expected_checksum)
Save the content of an http response and verify the checksum matches.
1.98737
1.942472
1.023114
if symlink_path is not None: if os.path.exists(symlink_path) or os.path.lexists(symlink_path): os.unlink(symlink_path) local_file = os.path.normpath(local_file) symlink_path = os.path.normpath(symlink_path) num_dirs_upward = len(os.path.dirname(symlink_path).split(os...
def create_symlink(local_file, symlink_path)
Create a relative symbolic link if symlink path is given. Parameters ---------- local_file relative path to output folder (includes ./ prefix) of file saved symlink_path relative path to output folder (includes ./ prefix) of symbolic link to be created Returns ------- ...
2.387522
2.60371
0.916969
def get_strain(entry): strain = entry['infraspecific_name'] if strain != '': strain = strain.split('=')[-1] return strain strain = entry['isolate'] if strain != '': return strain if len(entry['organism_name'].split(' ')) > 2 and not ...
def get_strain_label(entry, viral=False)
Try to extract a strain from an assemly summary entry. First this checks 'infraspecific_name', then 'isolate', then it tries to get it from 'organism_name'. If all fails, it falls back to just returning the assembly accesion number.
2.318422
1.951883
1.187787
desc = 'Perform queries against the NCBI Taxa database' epi = parser = argparse.ArgumentParser(description=desc, epilog=epi, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('taxid', metavar='taxid', type=str, help='A comma-separated list o...
def get_args()
Parse command line arguments
3.584219
3.525531
1.016647
for key, value in d.items(): print(' ' * indent + str(key)) if isinstance(value, dict): pretty(value, indent+1) else: sys.stderr.write(' ' * (indent+1) + str(value) + '\n')
def pretty(d, indent=0)
A prettier way to print nested dicts
1.906187
1.835253
1.038651
# Main feature of the script is to get all taxa within a given group. descendent_taxa = ncbi.get_descendant_taxa(taxid) descendent_taxa_names = ncbi.translate_to_names(descendent_taxa) if just_taxids: for taxid in descendent_taxa: outFH.write(str(taxid) + '\n') else: ...
def desc_taxa(taxid, ncbi, outFH, just_taxids=False)
Write descendent taxa for taxid
2.749758
2.734225
1.005681
taxid = int(taxid) tax_name = ncbi.get_taxid_translator([taxid])[taxid] rank = list(ncbi.get_rank([taxid]).values())[0] lineage = ncbi.get_taxid_translator(ncbi.get_lineage(taxid)) lineage = ['{}:{}'.format(k,v) for k,v in lineage.items()] lineage = ';'.join(lineage) x = [str(x) for x i...
def taxon_info(taxid, ncbi, outFH)
Write info on taxid
1.974577
1.966489
1.004113
new_taxids = [] for taxid in taxids: try: new_taxids.append(ncbi.get_name_translator([taxid])[taxid][0]) except KeyError: try: new_taxids.append(int(taxid)) except ValueError: msg = 'Error: cannot convert to taxid: {}' ...
def name2taxid(taxids, ncbi)
Converting taxon names to taxids
2.133932
2.06142
1.035176
# Get commandline args args = get_args() # Instantiate the ete NCBI taxa object ncbi = NCBITaxa(dbfile=args.database) ## dbfile location if args.verbose > 1: sys.stderr.write('Taxa database is stored at {}\n'.format(ncbi.dbfile)) # Update the database if required. if ar...
def main()
Make queries against NCBI Taxa databases
3.452172
3.370814
1.024136
''' Returns the final optimal value and choice probabilities given the choice specific value functions `Vals`. Probabilities are degenerate if sigma == 0.0. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float...
def calcLogSumChoiceProbs(Vals, sigma)
Returns the final optimal value and choice probabilities given the choice specific value functions `Vals`. Probabilities are degenerate if sigma == 0.0. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float A n...
4.643164
2.600515
1.785478
''' Returns the choice probabilities given the choice specific value functions `Vals`. Probabilities are degenerate if sigma == 0.0. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float A number that c...
def calcChoiceProbs(Vals, sigma)
Returns the choice probabilities given the choice specific value functions `Vals`. Probabilities are degenerate if sigma == 0.0. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float A number that controls the ...
4.834671
2.405575
2.009778
''' Returns the optimal value given the choice specific value functions Vals. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float A number that controls the variance of the taste shocks Returns --...
def calcLogSum(Vals, sigma)
Returns the optimal value given the choice specific value functions Vals. Parameters ---------- Vals : [numpy.array] A numpy.array that holds choice specific values at common grid points. sigma : float A number that controls the variance of the taste shocks Returns ------- V ...
6.33258
3.259694
1.942692
''' Evaluates the derivative of the interpolated function at the given input. Parameters ---------- x : np.array or float Real values to be evaluated in the interpolated function. Returns ------- dydx : np.array or float The inter...
def derivative(self,x)
Evaluates the derivative of the interpolated function at the given input. Parameters ---------- x : np.array or float Real values to be evaluated in the interpolated function. Returns ------- dydx : np.array or float The interpolated function's f...
4.935203
1.946709
2.535152