Ejemplo n.º 1
0
 def test_add_network_attribute_no_type(self):
     builder = NiceCXBuilder()
     builder.add_network_attribute(name='foo', values='somevalue')
     net = builder.get_nice_cx()
     self.assertEqual(['foo'], list(net.get_network_attribute_names()))
     self.assertEqual({
         'n': 'foo',
         'v': 'somevalue'
     }, net.get_network_attribute('foo'))
Ejemplo n.º 2
0
 def test_add_network_attribute_no_with_type(self):
     builder = NiceCXBuilder()
     builder.add_network_attribute(name='foo', values='1.0', type='double')
     net = builder.get_nice_cx()
     self.assertEqual(['foo'], list(net.get_network_attribute_names()))
     self.assertEqual({
         'd': 'double',
         'n': 'foo',
         'v': '1.0'
     }, net.get_network_attribute('foo'))
Ejemplo n.º 3
0
def convert_pandas_to_nice_cx_with_load_plan(pandas_dataframe,
                                             load_plan,
                                             max_rows=None,
                                             name=None,
                                             description=None,
                                             network_attributes=None,
                                             provenance=None):

    # open the schema first
    here = path.abspath(path.dirname(__file__))
    with open(path.join(here, 'loading_plan_schema.json')) as json_file:
        plan_schema = json.load(json_file)

    jsonschema.validate(load_plan, plan_schema)

    node_lookup = {}
    nice_cx_builder = NiceCXBuilder()
    row_count = 0
    t1 = int(time.time() * 1000)

    #Add context if they are defined
    context = load_plan.get('context')
    if context:
        if network_attributes is None:
            network_attributes = []
        network_attributes.append({"n": "@context", "v": json.dumps(context)})

    total_row_count = pandas_dataframe.shape
    if len(total_row_count) > 1:
        total_row_count = str(total_row_count[0])
    for index, row in pandas_dataframe.iterrows():
        # As each row is processed, self.G_nx is updated
        process_row(nice_cx_builder, load_plan, row, node_lookup)
        row_count = row_count + 1
        if max_rows and row_count > max_rows + 2:
            break

        if row_count % 2500 == 0:
            logger.info('processing %s out of %s edges' %
                        (str(row_count), total_row_count))

    if network_attributes:
        for attribute in network_attributes:
            if attribute.get("n") == "name":
                nice_cx_builder.set_name(attribute.get("v"))
            else:
                nice_cx_builder.add_network_attribute(
                    name=attribute.get('n'),
                    values=attribute.get('v'),
                    type=attribute.get('d'))

    tsv_data_event = {
        "inputs": None,
        "startedAtTime": t1,
        "endedAtTime": int(time.time() * 1000),
        "eventType": "TSV network generation",
        "properties": [{
            "name": "TSV loader version",
            "value": version
        }]
    }

    # name and description take precedence over any prior values
    if name:
        nice_cx_builder.set_name(name)
    if description:
        nice_cx_builder.add_network_attribute(name='description',
                                              values=description)

    return nice_cx_builder.get_nice_cx()