Example #1
0
def main(args: argparse.Namespace):
    interval = serial_utils.make_interval(AST.Interval.Start, AST.Interval.End,
                                          0, 0)

    # create the stencil body AST
    body_ast = serial_utils.make_ast([
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("lap"),
            serial_utils.make_binary_operator(
                serial_utils.make_binary_operator(
                    serial_utils.make_literal_access_expr(
                        "-4.0", AST.BuiltinType.Float),
                    "*",
                    serial_utils.make_field_access_expr("in"),
                ),
                "+",
                serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("coeff"),
                    "*",
                    serial_utils.make_binary_operator(
                        serial_utils.make_field_access_expr("in", [1, 0, 0]),
                        "+",
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "in", [-1, 0, 0]),
                            "+",
                            serial_utils.make_binary_operator(
                                serial_utils.make_field_access_expr(
                                    "in", [0, 1, 0]),
                                "+",
                                serial_utils.make_field_access_expr(
                                    "in", [0, -1, 0]),
                            ),
                        ),
                    ),
                ),
            ),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("out"),
            serial_utils.make_binary_operator(
                serial_utils.make_binary_operator(
                    serial_utils.make_literal_access_expr(
                        "-4.0", AST.BuiltinType.Float),
                    "*",
                    serial_utils.make_field_access_expr("lap"),
                ),
                "+",
                serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("coeff"),
                    "*",
                    serial_utils.make_binary_operator(
                        serial_utils.make_field_access_expr("lap", [1, 0, 0]),
                        "+",
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "lap", [-1, 0, 0]),
                            "+",
                            serial_utils.make_binary_operator(
                                serial_utils.make_field_access_expr(
                                    "lap", [0, 1, 0]),
                                "+",
                                serial_utils.make_field_access_expr(
                                    "lap", [0, -1, 0]),
                            ),
                        ),
                    ),
                ),
            ),
            "=",
        ),
    ])

    vertical_region_stmt = serial_utils.make_vertical_region_decl_stmt(
        body_ast, interval, AST.VerticalRegion.Forward)

    sir = serial_utils.make_sir(
        OUTPUT_FILE,
        AST.GridType.Value("Cartesian"),
        [
            serial_utils.make_stencil(
                OUTPUT_NAME,
                serial_utils.make_ast([vertical_region_stmt]),
                [
                    serial_utils.make_field(
                        "in", serial_utils.make_field_dimensions_cartesian()),
                    serial_utils.make_field(
                        "out", serial_utils.make_field_dimensions_cartesian()),
                    serial_utils.make_field(
                        "coeff",
                        serial_utils.make_field_dimensions_cartesian()),
                    serial_utils.make_field(
                        "lap",
                        serial_utils.make_field_dimensions_cartesian(),
                        is_temporary=True),
                ],
            )
        ],
    )

    # print the SIR
    if args.verbose:
        print(MessageToJson(sir))

    # compile
    code = dawn4py.compile(sir, backend=dawn4py.CodeGenBackend.CUDA)

    # write to file
    print(f"Writing generated code to '{OUTPUT_PATH}'")
    with open(OUTPUT_PATH, "w") as f:
        f.write(code)
Example #2
0
 def state(self):
     if self.__state is None:
         return None
     return MessageToJson(self.__state)
Example #3
0
 def __str__(self):
     return str(MessageToJson(self.to_proto()))
Example #4
0
def _message_to_json(message):
    # preserving_proto_field_name keeps the JSON-serialized form snake_case
    return MessageToJson(message, preserving_proto_field_name=True)
Example #5
0
 def to_json(self):
     return MessageToJson(self._data, sort_keys=True)
Example #6
0
 def as_json(self):
     '''Returns a json str representation of the model response'''
     pb_msg_out = self.as_pb_msg()
     return MessageToJson(pb_msg_out, self._pb_output_type(), indent=0)
Example #7
0
def copy_fields():
    outputfile = "../input/test_set_stage_location_type_copy_fields.sir"
    interval = serial_utils.make_interval(
        SIR.Interval.Start, SIR.Interval.End, 0, 0)

    body_ast = serial_utils.make_ast(
        [
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("out_cell"),
                serial_utils.make_field_access_expr("in_cell"),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("out_edge"),
                serial_utils.make_field_access_expr("in_edge"),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("out_vertex"),
                serial_utils.make_field_access_expr("in_vertex"),
                "=",
            )
        ]
    )

    vertical_region_stmt = serial_utils.make_vertical_region_decl_stmt(
        body_ast, interval, SIR.VerticalRegion.Forward
    )

    sir = serial_utils.make_sir(
        outputfile,
        SIR.GridType.Value("Unstructured"),
        [
            serial_utils.make_stencil(
                "generated",
                serial_utils.make_ast([vertical_region_stmt]),
                [
                    serial_utils.make_field(
                        "in_cell",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Cell")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "out_cell",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Cell")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "in_edge",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "out_edge",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "in_vertex",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "out_vertex",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Vertex")], 1
                        ),
                    ),
                ],
            ),
        ],
    )
    f = open(outputfile, "w")
    f.write(MessageToJson(sir))
    f.close()
