def generate(shape, chunks=(), n_steps=1, **kwargs): """Generate microstructures and responses for Cahn-Hilliard. Interface to generate random concentration fields and their evolution to be used for the fit method in the localization regression model. Args: shape: the shape of the microstructures where the first index is the number of samples chunks: chunks argument to make the Dast array n_steps: number of time steps used **kwargs: parameters for CH model Returns: Tuple containing the microstructures and responses. Raises: RuntimeError if domain is not square Example >>> x_data, y_data = generate((1, 6, 6)) >>> print(y_data.chunks) ((1,), (6,), (6,)) """ return pipe( 2 * da.random.random(shape, chunks=chunks or shape) - 1, juxt(identity, map_blocks(iterate_times(solve(**kwargs), n_steps))), )
def save_time_data(ctx): """Dump all the time data to a CSV dat file All the time data with each column a differnt qunatity and each row a different time step. Args: ctx: the Click context from the base command """ read_and_save( "time.csv", [ "time", "a_01", "a_10", "a_d", "elastic_free_energy", "gradient_free_energy", "total_free_energy", "precipitate_area", ], juxt( calc_elapsed_time, calc_position_01, calc_position_10, calc_position_d, calc_elastic_free_energy, calc_gradient_free_energy, calc_total_free_energy, calc_total_area, ), )(ctx)
def linspace_(arr, spacing): """Calcuate the linspace based on a spacing """ return pipe( arr, juxt(min, max), tlam(lambda x_, y_: np.linspace(x_, y_, (y_ - x_) / spacing)) )
def time_ratio(data): """Calcuate the sim_time over wall_time ration """ return pipe( data[-1], juxt(lambda x: x.get('sim_time', x.get('time')), lambda x: x.get('wall_time', x.get('time'))), lambda x: float(x[0]) / float(x[1]))
def check_lines(lines): errors = [ msg for boolean, msg in juxt( not_correct_order, not_enough_tags, )(lines) if boolean ] if errors: return False, '\n'.join(errors) return True, ''
def time_ratio(data): """Calcuate the sim_time over wall_time ration """ def not0(value): """Set to 1e-10 if 0 """ if value == 0: return 1e-10 return value return pipe( data[-1], juxt(lambda x: x.get('sim_time', x.get('time')), lambda x: x.get('wall_time', x.get('time'))), lambda x: float(x[0]) / not0(float(x[1])))
def add_weight(answer: dict): def is_a_matching_question(answer): return pipe( [answer_keys.match_left, answer_keys.incorrect], map(lambda k: k in answer), any, ) needs_weight = compose( any, juxt(complement(is_a_matching_question), ), ) if needs_weight(answer): return assoc(answer, answer_keys.weight, int(answer.get(answer_keys.weight, 0) and 100)) return answer
def run(self, maxiter=1e3): self.maxiter = int(maxiter) starting_iteration = len(self) callback_func = juxt(*self.callbacks) # init display if self.display: self.display.start() try: for ix, theta in enumerate(self): k = starting_iteration + ix obj = self.obj(theta) if self.gradient: grad = self.restruct(self.gradient(theta)) else: grad = None if len(self.runtimes) == 0: rt = 0. else: rt = self.runtimes[-1] # build the datum d = Datum(k, obj, self.restruct(theta), grad, rt) # farm out to callbacks callback_func(d) # display/storage if self.display is not None: self.display(d) if self.storage is not None: self.storage(d) except KeyboardInterrupt: pass self.display.cleanup(d, self.runtimes) if self.display else None self.theta = self.restruct(theta)
lambda x: func(x) * (x["kappa"] / 2) * calc_dx2(x), np.array, np.sum, ) @cli.command() @click.pass_context def total_free_energy(ctx): """Command to plot the total free energy """ read_and_plot(calc_total_free_energy)(ctx) calc_total_free_energy = sequence( juxt(calc_bulk_free_energy, calc_gradient_free_energy, calc_elastic_free_energy), sum, ) @cli.command() @click.pass_context def total_area(ctx): """Command to plot the precipitate area """ read_and_plot(calc_total_area)(ctx) calc_total_area = sequence( lambda x: (x["eta"] > 0.5) * calc_dx2(x["params"].item()), np.sum )
def _to_single_nn(self, shape, image_id): return pipe(image_id, juxt(self.image_for, self.mask_for), map(self._resize_image(shape)), list)
def juxtapose(func, x): return juxt(*func)(x)
def verify(*conditions) -> Callable[[Any], bool]: return compose( reduce(and_), juxt(*conditions) )
def __call__(self, *args, **kwargs): return juxt(*self)(*args, **kwargs)
def run(self, maxiter=1e3, tol=(1e-18, 1e-18, 1e-16)): # reset exit message (for display) self.exit_message = None # tolerance tol = namedtuple('tolerance', ['obj', 'param', 'grad'])(*tol) self.maxiter = int(maxiter) starting_iteration = len(self) callback_func = juxt(*self.callbacks) # store previous iterates obj_prev = np.Inf theta_prev = np.Inf # init display if self.display is not None: self.display.start() display_batch_size = self.display.every else: display_batch_size = 1 try: for ix, theta in enumerate(self): k = starting_iteration + ix obj = self.obj(theta) if self.gradient: grad = self.restruct(self.gradient(theta)) else: grad = None # hack to get around the first iteration runtime if ix >= 1: # collect a bunch of information for the current iterate d = Datum(k, obj, grad, self.restruct(theta), np.sum(self.runtimes[-display_batch_size:])) # send out to callbacks callback_func(d) # display/storage if self.display is not None: self.display(d) if self.storage is not None: self.storage(d) # tolerance if grad is not None: if np.linalg.norm(destruct(grad), 2) <= (tol.grad * np.sqrt(theta.size)): self.exit_message = 'Stopped on interation {}. Scaled gradient norm: {}'.format(ix, np.sqrt(theta.size) * np.linalg.norm(destruct(grad), 2)) break elif np.abs(obj - obj_prev) <= tol.obj: self.exit_message = 'Stopped on interation {}. Objective value not changing, |f(x^k) - f(x^{k+1})|: {}'.format(ix, np.abs(obj - obj_prev)) break elif np.linalg.norm(theta - theta_prev, 2) <= (tol.param * np.sqrt(theta.size)): self.exit_message = 'Stopped on interation {}. Parameters not changing, \sqrt(dim) * ||x^k - x^{k+1}||_2: {}'.format(ix, np.sqrt(theta.size) * np.linalg.norm(theta - theta_prev, 2)) break theta_prev = theta.copy() obj_prev = obj except KeyboardInterrupt: pass self.display.cleanup(d, self.runtimes, self.exit_message) if self.display else None self.theta = self.restruct(theta)
def read_and_plot(f_calc): """Read in a file and plot using f_calc """ return sequence(read_and_calc(juxt(get("step_counter"), f_calc)), plot2d)
"EXPOSE 5000\n" "\n" "ENTRYPOINT [ \"bokeh-server\" ]\n" "CMD [ \"--ip=0.0.0.0\", \"--port=5000\" ]\n" ).format(version=version) def write_file((filename, data)): print("Write '{}' .. ".format(filename), end="") with open(filename, 'w') as f: f.write(data) print("OK") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Generates bokeh Dockerfile for given version") parser.add_argument("version", type=str, nargs="*") args = parser.parse_args() list(map( compose( write_file, juxt([ "Dockerfile_{}".format, get_dockerfile ]) ), args.version ))
import operator import toolz.curried as toolz import pyramda as R reg_typename = "{}_reg_t".format rin_name = "{}_rin".format r_name = "{}_r".format v_name = "{}_v".format architecture_id = "two_process_{}".format indent = R.curry_n( 2, toolz.comp( "\n".join, R.apply(map), R.unapply(toolz.juxt([ toolz.comp(R.add, R.multiply(" "), toolz.first), toolz.comp(operator.methodcaller("split", "\n"), toolz.second)]))))
import operator import toolz.curried as toolz import pyramda as R reg_typename = "{}_reg_t".format rin_name = "{}_rin".format r_name = "{}_r".format v_name = "{}_v".format architecture_id = "two_process_{}".format indent = R.curry_n( 2, toolz.comp( "\n".join, R.apply(map), R.unapply( toolz.juxt([ toolz.comp(R.add, R.multiply(" "), toolz.first), toolz.comp(operator.methodcaller("split", "\n"), toolz.second) ]))))