def apply(self, f, x, xstart, xstop, y, ystart, ystop, evaluation, options): 'DensityPlot[f_, {x_Symbol, xstart_, xstop_}, {y_Symbol, ystart_, ystop_}, OptionsPattern[DensityPlot]]' x = x.get_name() y = y.get_name() color_function = self.get_option(options, 'ColorFunction', evaluation, pop=True) color_function_scaling = self.get_option(options, 'ColorFunctionScaling', evaluation, pop=True) color_function_min = color_function_max = None if color_function.get_name() == 'Automatic': color_function = String('LakeColors') if color_function.get_string_value(): func = Expression('ColorData', color_function.get_string_value()).evaluate(evaluation) if func.has_form('ColorDataFunction', 4): color_function_min = func.leaves[2].leaves[0].get_real_value() color_function_max = func.leaves[2].leaves[1].get_real_value() color_function = Expression('Function', Expression(func.leaves[3], Expression('Slot', 1))) else: evaluation.message('DensityPlot', 'color', func) return if color_function.has_form('ColorDataFunction', 4): color_function_min = color_function.leaves[2].leaves[0].get_real_value() color_function_max = color_function.leaves[2].leaves[1].get_real_value() color_function_scaling = color_function_scaling.is_true() try: xstart, xstop, ystart, ystop = [value.to_number(n_evaluation=evaluation) for value in (xstart, xstop, ystart, ystop)] except NumberError, exc: expr = Expression('DensityPlot', f, Expression('List', x, xstart, xstop), Expression('List', y, ystart, ystop), *options_to_rules(options)) evaluation.message('DensityPlot', 'plln', exc.value, expr) return
def apply(self, f, x, xstart, xstop, y, ystart, ystop, evaluation, options): 'DensityPlot[f_, {x_Symbol, xstart_, xstop_}, {y_Symbol, ystart_, ystop_}, OptionsPattern[DensityPlot]]' x = x.get_name() y = y.get_name() color_function = self.get_option(options, 'ColorFunction', evaluation, pop=True) color_function_scaling = self.get_option(options, 'ColorFunctionScaling', evaluation, pop=True) color_function_min = color_function_max = None if color_function.get_name() == 'Automatic': color_function = String('LakeColors') if color_function.get_string_value(): func = Expression( 'ColorData', color_function.get_string_value()).evaluate(evaluation) if func.has_form('ColorDataFunction', 4): color_function_min = func.leaves[2].leaves[0].get_real_value() color_function_max = func.leaves[2].leaves[1].get_real_value() color_function = Expression( 'Function', Expression(func.leaves[3], Expression('Slot', 1))) else: evaluation.message('DensityPlot', 'color', func) return if color_function.has_form('ColorDataFunction', 4): color_function_min = color_function.leaves[2].leaves[ 0].get_real_value() color_function_max = color_function.leaves[2].leaves[ 1].get_real_value() color_function_scaling = color_function_scaling.is_true() try: xstart, xstop, ystart, ystop = [ value.to_number(n_evaluation=evaluation) for value in (xstart, xstop, ystart, ystop) ] except NumberError, exc: expr = Expression('DensityPlot', f, Expression('List', x, xstart, xstop), Expression('List', y, ystart, ystop), *options_to_rules(options)) evaluation.message('DensityPlot', 'plln', exc.value, expr) return
def _load_spacy(self, evaluation, options): language_code = None language_name = self.get_option(options, 'Language', evaluation) if language_name is None: language_name = String('Undefined') if isinstance(language_name, String): language_code = _SpacyBuiltin._language_codes.get(language_name.get_string_value()) if not language_code: evaluation.message(self.get_name(), 'lang', language_name, strip_context(self.get_name())) return None instance = _SpacyBuiltin._spacy_instances.get(language_code) if instance: return instance try: if 'SPACY_DATA' in os.environ: instance = spacy.load(language_code, via=os.environ['SPACY_DATA']) else: instance = spacy.load(language_code) _SpacyBuiltin._spacy_instances[language_code] = instance return instance except RuntimeError as e: evaluation.message(self.get_name(), 'runtime', str(e)) return None
def _load_spacy(self, evaluation, options): language_code = None language_name = self.get_option(options, 'Language', evaluation) if language_name is None: language_name = String('Undefined') if isinstance(language_name, String): language_code = _SpacyBuiltin._language_codes.get(language_name.get_string_value()) if not language_code: evaluation.message(self.get_name(), 'lang', language_name, strip_context(self.get_name())) return None instance = _SpacyBuiltin._spacy_instances.get(language_code) if instance: return instance try: if 'SPACY_DATA' in os.environ: instance = spacy.load(language_code, via=os.environ['SPACY_DATA']) else: instance = spacy.load(language_code) _SpacyBuiltin._spacy_instances[language_code] = instance return instance except RuntimeError as e: evaluation.message(self.get_name(), 'runtime', str(e)) return None
def apply_alpha_str(self, chars: List[Any], alpha: String, evaluation): "LetterNumber[chars_, alpha_String]" alphakey = alpha.get_string_value() alphakey = alphabet_alias.get(alphakey, None) if alphakey is None: evaluation.message("LetterNumber", "nalph", alpha) return if alphakey == "English": return self.apply(chars, evaluation) alphabet = alphabet_descriptions.get(alphakey, None) if alphabet is None: evaluation.message("LetterNumber", "nalph", alpha) return # TODO: handle Uppercase if isinstance(chars, String): py_chars = chars.get_string_value() if len(py_chars) == 1: # FIXME generalize ord("a") res = alphabet["Lowercase"].find(py_chars) + 1 if res == -1: res = alphabet["Uppercase"].find(py_chars) + 1 return Integer(res) else: r = [] for c in py_chars: cp = alphabet["Lowercase"].find(c) + 1 if cp == -1: cp = alphabet["Uppercase"].find(c) + 1 r.append(cp) return Expression(SymbolList, *r) elif chars.has_form("List", 1, None): result = [] for leaf in chars.leaves: result.append(self.apply_alpha_str(leaf, alpha, evaluation)) return Expression(SymbolList, *result) else: return evaluation.message(self.__class__.__name__, "nas", chars) return None
def construct_graphics(self, triangles, mesh_points, v_min, v_max, options, evaluation): mesh_option = self.get_option(options, 'Mesh', evaluation) mesh = mesh_option.to_python() color_function = self.get_option( options, 'ColorFunction', evaluation, pop=True) color_function_scaling = self.get_option( options, 'ColorFunctionScaling', evaluation, pop=True) color_function_min = color_function_max = None if color_function.get_name() == 'Automatic': color_function = String('LakeColors') if color_function.get_string_value(): func = Expression( 'ColorData', color_function.get_string_value()).evaluate(evaluation) if func.has_form('ColorDataFunction', 4): color_function_min = func.leaves[2].leaves[0].get_real_value() color_function_max = func.leaves[2].leaves[1].get_real_value() color_function = Expression('Function', Expression( func.leaves[3], Expression('Slot', 1))) else: evaluation.message('DensityPlot', 'color', func) return if color_function.has_form('ColorDataFunction', 4): color_function_min = \ color_function.leaves[2].leaves[0].get_real_value() color_function_max = \ color_function.leaves[2].leaves[1].get_real_value() color_function_scaling = color_function_scaling.is_true() v_range = v_max - v_min if v_range == 0: v_range = 1 if color_function.has_form('ColorDataFunction', 4): color_func = color_function.leaves[3] else: color_func = color_function if (color_function_scaling and # noqa color_function_min is not None and color_function_max is not None): color_function_range = color_function_max - color_function_min colors = {} def eval_color(x, y, v): v_scaled = (v - v_min) / v_range if (color_function_scaling and # noqa color_function_min is not None and color_function_max is not None): v_color_scaled = color_function_min + \ v_scaled * color_function_range else: v_color_scaled = v # Calculate and store 100 different shades max. v_lookup = int(v_scaled * 100 + 0.5) value = colors.get(v_lookup) if value is None: value = Expression(color_func, Real(v_color_scaled)) value = value.evaluate(evaluation) colors[v_lookup] = value return value points = [] vertex_colors = [] graphics = [] for p in triangles: points.append( Expression('List', *(Expression('List', *x[:2]) for x in p))) vertex_colors.append( Expression('List', *(eval_color(*x) for x in p))) graphics.append(Expression( 'Polygon', Expression('List', *points), Expression('Rule', Symbol('VertexColors'), Expression('List', *vertex_colors)))) if mesh == 'Full': for xi in range(len(mesh_points)): line = [] for yi in range(len(mesh_points[xi])): line.append(Expression('List', mesh_points[xi][yi][0], mesh_points[xi][yi][1])) graphics.append(Expression('Line', Expression('List', *line))) elif mesh == 'All': for p in triangles: graphics.append(Expression( 'Line', Expression('List', *(from_python(x[:2]) for x in p)))) return graphics
def construct_graphics(self, triangles, mesh_points, v_min, v_max, options, evaluation): mesh_option = self.get_option(options, "Mesh", evaluation) mesh = mesh_option.to_python() color_function = self.get_option(options, "ColorFunction", evaluation, pop=True) color_function_scaling = self.get_option(options, "ColorFunctionScaling", evaluation, pop=True) color_function_min = color_function_max = None if color_function.get_name() == "Automatic": color_function = String("LakeColors") if color_function.get_string_value(): func = Expression("ColorData", color_function.get_string_value()).evaluate(evaluation) if func.has_form("ColorDataFunction", 4): color_function_min = func.leaves[2].leaves[0].get_real_value() color_function_max = func.leaves[2].leaves[1].get_real_value() color_function = Expression("Function", Expression(func.leaves[3], Expression("Slot", 1))) else: evaluation.message("DensityPlot", "color", func) return if color_function.has_form("ColorDataFunction", 4): color_function_min = color_function.leaves[2].leaves[0].get_real_value() color_function_max = color_function.leaves[2].leaves[1].get_real_value() color_function_scaling = color_function_scaling.is_true() v_range = v_max - v_min if v_range == 0: v_range = 1 if color_function.has_form("ColorDataFunction", 4): color_func = color_function.leaves[3] else: color_func = color_function if color_function_scaling and color_function_min is not None and color_function_max is not None: color_function_range = color_function_max - color_function_min colors = {} def eval_color(x, y, v): v_scaled = (v - v_min) / v_range if color_function_scaling and color_function_min is not None and color_function_max is not None: v_color_scaled = color_function_min + v_scaled * color_function_range else: v_color_scaled = v v_lookup = int(v_scaled * 100 + 0.5) # calculate and store 100 different shades max. value = colors.get(v_lookup) if value is None: value = Expression(color_func, Real(v_color_scaled)) value = value.evaluate(evaluation) colors[v_lookup] = value return value points = [] vertex_colors = [] graphics = [] for p1, p2, p3 in triangles: c1, c2, c3 = eval_color(*p1), eval_color(*p2), eval_color(*p3) points.append( Expression( "List", Expression("List", *p1[:2]), Expression("List", *p2[:2]), Expression("List", *p3[:2]) ) ) vertex_colors.append(Expression("List", c1, c2, c3)) graphics.append( Expression( "Polygon", Expression("List", *points), Expression("Rule", Symbol("VertexColors"), Expression("List", *vertex_colors)), ) ) if mesh == "Full": for xi in range(len(mesh_points)): line = [] for yi in range(len(mesh_points[xi])): line.append(Expression("List", mesh_points[xi][yi][0], mesh_points[xi][yi][1])) graphics.append(Expression("Line", Expression("List", *line))) elif mesh == "All": for p1, p2, p3 in triangles: line = [from_python(p1[:2]), from_python(p2[:2]), from_python(p3[:2])] graphics.append(Expression("Line", Expression("List", *line))) return graphics