Example #8
0
def pb2json(pb):
    from google.protobuf.json_format import MessageToJson
    data = (MessageToJson(req))
    data = data.replace("\n", "").replace(" ", "")
    return data
Example #9
0
def run(args):
    # check if loading a json file
    if len(args.files) == 1 and args.files[0].endswith("json"):
        import json
        with open(args.files[0], 'r') as f:
            json_args = json.load(f)
            for key, value in json_args.items():
                setattr(args, key, value)

    # checks
    if args.extended_prediction_data_format not in ["pred", "json"]:
        raise Exception(
            "Only 'pred' and 'json' are allowed extended prediction data formats"
        )

    # add json as extension, resolve wildcard, expand user, ... and remove .json again
    args.checkpoint = [(cp if cp.endswith(".json") else cp + ".json")
                       for cp in args.checkpoint]
    args.checkpoint = glob_all(args.checkpoint)
    args.checkpoint = [cp[:-5] for cp in args.checkpoint]

    # create voter
    voter_params = VoterParams()
    voter_params.type = VoterParams.Type.Value(args.voter.upper())
    voter = voter_from_proto(voter_params)

    # load files
    input_image_files = glob_all(args.files)
    if args.text_files:
        args.text_files = glob_all(args.text_files)

    # skip invalid files and remove them, there wont be predictions of invalid files
    dataset = create_dataset(
        args.dataset,
        DataSetMode.PREDICT,
        input_image_files,
        args.text_files,
        skip_invalid=True,
        remove_invalid=True,
        args={'text_index': args.pagexml_text_index},
    )

    print("Found {} files in the dataset".format(len(dataset)))
    if len(dataset) == 0:
        raise Exception(
            "Empty dataset provided. Check your files argument (got {})!".
            format(args.files))

    # predict for all models
    predictor = MultiPredictor(checkpoints=args.checkpoint,
                               batch_size=args.batch_size,
                               processes=args.processes)
    do_prediction = predictor.predict_dataset(
        dataset, progress_bar=not args.no_progress_bars)

    # output the voted results to the appropriate files
    for result, sample in do_prediction:
        for i, p in enumerate(result):
            p.prediction.id = "fold_{}".format(i)

        # vote the results (if only one model is given, this will just return the sentences)
        prediction = voter.vote_prediction_result(result)
        prediction.id = "voted"
        sentence = prediction.sentence
        if args.verbose:
            lr = "\u202A\u202B"
            print("{}: '{}{}{}'".format(sample['id'],
                                        lr[get_base_level(sentence)], sentence,
                                        "\u202C"))

        output_dir = args.output_dir

        dataset.store_text(sentence,
                           sample,
                           output_dir=output_dir,
                           extension=".pred.txt")

        if args.extended_prediction_data:
            ps = Predictions()
            ps.line_path = sample['image_path']
            ps.predictions.extend([prediction] +
                                  [r.prediction for r in result])
            output_dir = output_dir if output_dir else os.path.dirname(
                sample['image_path'])
            if args.extended_prediction_data_format == "pred":
                with open(os.path.join(output_dir, sample['id'] + ".pred"),
                          'wb') as f:
                    f.write(ps.SerializeToString())
            elif args.extended_prediction_data_format == "json":
                with open(os.path.join(output_dir, sample['id'] + ".json"),
                          'w') as f:
                    # remove logits
                    for prediction in ps.predictions:
                        prediction.logits.rows = 0
                        prediction.logits.cols = 0
                        prediction.logits.data[:] = []

                    f.write(
                        MessageToJson(ps, including_default_value_fields=True))
            else:
                raise Exception("Unknown prediction format.")

    dataset.store()
    print("All files written")
