Example #1
0
    def inv_eval(vtrace):

        constraints = []

        # frozen data used for removing duplicate points
        frozen_data = []
        for vt in vtrace:

            # add fault tolerency: if the field is null, ignore it
            if vt.x1 != None and vt.y1 != None:
                # each end of an point will only be added once
                p1 = json.dumps(
                    {
                        "c_x": vt.x1,
                        "c_y": vt.y1,
                        "c_size": vt.size,
                        "c_color": vt.color,
                        "c_column": vt.column
                    },
                    sort_keys=True)
                if p1 not in frozen_data: frozen_data.append(p1)

            if vt.x2 != None and vt.y2 != None:
                p2 = json.dumps(
                    {
                        "c_x": vt.x2,
                        "c_y": vt.y2,
                        "c_size": vt.size,
                        "c_color": vt.color,
                        "c_column": vt.column
                    },
                    sort_keys=True)
                if p2 not in frozen_data: frozen_data.append(p2)

            # there should not be any points between these two
            constraints.append("""
                (not (exists (r Tuple) (and (> r.c_x vt.x1) 
                                            (< r.c_x vt.x2) 
                                            (= r.c_color vt.color) 
                                            (= r.c_column vt.column))))""")

        data_values = [json.loads(r) for r in frozen_data]
        unused_fields = remove_unused_fields(data_values)

        encodings = []
        for channel, enc_ty in [("x", "_"), ("y", "_"), ("size", "nominal"),
                                ("color", "nominal"), ("column", "nominal")]:
            field_name = "c_{}".format(channel)
            if field_name in unused_fields:
                continue
            if channel in ["x", "y"]:
                dtype = table_utils.infer_dtype(
                    [r[field_name] for r in data_values])
                enc_ty = "nominal" if dtype == "string" else "quantitative"

            encodings.append(Encoding(channel, field_name, enc_ty))

        bar_chart = LineChart(encodings=encodings)
        return [(SymTable(values=data_values,
                          constraints=constraints), bar_chart)]
Example #2
0
    def inv_eval(vtrace):
        data_values = []
        constraints = []

        for vt in vtrace:
            # min max will appear in the table
            data_values.append({
                "c_x": vt.x,
                "c_y": vt.min,
                "c_color": vt.color,
                "c_column": vt.column
            })
            data_values.append({
                "c_x": vt.x,
                "c_y": vt.max,
                "c_color": vt.color,
                "c_column": vt.column
            })

            # the output table should satisfy these constraints
            constraints.append(
                "min([r.c_y for r in T if r.c_color == {} and r.c_column == {}]) = {}"
                .format(vt.color, vt.column, vt.min))
            constraints.append(
                "max([r.c_y for r in T if r.c_color == {} and r.c_column == {}]) = {}"
                .format(vt.color, vt.column, vt.max))
            constraints.append(
                "Q1([r.c_y for r in T if r.c_color == {} and r.c_column == {}]) = {}"
                .format(vt.color, vt.column, vt.Q1))
            constraints.append(
                "Q3([r.c_y for r in T if r.c_color == {} and r.c_column == {}]) = {}"
                .format(vt.color, vt.column, vt.Q3))
            constraints.append(
                "median([r.c_y for r in T if r.c_color == {} and r.c_column == {}]) = {}"
                .format(vt.color, vt.column, vt.median))

        # remove fields that contain none values
        unused_fields = remove_unused_fields(data_values)

        encodings = []
        for channel, enc_ty in [("x", "_"), ("y", "_"), ("color", "nominal"),
                                ("column", "nominal")]:
            field_name = "c_{}".format(channel)
            if field_name in unused_fields:
                continue

            if channel in ["x", "y"]:
                # the type needs to be determined by datatype
                dtype = table_utils.infer_dtype(
                    [r[field_name] for r in data_values])
                enc_ty = "nominal" if dtype == "string" else "quantitative"

            encodings.append(Encoding(channel, field_name, enc_ty))

        chart = BoxPlot(encodings=encodings)
        return [(SymTable(data_values, constraints), chart)]
