code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
assert isinstance(data, bytes) with self.lock: self.buf += data
def add(self, data)
Add data to the buffer
6.574863
5.319955
1.235887
data = b'' with self.lock: data, self.buf = self.buf, b'' return data
def get(self)
Get the content of the buffer
7.914587
5.551437
1.425682
while True: try: c = self.recv(1) except socket.error as e: if e.errno == errno.EWOULDBLOCK: return else: raise else: self._do(c) self.socket.setbl...
def handle_read(self)
Handle all the available character commands in the socket
3.031657
2.804584
1.080965
x = a + b z = x - a y = (a - (x - z)) + (b - z) return x, y
def knuth_sum(a, b)
Error-free transformation of the sum of two floating point numbers according to D.E. Knuth. The Art of Computer Programming: Seminumerical Algorithms, volume 2. Addison Wesley, Reading, Massachusetts, second edition, 1981. The underlying problem is that the exact sum a+b of two floating point ...
4.91548
5.115287
0.960939
x = a + b y = b - (x - a) if abs(a) > abs(b) else a - (x - b) return x, y
def decker_sum(a, b)
Computationally equivalent to knuth_sum, but formally a bit cheaper. Only works for floats though (and not arrays), and the branch make it in fact less favorable in terms of actual speed.
4.074808
4.026652
1.011959
q = p.reshape(p.shape[0], -1) for _ in range(K): _accupy.distill(q) return q.reshape(p.shape)
def distill(p, K)
Algorithm 4.3. Error-free vector transformation for summation. The vector p is transformed without changing the sum, and p_n is replaced by float(sum(p)). Kahan [21] calls this a 'distillation algorithm.'
4.913209
6.373367
0.770897
# Don't override the input data. q = p.copy() distill(q, K - 1) return numpy.sum(q[:-1], axis=0) + q[-1]
def ksum(p, K=2)
From T. Ogita, S.M. Rump, and S. Oishi. Accurate Sum and Dot Product, SIAM J. Sci. Comput., 26(6), 1955–1988 (34 pages). <https://doi.org/10.1137/030601818>. Algorithm 4.8. Summation as in K-fold precision by (K−1)-fold error-free vector transformation.
9.938807
10.136146
0.980531
q = p.reshape(p.shape[0], -1) s = _accupy.kahan(q) return s.reshape(p.shape[1:])
def kahan_sum(p)
Kahan summation <https://en.wikipedia.org/wiki/Kahan_summation_algorithm>.
5.378358
6.407187
0.839426
selection = list(selected_shells(command)) if command and command != '*' and selection: for i in selection: if i.state != remote_dispatcher.STATE_DEAD and i.enabled != enable: break else: toggle_shells('*', not enable) for i in selection: ...
def toggle_shells(command, enable)
Enable or disable the specified shells. If the command would have no effect, it changes all other shells to the inverse enable value.
6.159487
6.20392
0.992838
if not command or command == '*': for i in dispatchers.all_instances(): yield i return selected = set() instance_found = False for pattern in command.split(): found = False for expanded_pattern in expand_syntax(pattern): for i in dispatchers.a...
def selected_shells(command)
Iterator over the shells with names matching the patterns. An empty patterns matches all the shells
4.178833
4.126455
1.012693
res = [i.display_name + ' ' for i in dispatchers.all_instances() if i.display_name.startswith(text) and predicate(i) and ' ' + i.display_name + ' ' not in line] return res
def complete_shells(line, text, predicate=lambda i: True)
Return the shell names to include in the completion
5.782547
5.217525
1.108293
xx = x.reshape(-1, x.shape[-1]) yy = y.reshape(y.shape[0], -1) xx = numpy.ascontiguousarray(xx) yy = numpy.ascontiguousarray(yy) r = _accupy.kdot_helper(xx, yy).reshape((-1,) + x.shape[:-1] + y.shape[1:]) return ksum(r, K - 1)
def kdot(x, y, K=2)
Algorithm 5.10. Dot product algorithm in K-fold working precision, K >= 3.
3.616744
3.679536
0.982935
xx = x.reshape(-1, x.shape[-1]) yy = y.reshape(y.shape[0], -1) xx = numpy.ascontiguousarray(xx) yy = numpy.ascontiguousarray(yy) r = _accupy.kdot_helper(xx, yy).reshape((-1,) + x.shape[:-1] + y.shape[1:]) return fsum(r)
def fdot(x, y)
Algorithm 5.10. Dot product algorithm in K-fold working precision, K >= 3.
3.833109
3.899724
0.982918
for i in dispatchers.all_instances(): try: os.kill(-i.pid, signal.SIGKILL) except OSError: # The process was already dead, no problem pass
def kill_all()
When polysh quits, we kill all the remote shells we started
5.483855
5.292077
1.036239
locale.setlocale(locale.LC_ALL, '') atexit.register(kill_all) signal.signal(signal.SIGPIPE, signal.SIG_DFL) args = parse_cmdline() args.command = find_non_interactive_command(args.command) args.exit_code = 0 args.interactive = ( not args.command and sys.stdin.isatty() ...
def run()
Launch polysh
4.4881
4.497483
0.997914
sentry_dsn = os.environ.get('POLYSH_SENTRY_DSN') if sentry_dsn: from raven import Client client = Client( dsn=sentry_dsn, release='.'.join(map(str, VERSION)), ignore_exceptions=[ KeyboardInterrupt ] ) try: ...
def main()
Wrapper around run() to setup sentry
3.642267
3.112223
1.17031
new_data = b'' buffer_length = len(self.read_buffer) try: while buffer_length < self.MAX_BUFFER_SIZE: try: piece = self.recv(4096) except OSError as e: if e.errno == errno.EAGAIN: ...
def _handle_read_chunk(self)
Some data can be read
3.950065
3.872291
1.020085
self.write_buffer += buf if len(self.write_buffer) > self.MAX_BUFFER_SIZE: console_output('Buffer too big ({:d}) for {}\n'.format( len(self.write_buffer), str(self)).encode()) raise asyncore.ExitNow(1) return True
def dispatch_write(self, buf)
Augment the buffer with stuff to write when possible
5.206362
4.971889
1.04716
assert isinstance(buf, bytes) while True: try: os.write(1, buf) break except IOError as e: if e.errno != errno.EINTR: raise
def safe_write(buf)
We can get a SIGWINCH when printing, which will cause write to raise an EINTR. That's not a reason to stop printing.
2.607738
2.247659
1.160202
assert isinstance(msg, bytes) assert isinstance(logging_msg, bytes) or logging_msg is None from polysh import remote_dispatcher remote_dispatcher.log(logging_msg or msg) if remote_dispatcher.options.interactive: from polysh.stdin import the_stdin_thread the_stdin_thread.no_raw...
def console_output(msg, logging_msg=None)
Use instead of print, to clear the status information before printing
6.120929
5.944936
1.029604
match = syntax_pattern.search(string) if match: prefix = string[:match.start()] suffix = string[match.end():] intervals = match.group(1).split(',') for interval in intervals: interval_match = interval_pattern.match(interval) if interval_match: ...
def expand_syntax(string)
Iterator over all the strings in the expansion of the argument
2.554843
2.554253
1.000231
return sorted([i for i in asyncore.socket_map.values() if isinstance(i, remote_dispatcher.RemoteDispatcher)], key=lambda i: i.display_name or '')
def all_instances()
Iterator over all the remote_dispatcher instances
7.465703
5.129054
1.455571
awaited = 0 total = 0 for i in all_instances(): if i.enabled: total += 1 if i.state is not remote_dispatcher.STATE_IDLE: awaited += 1 return awaited, total
def count_awaited_processes()
Return a tuple with the number of awaited processes and the total number
5.639083
5.498604
1.025548
instances_found = False for i in all_instances(): instances_found = True if i.state not in (remote_dispatcher.STATE_TERMINATED, remote_dispatcher.STATE_DEAD): return False return instances_found
def all_terminated()
For each remote shell determine if its terminated
5.552853
5.226584
1.062425
w, h = terminal_size() w = max(w - display_names.max_display_name_length - 2, min(w, 10)) # python bug http://python.org/sf/1112949 on amd64 # from ajaxterm.py bug = struct.unpack('i', struct.pack('I', termios.TIOCSWINSZ))[0] packed_size = struct.pack('HHHH', h, w, 0, 0) term_size = w, ...
def update_terminal_size()
Propagate the terminal size to the remote shells accounting for the place taken by the longest name
5.994127
5.960204
1.005692
max_lengths = [] if info_list: nr_columns = len(info_list[0]) else: nr_columns = 0 for i in range(nr_columns): max_lengths.append(max([len(info[i]) for info in info_list])) flattened_info_list = [] for info_id in range(len(info_list)): info = info_list[info...
def format_info(info_list)
Turn a 2-dimension list of bytes into a 1-dimension list of bytes with correct spacing
3.035344
2.986282
1.016429
# Algorithm 6.1 from # # ACCURATE SUM AND DOT PRODUCT, # TAKESHI OGITA, SIEGFRIED M. RUMP, AND SHIN'ICHI OISHI. assert n >= 6 n2 = round(n / 2) x = numpy.zeros(n) y = numpy.zeros(n) b = math.log2(c) # vector of exponents between 0 and b/2: e = numpy.rint(numpy.random.ra...
def generate_ill_conditioned_dot_product(n, c, dps=100)
n ... length of vector c ... target condition number
4.725657
4.650469
1.016168
prev_nr_read = nr_handle_read asyncore.loop(count=1, timeout=timeout, use_poll=True) return nr_handle_read - prev_nr_read
def main_loop_iteration(timeout=None)
Return the number of RemoteDispatcher.handle_read() calls made by this iteration
5.594931
4.200292
1.332034
if options.user: name = '%s@%s' % (options.user, name) evaluated = options.ssh % {'host': name, 'port': port} if evaluated == options.ssh: evaluated = '%s %s' % (evaluated, name) os.execlp('/bin/sh', 'sh', '-c', evaluated)
def launch_ssh(self, name, port)
Launch the ssh command in the child process
3.565905
3.439424
1.036774
if state is not self.state: if self.debug: self.print_debug(b'state => ' + STATE_NAMES[state].encode()) if self.state is STATE_NOT_STARTED: self.read_in_state_not_started = b'' self.state = state
def change_state(self, state)
Change the state of the remote process, logging the change
6.311535
5.996448
1.052546
try: os.kill(-self.pid, signal.SIGKILL) except OSError: # The process was already dead, no problem pass self.read_buffer = b'' self.write_buffer = b'' self.set_enabled(False) if self.read_in_state_not_started: self....
def disconnect(self)
We are no more interested in this remote process
4.966869
4.734533
1.049073
attr = termios.tcgetattr(self.fd) attr[1] &= ~termios.ONLCR # oflag attr[3] &= ~termios.ECHO # lflag termios.tcsetattr(self.fd, termios.TCSANOW, attr) # unsetopt zle prevents Zsh from resetting the tty return b'unsetopt zle 2> /dev/null;stty -echo -onlcr -ctlec...
def configure_tty(self)
We don't want \n to be replaced with \r\n, and we disable the echo
8.563383
7.582639
1.129341
# No right prompt command_line = b'PS2=;RPS1=;RPROMPT=;' command_line += b'PROMPT_COMMAND=;' command_line += b'TERM=ansi;' command_line += b'unset HISTFILE;' prompt1, prompt2 = callbacks.add(b'prompt', self.seen_prompt_cb, True) command_line += b'PS1="' +...
def set_prompt(self)
The prompt is important because we detect the readyness of a process by waiting for its prompt.
9.522697
9.107814
1.045552
if self.state is not STATE_RUNNING or callbacks.any_in(data): # Slow case :-( return False last_nl = data.rfind(b'\n') if last_nl == -1: # No '\n' in data => slow case return False self.read_buffer = data[last_nl + 1:] sel...
def handle_read_fast_case(self, data)
If we are in a fast case we'll avoid the long processing of each line
6.332706
5.931469
1.067646
if self.state == STATE_DEAD: return global nr_handle_read nr_handle_read += 1 new_data = self._handle_read_chunk() if self.debug: self.print_debug(b'==> ' + new_data) if self.handle_read_fast_case(self.read_buffer): return ...
def handle_read(self)
We got some output from a remote shell, this is one of the state machine
4.097639
4.053332
1.010931
if self.state is STATE_RUNNING: if not callbacks.process(self.read_buffer): self.print_lines(self.read_buffer) self.read_buffer = b''
def print_unfinished_line(self)
The unfinished line stayed long enough in the buffer to be printed
8.13887
6.884743
1.18216
num_sent = self.send(self.write_buffer) if self.debug: if self.state is not STATE_NOT_STARTED or options.password is None: self.print_debug(b'<== ' + self.write_buffer[:num_sent]) self.write_buffer = self.write_buffer[num_sent:]
def handle_write(self)
Let's write as much as we can
5.583064
5.282134
1.056971
assert isinstance(msg, bytes) state = STATE_NAMES[self.state].encode() console_output(b'[dbg] ' + self.display_name.encode() + b'[' + state + b']: ' + msg + b'\n')
def print_debug(self, msg)
Log some debugging information to the console
7.769361
7.523017
1.032745
return [self.display_name.encode(), self.enabled and b'enabled' or b'disabled', STATE_NAMES[self.state].encode() + b':', self.last_printed_line.strip()]
def get_info(self)
Return a list with all information available about this process
10.751369
9.654488
1.113614
if self.state != STATE_DEAD and self.enabled: super().dispatch_write(buf) return True return False
def dispatch_write(self, buf)
There is new stuff to write when possible
7.050703
7.015524
1.005014
if not new_name: name = self.hostname else: name = new_name.decode() self.display_name = display_names.change( self.display_name, name)
def change_name(self, new_name)
Change the name of the shell, possibly updating the maximum name length
6.004745
6.127155
0.980022
if name: # defug callback add? rename1, rename2 = callbacks.add( b'rename', self.change_name, False) self.dispatch_command(b'/bin/echo "' + rename1 + b'""' + rename2 + b'"' + name + b'\n') else: se...
def rename(self, name)
Send to the remote shell, its new name to be shell expanded
13.087386
10.907677
1.199833
fd = os.open(os.ctermid(), os.O_RDONLY) cr = _ioctl_GWINSZ(fd) os.close(fd) except BaseException: pass if not cr: # env vars or finally defaults try: cr = os.environ['LINES'], os.environ['COLUMNS'] ...
def terminal_size(): # decide on *some* terminal size cr = _ioctl_GWINSZ(0) or _ioctl_GWINSZ( 1) or _ioctl_GWINSZ(2) # try open fds if not cr: # ...then ctty try
Return (lines, columns).
2.994983
3.10357
0.965012
global completion_results if state == 0: line = readline.get_line_buffer() if line.startswith(':'): # Control command completion completion_results = complete_control_command(line, text) else: if line.startswith('!') and text and line.startswith(t...
def complete(text, state)
On tab press, return the next possible completion
3.678234
3.713008
0.990635
read_line = None if 'read_line' in kwargs: read_line = kwargs['read_line'] del kwargs['read_line'] p = subprocess.Popen(*args, **kwargs) wait_stdout = None wait_stderr = None if p.stdout: wait_stdout = sys.stdout.attach(p.stdout, read_line=read_line) if p.stde...
def Popen(*args, **kwargs)
Executes a command using subprocess.Popen and redirects output to AETROS and stdout. Parses stdout as well for stdout API calls. Use read_line argument to read stdout of command's stdout line by line. Use returned process stdin to communicate with the command. :return: subprocess.Popen
2.230954
2.21375
1.007772
job = JobBackend() offline = False if '1' == os.getenv('AETROS_OFFLINE', ''): offline = True if os.getenv('AETROS_JOB_ID'): job.load(os.getenv('AETROS_JOB_ID')) if not offline: job.connect() else: job.create() if not offline: job...
def context()
Returns a new JobBackend instance which connects to AETROS Trainer based on "model" in aetros.yml or (internal: env:AETROS_MODEL_NAME environment variable). internal: If env:AETROS_JOB_ID is not defined, it creates a new job. Job is ended either by calling JobBackend.done(), JobBackend.fail() or JobBacken...
4.016358
3.027535
1.32661
if self.stop_requested or self.stop_requested_force: # signal has already been sent or we force a shutdown. # handles the keystroke 2x CTRL+C to force an exit. self.stop_requested_force = True self.logger.warning('Force stopped: ' + str(sig)) ...
def on_sigint(self, sig, frame)
We got SIGINT signal.
10.769229
10.382028
1.037295
self.ended = True self.running = False # When the server sends an abort signal, we really have to close immediately, # since for example the job has been already deleted. # without touching the git and client any further os._exit(1)
def external_aborted(self, params)
Immediately abort the job by server. This runs in the Client:read() thread.
23.644022
21.589571
1.095159
# only the master processes handles the regular stop signal from the server, sending a SIGINT to # all its child (means to us, non-master process) if not self.is_master_process(): if force: # make sure even the subprocess dies really on force ...
def external_stop(self, force)
Stop signal by server.
12.82651
11.832162
1.084038
self.lock.acquire() try: time_diff = time.time() - self.last_step_time if self.last_step > step: # it restarted self.last_step = 0 made_steps_since_last_call = step - self.last_step self.last_step = step ...
def step(self, step, total, label='STEP', speed_label='STEPS/S', size=1)
Increase the step indicator, which is a sub progress circle of the actual main progress circle (epoch, progress() method).
2.657925
2.650645
1.002747
return JobLossChannel(self, name, xaxis, yaxis, layout)
def create_loss_channel(self, name='loss', xaxis=None, yaxis=None, layout=None)
:param name: string :return: JobLossGraph
6.888734
4.543021
1.516333
return JobChannel(self, name, traces, main, kpi, kpiTrace, max_optimization, type, xaxis, yaxis, layout)
def create_channel(self, name, traces=None, main=False, kpi=False, kpiTrace=0, max_optimization=True, type=JobChannel.NUMBER, xaxis=None, yaxis=None, layout=None)
:param name: str :param traces: None|list : per default create a trace based on "name". :param main: bool : whether this channel is visible in the job list as column for better comparison. :param kpi: bool : whether this channel is the KPI (key performance indicator). ...
1.906277
2.503854
0.761337
self.in_shutdown = True self.logger.debug('on_shutdown, stopped=%s, ended=%s, early_stop=%s, stop_requested=%s' % (str(self.stopped), str(self.ended), str(self.in_early_stop), str(self.stop_requested))) if self.stopped or self.ended: # make really...
def on_shutdown(self)
Shutdown routine. Sets the last progress (done, aborted, failed) and tries to send last logs and git commits. Also makes sure the ssh connection is closed (thus, the job marked as offline). Is triggered by atexit.register().
4.361254
4.131452
1.055623
global last_exit_code if not last_exit_code: last_exit_code = 1 with self.git.batch_commit('FAILED'): self.set_status('FAILED', add_section=False) self.git.commit_json_file('FAIL_MESSAGE', 'aetros/job/crash/error', str(message) if message else '') ...
def fail(self, message=None, force_exit=False)
Marks the job as failed, saves the given error message and force exists the process when force_exit=True.
7.024551
6.606541
1.063272
if self.stream_log and not self.ended: # points to the Git stream write self.stream_log.write(message) return True
def write_log(self, message)
Proxy method for GeneralLogger.
15.452847
14.829576
1.042029
status = str(status) if add_section: self.section(status) self.job_add_status('status', status)
def set_status(self, status, add_section=True)
Set an arbitrary status, visible in the big wheel of the job view.
7.142827
5.370971
1.329895
if not create_info: create_info = { 'server': server, 'config': { 'insights': insights, 'command': ' '.join(sys.argv) } } config = find_config(self.config_path, logger=self.logger...
def create(self, create_info=None, hyperparameter=None, server='local', insights=False)
Creates a new job in git and pushes it. :param create_info: from the api.create_job_info(id). Contains the config and job info (type, server) :param hyperparameter: simple nested dict with key->value, which overwrites stuff from aetros.yml :param server: if None, the the job will be assigned to...
5.651333
5.140796
1.099311
value = read_parameter_by_path(self.job['config']['parameters'], path, return_group) if value is None: return default return value
def get_parameter(self, path, default=None, return_group=False)
Reads hyperparameter from job configuration. If nothing found use given default. :param path: str :param default: * :param return_group: If true and path is a choice_group, we return the dict instead of the group name. :return: *
6.727244
7.330933
0.917652
self.git.read_job(job_id, checkout=self.is_master_process()) self.load_job_from_ref()
def load(self, job_id)
Loads job into index and work-tree, restart its ref and sets as current. :param job_id: int
13.244355
12.720363
1.041193
if not self.job_id: raise Exception('Job not loaded yet. Use load(id) first.') if not os.path.exists(self.git.work_tree + '/aetros/job.json'): raise Exception('Could not load aetros/job.json from git repository. Make sure you have created the job correctly.') w...
def load_job_from_ref(self)
Loads the job.json into self.job
2.837799
2.65736
1.067901
if not self.job: raise Exception('Job not loaded yet. Use load(id) first.') return JobModel(self.job_id, self.job, self.home_config['storage_dir'])
def get_job_model(self)
Returns a new JobModel instance with current loaded job data attached. :return: JobModel
8.3842
7.798246
1.075139
blacklist = ['.git', 'aetros'] working_tree = self.git.work_tree def recursive(path='.'): if os.path.basename(path) in blacklist: return 0, 0 if os.path.isdir(path): files = [] for file in os.listdir(path): ...
def file_list(self)
Lists all files in the working directory.
3.964181
3.800057
1.04319
blacklist = ['.git'] def add_resursiv(path = '.', report=report): if os.path.basename(path) in blacklist: return 0, 0 if working_tree + '/aetros' == path: # ignore in work_tree the folder ./aetros/, as it could be # that ...
def add_files(self, working_tree, report=False)
Commits all files from limited in aetros.yml. `files` is a whitelist, `exclude_files` is a blacklist. If both are empty, we commit all files smaller than 10MB. :return:
3.782106
3.768248
1.003678
if path.endswith('.txt'): if not os.path.exists(path): raise Exception("Given word2vec file does not exist: " + path) f = open(path, 'r') if not header_with_dimensions and not dimensions: raise Exception('Either the word2vec file sho...
def add_embedding_word2vec(self, x, path, dimensions=None, header_with_dimensions=True)
Parse the word2vec file and extracts vectors as bytes and labels as TSV file. The format is simple: It's a UTF-8 encoded file, each word + vectors separated by new line. Vector is space separated. At the very first line might be dimensions, given as space separated value. Line 1: 2 4\n...
3.475192
3.385641
1.02645
if not os.path.exists(vectors_path): raise Exception("Given embedding vectors file does not exist: " + vectors_path) if metadata and not os.path.exists(metadata): raise Exception("Given embedding metadata file does not exist: " + metadata) name = os.path.basena...
def add_embedding_path(self, x, dimensions, vectors_path, metadata=None, image_shape=None, image=None)
Adds a new embedding with optional metadata. Example how to generate vectors based on 2D numpy array: # 4 vectors, each size of 3 vectors = [ [2.3, 4.0, 33], [2.4, 4.2, 44], [2.5, 3.9, 34], [5.5, 200.2, 66] ] metadata = [...
2.984046
2.742932
1.087904
current_year = datetime.datetime.now().strftime('%Y') files = [] for line in fileinfo: parts = re.split( r'^([\-dbclps])' + # Directory flag [1] r'([\-rwxs]{9})\s+' + # Permissions [2] r'(\d+)\s+' + # Numbe...
def split_file_info(fileinfo)
Parse sane directory output usually ls -l Adapted from https://gist.github.com/tobiasoberrauch/2942716
3.04085
2.946586
1.031991
if isinstance(local, file_type): # open file, leave open local_file = local elif local is None: # return string local_file = buffer_type() else: # path to file, open, write/close return None local_file = open(local, 'wb') self.conn.retrbin...
def get(self, remote, local=None)
Gets the file from FTP server local can be: a file: opened for writing, left open a string: path to output file None: contents are returned
3.484536
3.112899
1.119386
remote_dir = os.path.dirname(remote) remote_file = os.path.basename(local)\ if remote.endswith('/') else os.path.basename(remote) if contents: # local is ignored if contents is set local_file = buffer_type(contents) elif isinstance(local, fil...
def put(self, local, remote, contents=None, quiet=False)
Puts a local file (or contents) on to the FTP server local can be: a string: path to inpit file a file: opened for reading None: contents are pushed
3.175283
3.169061
1.001963
names = os.listdir(src) if ignore is not None: ignored_names = ignore(src, names) else: ignored_names = set() try: self.conn.mkd(dst) except error_perm: pass errors = [] for name in names: if n...
def upload_tree(self, src, dst, ignore=None)
Recursively upload a directory tree. Although similar to shutil.copytree we don't follow symlinks.
2.015482
1.86457
1.080937
if extra: self.tmp_output = [] self.conn.dir(remote, self._collector) directory_list = split_file_info(self.tmp_output) else: directory_list = self.conn.nlst(remote) if remove_relative_paths: return list(filter(self.is_not_rel...
def list(self, remote='.', extra=False, remove_relative_paths=False)
Return directory list
4.57726
4.094445
1.117919
remote_dirs = remote.split('/') for directory in remote_dirs: try: self.conn.cwd(directory) except Exception: if force: self.conn.mkd(directory) self.conn.cwd(directory) return self.conn.pwd(...
def descend(self, remote, force=False)
Descend, possibly creating directories as needed
3.232918
2.910121
1.110922
try: self.conn.delete(remote) except Exception: return False else: return True
def delete(self, remote)
Delete a file from server
4.042645
3.876176
1.042946
try: self.conn.cwd(remote) except Exception: return False else: return self.pwd()
def cd(self, remote)
Change working directory on server
4.827207
4.838479
0.99767
owner, name, id = unpack_full_job_id(full_id) if isinstance(sys.stdout, GeneralLogger): # we don't want to have stuff written to stdout before in job's log sys.stdout.clear_buffer() job_backend = JobBackend(model_name=owner + '/' + name) if fetch: job_backend.fetch(id) ...
def start(logger, full_id, fetch=True, env=None, volumes=None, cpus=None, memory=None, gpu_devices=None, offline=False)
Starts the job with all logging of a job_id
4.352179
4.249815
1.024087
if not Image.isImageType(im): raise TypeError("Input is not a PIL image.") if mode is not None: if mode != im.mode: im = im.convert(mode) elif im.mode == 'P': # Mode 'P' means there is an indexed "palette". If we leave the mode # as 'P', then when we do `a ...
def fromimage(im, flatten=False, mode=None)
Return a copy of a PIL image as a numpy array. Parameters ---------- im : PIL image Input image. flatten : bool If true, convert the output to grey-scale. mode : str, optional Mode to convert image to, e.g. ``'RGB'``. See the Notes of the `imread` docstring for more...
3.703662
3.821529
0.969157
if data.dtype == uint8: return data if high > 255: raise ValueError("`high` should be less than or equal to 255.") if low < 0: raise ValueError("`low` should be greater than or equal to 0.") if high < low: raise ValueError("`high` should be greater than or equal to ...
def bytescale(data, cmin=None, cmax=None, high=255, low=0)
Byte scales an array (image). Byte scaling means converting the input image to uint8 dtype and scaling the range to ``(low, high)`` (default 0-255). If the input image already has dtype uint8, no scaling is done. Parameters ---------- data : ndarray PIL image data array. cmin : sca...
1.569642
1.743761
0.900147
im = toimage(arr, mode=mode) ts = type(size) if issubdtype(ts, int): percent = size / 100.0 size = tuple((array(im.size)*percent).astype(int)) elif issubdtype(type(size), float): size = tuple((array(im.size)*size).astype(int)) else: size = (size[1], size[0]) ...
def imresize(arr, size, interp='bilinear', mode=None)
Resize an image. Parameters ---------- arr : ndarray The array of image to be resized. size : int, float or tuple * int - Percentage of current size. * float - Fraction of current size. * tuple - Size of the output image. interp : str, optional Interpolat...
2.693618
2.981876
0.90333
self.stop_on_empty_queue[channel] = True # by joining the we wait until its loop finishes. # it won't loop forever since we've set self.stop_on_empty_queue=True write_thread = self.thread_write_instances[channel] thread_join_non_blocking(write_thread)
def _end_channel(self, channel)
Soft end of ssh channel. End the writing thread as soon as the message queue is empty.
13.622161
11.034434
1.234514
if self.active and self.online is not False: self.logger.debug("client sends last %s messages ..." % ([str(i) + ':' + str(len(x)) for i, x in six.iteritems(self.queues)],)) for channel, messages in six.iteritems(self.queues): for id...
def wait_sending_last_messages(self)
Requests all channels to close and waits for it.
7.795324
7.304128
1.067249
state = {'message': ''} self.logger.debug("wait_until_queue_empty: report=%s %s" % (str(report), str([channel+':'+str(len(self.queues[channel])) for channel in channels]), )) queues = [] for channel in channels: queues += self.queues[channe...
def wait_until_queue_empty(self, channels, report=True, clear_end=True)
Waits until all queues of channels are empty.
3.081735
3.042445
1.012914
if not self.is_connected(channel): return False message['_sending'] = True if '_data' in message: data = message['_data'] else: data = msgpack.packb(message, default=invalid_json_values) self.bytes_total += len(data) ...
def send_message(self, message, channel)
Internal. Sends the actual message from a queue entry.
3.244554
3.201363
1.013492
unpacker = msgpack.Unpacker(encoding='utf-8') while True: try: start = time.time() chunk = self.ssh_channel[channel].recv(1024) end = time.time() self.read_speeds.append( len(chunk) / (end-start) ) if ...
def wait_for_at_least_one_message(self, channel)
Reads until we receive at least one message we can unpack. Return all found messages.
4.133142
4.054899
1.019296
if not self.ssh_channel[channel].recv_ready(): return try: start = time.time() chunk = self.ssh_channel[channel].recv(1024) end = time.time() self.read_speeds.append(len(chunk) / (end-start)) if len(self.read_speeds) > 2...
def read(self, channel)
Reads from the socket and tries to unpack the message. If successful (because msgpack was able to unpack) then we return that message. Else None. Keep calling .read() when new data is available so we try it again.
3.144205
3.06041
1.02738
if hasattr(signal, 'CTRL_C_EVENT'): # windows. Need CTRL_C_EVENT to raise the signal in the whole process group os.kill(os.getpid(), signal.CTRL_C_EVENT) else: # unix. pgid = os.getpgid(os.getpid()) if pgid == 1: os.kill(os.getpid(), signal.SIGINT) ...
def raise_sigint()
Raising the SIGINT signal in the current process and all sub-processes. os.kill() only issues a signal in the current process (without subprocesses). CTRL+C on the console sends the signal to the process group (which we need).
2.547083
2.308829
1.103192
if size_bytes == 1: # because I really hate unnecessary plurals return "1 byte" suffixes_table = [('bytes',0),('KB',0),('MB',1),('GB',2),('TB',2), ('PB',2)] num = float(size_bytes) for suffix, precision in suffixes_table: if num < 1024.0: break num /= 1...
def human_size(size_bytes, precision=0)
Format a size in bytes into a 'human' file size, e.g. bytes, KB, MB, GB, TB, PB Note that bytes/KB will be reported in whole numbers but MB and above will have greater precision e.g. 1 byte, 43 bytes, 443 KB, 4.3 MB, 4.43 GB, etc
2.097553
2.197204
0.954647
from PIL import Image if x.ndim != 3: raise Exception('Unsupported shape : ', str(x.shape), '. Need (channels, width, height)') if scale: x += max(-np.min(x), 0) x /= np.max(x) x *= 255 if x.shape[0] == 3: # RGB if x.dtype != 'uint8': x = ...
def array_to_img(x, scale=True)
x should be shape (channels, width, height)
2.181331
2.039351
1.06962
self.nb_val_samples = number diff_to_batch = number % self.get_batch_size() if diff_to_batch > 0: self.nb_val_samples += self.get_batch_size() - diff_to_batch import keras if '1' != keras.__version__[0]: self.nb_val_samples = self.nb_val_samples...
def set_generator_validation_nb(self, number)
sets self.nb_val_samples which is used in model.fit if input is a generator :param number: :return:
3.298131
2.879462
1.145398
self.samples_per_epoch = number diff_to_batch = number % self.get_batch_size() if diff_to_batch > 0: self.samples_per_epoch += self.get_batch_size() - diff_to_batch
def set_generator_training_nb(self, number)
sets self.samples_per_epoch which is used in model.fit if input is a generator :param number: :return:
3.352218
2.72343
1.230881
bid = id(buffer) self.attach_last_messages[bid] = b'' def reader(): current_line = b'' def handle_line(buf): if chunk == b'': return if read_line and callable(read_line): res = read_line(...
def attach(self, buffer, read_line=None)
Read buffer until end (read() returns '') and sends it to self.logger and self.job_backend. :param buffer: a buffer instance with block read() or readline() method :param read_line: callable or True to read line per line. If callable is given, it will be executed per line and ...
4.25204
4.109287
1.034739
if not isinstance(image, np.ndarray): raise ValueError('Expected ndarray') if ratio < 1: raise ValueError('Ratio must be greater than 1 (ratio=%f)' % ratio) width = int(math.floor(image.shape[1] * ratio)) height = int(math.floor(image.shape[0] * ratio)) channels = image.shape[2]...
def upscale(image, ratio)
return upscaled image array Arguments: image -- a (H,W,C) numpy.ndarray ratio -- scaling factor (>1)
2.085211
2.21364
0.941983
if image is None: return None elif isinstance(image, PIL.Image.Image): pass elif isinstance(image, np.ndarray): image = PIL.Image.fromarray(image) else: raise ValueError('image must be a PIL.Image or a np.ndarray') # Read format from the image fmt = image.fo...
def embed_image_html(image)
Returns an image embedded in HTML base64 format (Based on Caffe's web_demo) Arguments: image -- a PIL.Image or np.ndarray
2.426758
2.325336
1.043616
def expanded_bbox(bbox, n): l = min(bbox[0][0], bbox[1][0]) r = max(bbox[0][0], bbox[1][0]) t = min(bbox[0][1], bbox[1][1]) b = max(bbox[0][1], bbox[1][1]) return ((l - n, t - n), (r + n, b + n)) from PIL import Image, ImageDraw draw = ImageDraw.Draw(im...
def add_bboxes_to_image(image, bboxes, color='red', width=1)
Draw rectangles on the image for the bounding boxes Returns a PIL.Image Arguments: image -- input image bboxes -- bounding boxes in the [((l, t), (r, b)), ...] format Keyword arguments: color -- color to draw the rectangles width -- line width of the rectangles Example: image = Image...
2.116793
1.994944
1.061079
if channel_order not in ['RGB', 'BGR']: raise ValueError('Unsupported channel_order %s' % channel_order) if data.ndim == 1: # interpret as 1x1 grayscale images # (N, 1, 1) data = data[:, np.newaxis, np.newaxis] elif data.ndim == 2: # interpret as 1x1 grayscale im...
def get_layer_vis_square(data, allow_heatmap=True, normalize=True, min_img_dim=100, max_width=1200, channel_order='RGB', colormap='jet', )
Returns a vis_square for the given layer data Arguments: data -- a np.ndarray Keyword arguments: allow_heatmap -- if True, convert single channel images to heatmaps normalize -- whether to normalize the data when visualizing max_width -- maximum width for the vis_square
1.757838
1.795011
0.979291
if os.getenv('AETROS_GIT_INDEX_FILE'): self.index_path = os.getenv('AETROS_GIT_INDEX_FILE') return import tempfile h, path = tempfile.mkstemp('aetros-git', '', self.temp_path) self.index_path = path # we give git a unique file path for that ind...
def prepare_index_file(self)
Makes sure that GIT index file we use per job (by modifying environment variable GIT_INDEX_FILE) is not locked and empty. Git.fetch_job uses `git read-tree` to updates this index. For new jobs, we start with an empty index - that's why we delete it every time.
6.221183
5.424818
1.1468
self.job_id = job_id self.logger.debug("Git fetch job reference %s" % (self.ref_head, )) out, code, err = self.command_exec(['ls-remote', 'origin', self.ref_head]) if code: self.logger.error('Could not find the job ' + job_id + ' on the server. Are you online and d...
def fetch_job(self, job_id, checkout=False)
Fetch the current job reference (refs/aetros/job/<id>) from origin and (when checkout=True)read its tree to the current git index and checkout into working director.
4.98996
4.501575
1.108492
self.job_id = job_id commit = self.get_head_commit() self.logger.debug('Job ref points to ' + commit) self.command_exec(['read-tree', self.ref_head]) if checkout: self.logger.debug('Working directory in ' + self.work_tree) # make sure we have c...
def read_job(self, job_id, checkout=False)
Reads head and reads the tree into index, and checkout the work-tree when checkout=True. This does not fetch the job from the actual server. It needs to be in the local git already.
7.698386
7.080559
1.087257
self.add_file('aetros/job.json', simplejson.dumps(data, indent=4)) tree_id = self.write_tree() self.job_id = self.command_exec(['commit-tree', '-m', "JOB_CREATED", tree_id])[0].decode('utf-8').strip() out, code, err = self.command_exec(['show-ref', self.ref_head], allowed_to_f...
def create_job_id(self, data)
Create a new job id and reference (refs/aetros/job/<id>) by creating a new commit with empty tree. That root commit is the actual job id. A reference is then created to the newest (head) commit of this commit history. The reference will always be updated once a new commit is added.
7.883118
6.900421
1.142411
self.active_thread = True self.active_push = True self.thread_push_instance = Thread(target=self.thread_push) self.thread_push_instance.daemon = True self.thread_push_instance.start()
def start_push_sync(self)
Starts the detection of unsynced Git data.
3.107989
2.89864
1.072223
self.active_thread = False if self.thread_push_instance and self.thread_push_instance.isAlive(): self.thread_push_instance.join() with self.batch_commit('STREAM_END'): for path, handle in six.iteritems(self.streamed_files.copy()): # open again a...
def stop(self)
Stops the `git push` thread and commits all streamed files (Git.store_file and Git.stream_file), followed by a final git push. You can not start the process again.
2.914707
2.682006
1.086764
class controlled_execution: def __init__(self, git, message): self.git = git self.message = message def __enter__(self): self.git.git_batch_commit = True if self.git.job_id: # make sure we're a...
def batch_commit(self, message)
Instead of committing a lot of small commits you can batch it together using this controller. Example: with git.batch_commit('BATCHED'): git.commit_file('my commit 1', 'path/to/file', 'content from file') git.commit_json_file('[1, 2, 3]', 'path/to/file2', 'json array') ...
2.962157
2.923125
1.013353