Example #10
0
 def _gen_tx_proxy_address_topic(self, proxy_address):
     """Generate unique topic name specific to this proxy address for tx"""
     topic = 'tx:' + MessageToJson(proxy_address)
     return topic
    def dialogflow(self, question):

        resp = self.detectIntent(question)
        json_response = MessageToJson(resp)
        r = json.loads(json_response)

        print(r)
        responseId = r['responseId']
        queryResult = r['queryResult']
        queryText = queryResult['queryText']
        intent = queryResult['intent']
        action = queryResult['action']
        parameters = queryResult['parameters']
        order = ""

        f = open('price.json')
        price = json.load(f)

        if queryResult['action'] == 'order_veg':
            veg = parameters['vegetables']
            quan = parameters['number']

            if len(veg) != len(quan):
                for x in range(len(veg) - len(quan)):
                    parameters['number'].append(1.0)
            
            leafy = ['wheat grass', '']

            ## Iterating through all the vegetables in the parameters
            for i in range(len(veg)):
                veggie = veg[i].lower()
                ## Iterating through the vegetable categories in price.json
                for cate in price.keys():
                    ## If the vegetable type is part of the vegetable categories name
                    if veggie in cate:
                        variety1 = price[cate]
                        selection = []
                        for product in variety1.keys():
                            if veggie.capitalize() in product:
                                selection.append(product)

                        chosen_veg = input(f"Please provide the full name of the product you'd like to purchase: {selection}\n")

                        while chosen_veg not in selection:
                            chosen_veg = input(f"Please provide the full name of the product you'd like to purchase: {selection}\n")
                        
                        with open("order_list.csv", "a") as file:
                            f = csv.writer(file)
                            list1 = [f"{chosen_veg}", f"{quan[i]}", f"{variety1[chosen_veg]}"]
                            f.writerow(list1)
            
            with open("order_list.csv", "r") as file2:
                total_cost = 0
                for row in file2:
                    row = row.split(",")
                    total_cost += float(row[2])
                    order += "Product Name: " + row[0] + ", Quantity: " + row[1].strip(".0") + ", Unit Price: $" + row[2]
            
            order += "Total Cost: $" + str(total_cost)

            with open("order_list.csv", "w") as file3:
                pass
        
            return queryText, order
            
        else:
            return queryText, "I'm sorry I don't think I understand"
Example #12
0
def message_to_json(grpc_msg):
    """Wrap the grpc MessageToJson.  Returns a JSON string"""
    return MessageToJson(grpc_msg, including_default_value_fields=True)
Example #13
0
def getblockminingcompatible(height):
    stub = get_mining_stub()
    request = qrlmining_pb2.GetBlockMiningCompatibleReq(height=height)
    response = stub.GetBlockMiningCompatible(request=request, timeout=10)
    return MessageToJson(response)
Example #14
0
import HotMessage_pb2

PB_FILE_PATH = 'HotMessage.pb'
JSON_FILE_PATH = 'HotMessage.json'

message = HotMessage_pb2.Message()
message.msgType = 1
message.msg = str.encode('1011')
message.agree = True

# 以二进制形式保存
f = open(PB_FILE_PATH, "wb")
f.write(message.SerializeToString())
f.close()

# 读message
message = HotMessage_pb2.Message()
f = open(PB_FILE_PATH, "rb")
message.ParseFromString(f.read())
f.close()
from google.protobuf.json_format import MessageToJson  # 关键

json = MessageToJson(message)
print(json)

# 保存json
f = open(JSON_FILE_PATH, "w")
f.write(json)
f.close()
Example #15
0
 def update_run_info(self, run_uuid, run_status, end_time):
     """ Updates the metadata of the specified run. """
     req_body = MessageToJson(
         UpdateRun(run_uuid=run_uuid, status=run_status, end_time=end_time))
     response_proto = self._call_endpoint(UpdateRun, req_body)
     return RunInfo.from_proto(response_proto.run_info)
Example #16
0
 def to_json(self):
     # FIXME: Remove once we move completely to protobuf
     return MessageToJson(self._data, sort_keys=True)
Example #17
0
import example_pb2

PB_FILE_PATH = 'example.pb'
JSON_FILE_PATH = 'example.json'

event = example_pb2.Event()
event.message = 'Hello World!'
programming = example_pb2.Programming()
programming.language = 'C++'
event.language.append(programming)

# 以二进制形式保存
f = open(PB_FILE_PATH, "wb")
f.write(event.SerializeToString())
f.close()

# 读event
event = example_pb2.Event()
f = open(PB_FILE_PATH, "rb")
event.ParseFromString(f.read())
f.close()
from google.protobuf.json_format import MessageToJson  # 关键
json = MessageToJson(event)
print(json)

# 保存json
f = open(JSON_FILE_PATH, "w")
f.write(json)
f.close()
Example #18
0
import tensorflow as tf
from google.protobuf.json_format import MessageToJson

file = "../../../../models/research/object_detection/data/test.record"
fileNum = 1
for example in tf.python_io.tf_record_iterator(file):
    jsonMessage = MessageToJson(tf.train.Example.FromString(example))
    # with open("RESULTS/image_{}".format(fileNum),"w") as text_file:
    # print(jsonMessage,file=text_file)
    print(jsonMessage)
    fileNum += 1