Example #3
0
 def _update_encoding_type(layer_spec, layer_data):
     """use side effect to update encodings"""
     mark = layer_spec["mark"]["type"] if isinstance(
         layer_spec["mark"], (dict, )) else layer_spec["mark"]
     if "color" in layer_spec["encoding"]:
         field_data = set([
             r[layer_spec["encoding"]["color"]["field"]] for r in layer_data
         ])
         dtype = table_utils.infer_dtype(field_data)
         enc_ty = "nominal" if dtype == "string" else "quantitative"
         layer_spec["encoding"]["color"]["type"] = enc_ty
Example #4
0
    def inv_eval(vtrace):

        frozen_data = []
        for vt in vtrace:
            # each end of an point will only be added once
            p1 = json.dumps(
                {
                    "c_x": vt.x1,
                    "c_y": vt.yt1,
                    "c_y2": vt.yb1,
                    "c_color": vt.color,
                    "c_column": vt.column
                },
                sort_keys=True)
            p2 = json.dumps(
                {
                    "c_x": vt.x2,
                    "c_y": vt.yt2,
                    "c_y2": vt.yb2,
                    "c_color": vt.color,
                    "c_column": vt.column
                },
                sort_keys=True)
            if p1 not in frozen_data: frozen_data.append(p1)
            if p2 not in frozen_data: frozen_data.append(p2)

        data_values = [json.loads(r) for r in frozen_data]
        channel_types = [("x", "_"), ("y", "quantitative"),
                         ("y2", "quantitative"), ("color", "nominal"),
                         ("column", "nominal")]

        # remove fields that contain none values
        unused_fields = remove_unused_fields(data_values)

        encodings = []
        for channel, enc_ty in channel_types:
            field_name = "c_{}".format(channel)
            if field_name in unused_fields:
                continue
            if channel == "x":
                dtype = table_utils.infer_dtype(
                    [r[field_name] for r in data_values])
                enc_ty = "nominal" if dtype == "string" else "quantitative"
            encodings.append(Encoding(channel, field_name, enc_ty))

        chart = AreaChart(encodings=encodings)

        return [(SymTable(values=data_values), chart)]
Example #5
0
	def _update_encoding_type(layer_spec, layer_data):
		"""use side effect to update encodings"""
		mark = layer_spec["mark"]["type"] if isinstance(layer_spec["mark"], (dict,)) else layer_spec["mark"]
		if "color" in layer_spec["encoding"]:
			field_data = set([r[layer_spec["encoding"]["color"]["field"]] for r in layer_data])
			dtype = table_utils.infer_dtype(field_data)
			enc_ty = "nominal" if dtype == "string" else "quantitative"
			layer_spec["encoding"]["color"]["type"] = enc_ty
		if mark == "rect":
			for ch in layer_spec["encoding"]:
				if ch in ["x", "y"]:
					if layer_spec["encoding"][ch]["type"] != "nominal":
						layer_spec["encoding"][ch]["type"] = "nominal"

		for ch in layer_spec["encoding"]:

			if (ch == "x" or ch == "y") and mark == "rect":
				# rect won't support temporal
				continue

			field_data = set([r[layer_spec["encoding"][ch]["field"]] for r in layer_data])
			if all([is_date(d) for d in field_data]):
				layer_spec["encoding"][ch]["type"] = "temporal"
Example #6
0
    def inv_eval(vtrace):

        mark_ty = "rect" if vtrace[0].point_shape == "rect" else "point"

        data_values = []
        for vt in vtrace:
            data_values.append({
                "c_x": vt.x,
                "c_y": vt.y,
                "c_size": vt.size,
                "c_color": vt.color,
                "c_shape": vt.shape,
                "c_column": vt.column
            })

        # remove fields that contain none values
        unused_fields = remove_unused_fields(data_values)

        encodings = []
        for channel, enc_ty in [("x", "_"), ("y", "_"), ("size", "_"),
                                ("color", "nominal"), ("shape", "nominal"),
                                ("column", "nominal")]:
            field_name = "c_{}".format(channel)
            if field_name in unused_fields:
                continue

            if channel in ["x", "y", "size"] or (channel == "color"
                                                 and mark_ty == "rect"):
                # the type needs to be determined by datatype
                dtype = table_utils.infer_dtype(
                    [r[field_name] for r in data_values])
                enc_ty = "nominal" if dtype == "string" else "quantitative"

            encodings.append(Encoding(channel, field_name, enc_ty))

        chart = ScatterPlot(mark_ty=mark_ty, encodings=encodings)
        return [(SymTable(values=data_values), chart)]