Example #19
0
def function_call():
    outputfile = "../input/test_set_stage_location_type_function_call.sir"

    interval = serial_utils.make_interval(
        SIR.Interval.Start, SIR.Interval.End, 0, 0)

    fun_ast = serial_utils.make_ast(
        [
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("out"),
                serial_utils.make_literal_access_expr(
                    value="2.0", type=serial_utils.BuiltinType.Float),
                "=",
            ),
        ]
    )

    arg_field = serial_utils.make_field(
        "out",
        serial_utils.make_field_dimensions_unstructured(
            [SIR.LocationType.Value("Cell")], 1
        )
    )

    fun = serial_utils.make_stencil_function(
        name='f', asts=[fun_ast], intervals=[interval], arguments=[serial_utils.make_stencil_function_arg(arg_field)])

    body_ast = serial_utils.make_ast(
        [
            serial_utils.make_expr_stmt(expr=serial_utils.make_stencil_fun_call_expr(
                callee="f", arguments=[serial_utils.make_field_access_expr("out_cell")])),
        ]
    )

    vertical_region_stmt = serial_utils.make_vertical_region_decl_stmt(
        body_ast, interval, SIR.VerticalRegion.Forward
    )

    sir = serial_utils.make_sir(
        outputfile,
        SIR.GridType.Value("Unstructured"),
        [
            serial_utils.make_stencil(
                "generated",
                serial_utils.make_ast([vertical_region_stmt]),
                [
                    serial_utils.make_field(
                        "out_cell",
                        serial_utils.make_field_dimensions_unstructured(
                            [SIR.LocationType.Value("Cell")], 1
                        ),
                    ),
                ],
            ),
        ],
        functions=[fun]
    )

    f = open(outputfile, "w")
    f.write(MessageToJson(sir))
    f.close()
def print_data_as_json(data, type):
    json = MessageToJson(data)
    logging.info("{} json {}".format(type, json))
Example #21
0
 def to_json(self, obj):
     return MessageToJson(obj)
Example #22
0
def detect_text(img_bytes):
    client = vision.ImageAnnotatorClient()
    image_data = vision.types.Image(content=img_bytes)
    response = client.text_detection(image=image_data)
    return json.loads(MessageToJson(response))
Example #23
0
def pb_to_json(pb):
    """Converts arbitrary protobuf messages into JSON"""
    return MessageToJson(pb)
Example #24
0
def main(args: argparse.Namespace):
    stencil_name = "ICON_laplacian_stencil"
    gen_outputfile = f"{stencil_name}.cpp"
    sir_outputfile = f"{stencil_name}.sir"

    interval = serial_utils.make_interval(AST.Interval.Start, AST.Interval.End,
                                          0, 0)

    body_ast = serial_utils.make_ast([
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("rot_vec"),
            serial_utils.make_reduction_over_neighbor_expr(
                op="+",
                init=serial_utils.make_literal_access_expr(
                    "0.0", AST.BuiltinType.Double),
                rhs=serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("vec", [True, 0]),
                    "*",
                    serial_utils.make_field_access_expr("geofac_rot"),
                ),
                chain=[
                    AST.LocationType.Value("Vertex"),
                    AST.LocationType.Value("Edge")
                ],
            ),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("div_vec"),
            serial_utils.make_reduction_over_neighbor_expr(
                op="+",
                init=serial_utils.make_literal_access_expr(
                    "0.0", AST.BuiltinType.Double),
                rhs=serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("vec", [True, 0]),
                    "*",
                    serial_utils.make_field_access_expr("geofac_div"),
                ),
                chain=[
                    AST.LocationType.Value("Cell"),
                    AST.LocationType.Value("Edge")
                ],
            ),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("nabla2t1_vec"),
            serial_utils.make_reduction_over_neighbor_expr(
                op="+",
                init=serial_utils.make_literal_access_expr(
                    "0.0", AST.BuiltinType.Double),
                rhs=serial_utils.make_field_access_expr("rot_vec", [True, 0]),
                chain=[
                    AST.LocationType.Value("Edge"),
                    AST.LocationType.Value("Vertex")
                ],
                weights=[
                    serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double),
                    serial_utils.make_literal_access_expr(
                        "1.0", AST.BuiltinType.Double)
                ]),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("nabla2t1_vec"),
            serial_utils.make_binary_operator(
                serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("tangent_orientation"),
                    "*",
                    serial_utils.make_field_access_expr("nabla2t1_vec"),
                ),
                "/",
                serial_utils.make_field_access_expr("primal_edge_length"),
            ),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("nabla2t2_vec"),
            serial_utils.make_reduction_over_neighbor_expr(
                op="+",
                init=serial_utils.make_literal_access_expr(
                    "0.0", AST.BuiltinType.Double),
                rhs=serial_utils.make_field_access_expr("div_vec", [True, 0]),
                chain=[
                    AST.LocationType.Value("Edge"),
                    AST.LocationType.Value("Cell")
                ],
                weights=[
                    serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double),
                    serial_utils.make_literal_access_expr(
                        "1.0", AST.BuiltinType.Double)
                ]),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("nabla2t2_vec"),
            serial_utils.make_binary_operator(
                serial_utils.make_field_access_expr("nabla2t2_vec"),
                "/",
                serial_utils.make_field_access_expr("dual_edge_length"),
            ),
            "=",
        ),
        serial_utils.make_assignment_stmt(
            serial_utils.make_field_access_expr("nabla2_vec"),
            serial_utils.make_binary_operator(
                serial_utils.make_field_access_expr("nabla2t2_vec"),
                "-",
                serial_utils.make_field_access_expr("nabla2t1_vec"),
            ),
            "=",
        ),
    ])

    vertical_region_stmt = serial_utils.make_vertical_region_decl_stmt(
        body_ast, interval, AST.VerticalRegion.Forward)

    sir = serial_utils.make_sir(
        gen_outputfile,
        AST.GridType.Value("Unstructured"),
        [
            serial_utils.make_stencil(
                stencil_name,
                serial_utils.make_ast([vertical_region_stmt]),
                [
                    serial_utils.make_field(
                        "vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                    ),
                    serial_utils.make_field(
                        "div_vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Cell")], 1),
                    ),
                    serial_utils.make_field(
                        "rot_vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Vertex")], 1),
                    ),
                    serial_utils.make_field(
                        "nabla2t1_vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                        is_temporary=True),
                    serial_utils.make_field(
                        "nabla2t2_vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                        is_temporary=True),
                    serial_utils.make_field(
                        "nabla2_vec",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                    ),
                    serial_utils.make_field(
                        "primal_edge_length",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                    ),
                    serial_utils.make_field(
                        "dual_edge_length",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                    ),
                    serial_utils.make_field(
                        "tangent_orientation",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1),
                    ),
                    serial_utils.make_field(
                        "geofac_rot",
                        serial_utils.make_field_dimensions_unstructured([
                            AST.LocationType.Value("Vertex"),
                            AST.LocationType.Value("Edge")
                        ], 1),
                    ),
                    serial_utils.make_field(
                        "geofac_div",
                        serial_utils.make_field_dimensions_unstructured([
                            AST.LocationType.Value("Cell"),
                            AST.LocationType.Value("Edge")
                        ], 1),
                    ),
                ],
            ),
        ],
    )

    # print the SIR
    if args.verbose:
        print(MessageToJson(sir))

    # compile
    code = dawn4py.compile(sir,
                           groups=[],
                           backend=dawn4py.CodeGenBackend.CXXNaiveIco)

    # write to file
    print(f"Writing generated code to '{gen_outputfile}'")
    with open(gen_outputfile, "w") as f:
        f.write(code)
Example #25
0
 def _write_json(self, json_file, response):
     if json_file and response:
         with open(json_file, "w") as f:
             f.write(MessageToJson(response))
    def test_commit_w_bound_client(self):
        import json
        import datetime
        from google.protobuf.json_format import MessageToJson
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value
        from google.cloud._helpers import _datetime_to_rfc3339
        from google.cloud.logging_v2.entries import _GLOBAL_RESOURCE

        TEXT = "This is the entry text"
        STRUCT = {"message": TEXT, "weather": "partly cloudy"}
        message = Struct(fields={"foo": Value(bool_value=True)})
        IID1 = "IID1"
        IID2 = "IID2"
        IID3 = "IID3"
        TIMESTAMP1 = datetime.datetime(2016, 12, 31, 0, 0, 1, 999999)
        TIMESTAMP2 = datetime.datetime(2016, 12, 31, 0, 0, 2, 999999)
        TIMESTAMP3 = datetime.datetime(2016, 12, 31, 0, 0, 3, 999999)
        TRACE1 = "12345678-1234-5678-1234-567812345678"
        TRACE2 = "12345678-1234-5678-1234-567812345679"
        TRACE3 = "12345678-1234-5678-1234-567812345670"
        SPANID1 = "000000000000004a"
        SPANID2 = "000000000000004b"
        SPANID3 = "000000000000004c"
        ENTRIES = [
            {
                "textPayload": TEXT,
                "insertId": IID1,
                "timestamp": _datetime_to_rfc3339(TIMESTAMP1),
                "resource": _GLOBAL_RESOURCE._to_dict(),
                "trace": TRACE1,
                "spanId": SPANID1,
                "traceSampled": True,
            },
            {
                "jsonPayload": STRUCT,
                "insertId": IID2,
                "timestamp": _datetime_to_rfc3339(TIMESTAMP2),
                "resource": _GLOBAL_RESOURCE._to_dict(),
                "trace": TRACE2,
                "spanId": SPANID2,
                "traceSampled": False,
            },
            {
                "protoPayload": json.loads(MessageToJson(message)),
                "insertId": IID3,
                "timestamp": _datetime_to_rfc3339(TIMESTAMP3),
                "resource": _GLOBAL_RESOURCE._to_dict(),
                "trace": TRACE3,
                "spanId": SPANID3,
                "traceSampled": True,
            },
        ]
        client = _Client(project=self.PROJECT)
        api = client.logging_api = _DummyLoggingAPI()
        logger = _Logger()
        batch = self._make_one(logger, client=client)

        batch.log_text(
            TEXT,
            insert_id=IID1,
            timestamp=TIMESTAMP1,
            trace=TRACE1,
            span_id=SPANID1,
            trace_sampled=True,
        )
        batch.log_struct(
            STRUCT,
            insert_id=IID2,
            timestamp=TIMESTAMP2,
            trace=TRACE2,
            span_id=SPANID2,
            trace_sampled=False,
        )
        batch.log_proto(
            message,
            insert_id=IID3,
            timestamp=TIMESTAMP3,
            trace=TRACE3,
            span_id=SPANID3,
            trace_sampled=True,
        )
        batch.commit()

        self.assertEqual(list(batch.entries), [])
        self.assertEqual(api._write_entries_called_with,
                         (ENTRIES, logger.full_name, None, None))
Example #27
0
 def serialize(message):
     return json.loads(MessageToJson(message,
                                     preserving_proto_field_name=True,
                                     float_precision=20))
    def test_log_proto_w_explicit(self):
        import json
        import datetime
        from google.protobuf.json_format import MessageToJson
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value
        from google.cloud.logging import Resource

        message = Struct(fields={"foo": Value(bool_value=True)})
        ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
        DEFAULT_LABELS = {"foo": "spam"}
        LABELS = {"foo": "bar", "baz": "qux"}
        IID = "IID"
        SEVERITY = "CRITICAL"
        METHOD = "POST"
        URI = "https://api.example.com/endpoint"
        STATUS = "500"
        TRACE = "12345678-1234-5678-1234-567812345678"
        SPANID = "000000000000004a"
        REQUEST = {
            "requestMethod": METHOD,
            "requestUrl": URI,
            "status": STATUS
        }
        TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999)
        RESOURCE = Resource(type="gae_app",
                            labels={
                                "module_id": "default",
                                "version_id": "test"
                            })
        ENTRIES = [{
            "logName": ALT_LOG_NAME,
            "protoPayload": json.loads(MessageToJson(message)),
            "labels": LABELS,
            "insertId": IID,
            "severity": SEVERITY,
            "httpRequest": REQUEST,
            "timestamp": "2016-12-31T00:01:02.999999Z",
            "resource": RESOURCE._to_dict(),
            "trace": TRACE,
            "spanId": SPANID,
            "traceSampled": True,
        }]
        client1 = _Client(self.PROJECT)
        client2 = _Client(self.PROJECT)
        api = client2.logging_api = _DummyLoggingAPI()
        logger = self._make_one(self.LOGGER_NAME,
                                client=client1,
                                labels=DEFAULT_LABELS)

        logger.log_proto(
            message,
            log_name=ALT_LOG_NAME,
            client=client2,
            labels=LABELS,
            insert_id=IID,
            severity=SEVERITY,
            http_request=REQUEST,
            timestamp=TIMESTAMP,
            resource=RESOURCE,
            trace=TRACE,
            span_id=SPANID,
            trace_sampled=True,
        )

        self.assertEqual(api._write_entries_called_with,
                         (ENTRIES, None, None, None))
def main(args: argparse.Namespace):
    stencil_name = "ICON_laplacian_diamond_stencil"
    gen_outputfile = f"{stencil_name}.cpp"
    sir_outputfile = f"{stencil_name}.sir"

    interval = serial_utils.make_interval(
        AST.Interval.Start, AST.Interval.End, 0, 0)

    body_ast = serial_utils.make_ast(
        [
            # fill sparse dimension vn vert using the loop concept
            serial_utils.make_loop_stmt(
                [serial_utils.make_assignment_stmt(
                    serial_utils.make_field_access_expr("vn_vert"),
                    serial_utils.make_binary_operator(
                        serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "u_vert", [True, 0]), "*", serial_utils.make_field_access_expr("primal_normal_x", [True, 0])),
                        "+", serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "v_vert", [True, 0]), "*", serial_utils.make_field_access_expr("primal_normal_y", [True, 0])),
                    ),
                    "=")],
                [AST.LocationType.Value(
                    "Edge"), AST.LocationType.Value("Cell"), AST.LocationType.Value("Vertex")]
            ),
            # dvt_tang for smagorinsky
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("dvt_tang"),
                serial_utils.make_reduction_over_neighbor_expr(
                    op="+",
                    init=serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double),
                    rhs=serial_utils.make_binary_operator(
                        serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "u_vert", [True, 0]), "*", serial_utils.make_field_access_expr("dual_normal_x", [True, 0])),
                        "+", serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "v_vert", [True, 0]), "*", serial_utils.make_field_access_expr("dual_normal_y", [True, 0])),
                    ),
                    chain=[AST.LocationType.Value("Edge"), AST.LocationType.Value(
                        "Cell"), AST.LocationType.Value("Vertex")],
                    weights=[serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double)]

                ),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("dvt_tang"), serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("dvt_tang"), "*", serial_utils.make_field_access_expr("tangent_orientation")), "="),
            # dvt_norm for smagorinsky
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("dvt_norm"),
                serial_utils.make_reduction_over_neighbor_expr(
                    op="+",
                    init=serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double),
                    rhs=serial_utils.make_binary_operator(
                        serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "u_vert", [True, 0]), "*", serial_utils.make_field_access_expr("dual_normal_x", [True, 0])),
                        "+", serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                            "v_vert", [True, 0]), "*", serial_utils.make_field_access_expr("dual_normal_y", [True, 0])),
                    ),
                    chain=[AST.LocationType.Value("Edge"), AST.LocationType.Value(
                        "Cell"), AST.LocationType.Value("Vertex")],
                    weights=[serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "1.0", AST.BuiltinType.Double)]

                ),
                "=",
            ),
            # compute smagorinsky
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("kh_smag_1"),
                serial_utils.make_reduction_over_neighbor_expr(
                    op="+",
                    init=serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double),
                    rhs=serial_utils.make_field_access_expr("vn_vert"),
                    chain=[AST.LocationType.Value("Edge"), AST.LocationType.Value(
                        "Cell"), AST.LocationType.Value("Vertex")],
                    weights=[serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double)]

                ),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("kh_smag_1"),
                serial_utils.make_binary_operator(
                    serial_utils.make_binary_operator(
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr("kh_smag_1"),
                            "*",
                            serial_utils.make_field_access_expr("tangent_orientation")),
                        "*",
                        serial_utils.make_field_access_expr("inv_primal_edge_length")), "+",
                    serial_utils.make_binary_operator(
                        serial_utils.make_field_access_expr("dvt_norm"),
                        "*",
                        serial_utils.make_field_access_expr("inv_vert_vert_length"))), "="),
            serial_utils.make_assignment_stmt(serial_utils.make_field_access_expr("kh_smag_1"),
                                           serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                                               "kh_smag_1"), "*", serial_utils.make_field_access_expr("kh_smag_1"))),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("kh_smag_2"),
                serial_utils.make_reduction_over_neighbor_expr(
                    op="+",
                    init=serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double),
                    rhs=serial_utils.make_field_access_expr("vn_vert"),
                    chain=[AST.LocationType.Value("Edge"), AST.LocationType.Value(
                        "Cell"), AST.LocationType.Value("Vertex")],
                    weights=[serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        "-1.0", AST.BuiltinType.Double), serial_utils.make_literal_access_expr(
                        " 1.0", AST.BuiltinType.Double)]

                ),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("kh_smag_2"),
                serial_utils.make_binary_operator(
                    serial_utils.make_binary_operator(
                        serial_utils.make_field_access_expr("kh_smag_2"),
                        "*",
                        serial_utils.make_field_access_expr("inv_vert_vert_length")),
                    "+",
                    serial_utils.make_binary_operator(
                        serial_utils.make_field_access_expr("dvt_tang"),
                        "*",
                        serial_utils.make_field_access_expr("inv_primal_edge_length"))), "="),
            serial_utils.make_assignment_stmt(serial_utils.make_field_access_expr("kh_smag_2"),
                                           serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                                               "kh_smag_2"), "*", serial_utils.make_field_access_expr("kh_smag_2"))),
            # currently not able to forward a sqrt, so this is technically kh_smag**2
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("kh_smag"),
                serial_utils.make_binary_operator(serial_utils.make_field_access_expr("diff_multfac_smag"), "*",
                                               serial_utils.make_fun_call_expr("math::sqrt",
                                                                            [serial_utils.make_binary_operator(serial_utils.make_field_access_expr(
                                                                                "kh_smag_1"), "+", serial_utils.make_field_access_expr("kh_smag_2"))])),
                "="),
            # compute nabla2 using the diamond reduction
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("nabla2"),
                serial_utils.make_reduction_over_neighbor_expr(
                    op="+",
                    init=serial_utils.make_literal_access_expr(
                        "0.0", AST.BuiltinType.Double),
                    rhs=serial_utils.make_binary_operator(serial_utils.make_literal_access_expr(
                        "4.0", AST.BuiltinType.Double), "*", serial_utils.make_field_access_expr("vn_vert")),
                    chain=[AST.LocationType.Value("Edge"), AST.LocationType.Value(
                        "Cell"), AST.LocationType.Value("Vertex")],
                    weights=[
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "inv_primal_edge_length"),
                            '*',
                            serial_utils.make_field_access_expr(
                                "inv_primal_edge_length")),
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "inv_primal_edge_length"),
                            '*',
                            serial_utils.make_field_access_expr(
                                "inv_primal_edge_length")),
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "inv_vert_vert_length"),
                            '*',
                            serial_utils.make_field_access_expr(
                                "inv_vert_vert_length")),
                        serial_utils.make_binary_operator(
                            serial_utils.make_field_access_expr(
                                "inv_vert_vert_length"),
                            '*',
                            serial_utils.make_field_access_expr(
                                "inv_vert_vert_length")),
                    ]
                ),
                "=",
            ),
            serial_utils.make_assignment_stmt(
                serial_utils.make_field_access_expr("nabla2"),
                serial_utils.make_binary_operator(
                    serial_utils.make_field_access_expr("nabla2"),
                    "-",
                    serial_utils.make_binary_operator(
                        serial_utils.make_binary_operator(serial_utils.make_binary_operator(serial_utils.make_literal_access_expr(
                            "8.0", AST.BuiltinType.Double), "*", serial_utils.make_field_access_expr("vn")), "*",
                            serial_utils.make_binary_operator(
                                serial_utils.make_field_access_expr(
                                    "inv_primal_edge_length"),
                                "*",
                                serial_utils.make_field_access_expr(
                                    "inv_primal_edge_length"))),
                        "+",
                        serial_utils.make_binary_operator(serial_utils.make_binary_operator(serial_utils.make_literal_access_expr(
                            "8.0", AST.BuiltinType.Double), "*", serial_utils.make_field_access_expr("vn")), "*",
                            serial_utils.make_binary_operator(
                                serial_utils.make_field_access_expr(
                                    "inv_vert_vert_length"),
                                "*",
                                serial_utils.make_field_access_expr(
                                    "inv_vert_vert_length"))))),
                "=")
        ]
    )

    vertical_region_stmt = serial_utils.make_vertical_region_decl_stmt(
        body_ast, interval, AST.VerticalRegion.Forward
    )

    sir = serial_utils.make_sir(
        gen_outputfile,
        AST.GridType.Value("Unstructured"),
        [
            serial_utils.make_stencil(
                stencil_name,
                serial_utils.make_ast([vertical_region_stmt]),
                [
                    serial_utils.make_field(
                        "diff_multfac_smag",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value(
                                "Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "tangent_orientation",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "inv_primal_edge_length",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "inv_vert_vert_length",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "u_vert",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "v_vert",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "primal_normal_x",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge"), AST.LocationType.Value(
                                "Cell"), AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "primal_normal_y",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge"), AST.LocationType.Value(
                                "Cell"), AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "dual_normal_x",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge"), AST.LocationType.Value(
                                "Cell"), AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "dual_normal_y",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge"), AST.LocationType.Value(
                                "Cell"), AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "vn_vert",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge"), AST.LocationType.Value(
                                "Cell"), AST.LocationType.Value("Vertex")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "vn",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "dvt_tang",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "dvt_norm",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "kh_smag_1",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "kh_smag_2",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "kh_smag",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                    serial_utils.make_field(
                        "nabla2",
                        serial_utils.make_field_dimensions_unstructured(
                            [AST.LocationType.Value("Edge")], 1
                        ),
                    ),
                ],
            ),
        ],
    )

    # print the SIR       
    if args.verbose:
        print(MessageToJson(sir))

    # compile
    code = dawn4py.compile(sir, backend=dawn4py.CodeGenBackend.CXXNaiveIco)

    # write to file
    print(f"Writing generated code to '{gen_outputfile}'")
    with open(gen_outputfile, "w") as f:
        f.write(code)
Example #30
0
    def _make_entry_resource(self,
                             text=None,
                             info=None,
                             message=None,
                             labels=None,
                             insert_id=None,
                             severity=None,
                             http_request=None,
                             timestamp=None):
        """Return a log entry resource of the appropriate type.

        Helper for :meth:`log_text`, :meth:`log_struct`, and :meth:`log_proto`.

        Only one of ``text``, ``info``, or ``message`` should be passed.

        :type text: str
        :param text: (Optional) text payload

        :type info: dict
        :param info: (Optional) struct payload

        :type message: Protobuf message or :class:`NoneType`
        :param message: protobuf payload

        :type labels: dict
        :param labels: (Optional) labels passed in to calling method.

        :type insert_id: str
        :param insert_id: (optional) unique ID for log entry.

        :type severity: str
        :param severity: (optional) severity of event being logged.

        :type http_request: dict
        :param http_request: (optional) info about HTTP request associated with
                             the entry

        :type timestamp: :class:`datetime.datetime`
        :param timestamp: (optional) timestamp of event being logged.

        :rtype: dict
        :returns: The JSON resource created.
        """
        resource = {
            'logName': self.full_name,
            'resource': {
                'type': 'global'
            },
        }

        if text is not None:
            resource['textPayload'] = text

        if info is not None:
            resource['jsonPayload'] = info

        if message is not None:
            as_json_str = MessageToJson(message)
            as_json = json.loads(as_json_str)
            resource['protoPayload'] = as_json

        if labels is None:
            labels = self.labels

        if labels is not None:
            resource['labels'] = labels

        if insert_id is not None:
            resource['insertId'] = insert_id

        if severity is not None:
            resource['severity'] = severity

        if http_request is not None:
            resource['httpRequest'] = http_request

        if timestamp is not None:
            resource['timestamp'] = _datetime_to_rfc3339(timestamp)

        return resource