Exemplo n.º 1
0
def nest_get_simulation_time_info():  # noqa: E501
    """Retrieves simulation time information (stepSize, begin, current, end).

     # noqa: E501


    :rtype: SimulationTimeInfo
    """
    if len(simulation_nodes.nest_simulation_nodes) != 0:
        current_time = float("inf")
        begin = 0
        end = 0
        step_size = 0
        for node in simulation_nodes.nest_simulation_nodes:
            try:
                response = requests.get(node + "/simulationTimeInfo").json()
                current_time = min(current_time, response["current"])
                begin = max(begin, response["begin"])
                end = max(end, response["end"])
                step_size = response["stepSize"]
            except:
                error = Error(code="SimulationNotRunning",
                              message="No simulation running at the moment")
                response = ErrorResponse(error)
                return response, 422
        time_info = SimulationTimeInfo(current=current_time,
                                       begin=begin,
                                       end=end,
                                       step_size=step_size)
        return time_info, 200
    else:
        error = Error(code="SimulationNotRunning",
                      message="No simulation nodes available")
        response = ErrorResponse(error)
        return response, 500
Exemplo n.º 2
0
def nest_get_nodes_by_multimeter_id(multimeter_id):
    mult_info = nest_get_multimeters()

    multimeter_exists = False
    for mult in mult_info:
        if mult["multimeterId"] == multimeter_id:
            mult_node_ids = mult['nodeIds']
            return True, mult_node_ids
    if not multimeter_exists:
        error = Error(code="InvalidMultimeterRequest",
                      message="Given multimeter does not exist")
        error_response = ErrorResponse(error)
        return False, error_response
Exemplo n.º 3
0
def nest_get_node_by_id(node_id):  # noqa: E501
    """Retrieves the properties of the specified node.

     # noqa: E501

    :param node_id: The ID of the queried node.
    :type node_id: int

    :rtype: NestNodeProperties
    """
    nodes = nest_get_nodes()
    for node in nodes:
        if node['nodeId'] is node_id:
            return node
    error = Error(code ="invalidNodeID", message = "Node ID doesnt exist.")
    response = ErrorResponse(error)
    return response, 500
Exemplo n.º 4
0
def nest_get_multimeter_by_id(multimeter_id):  # noqa: E501
    """Retreives the available multimeters and their properties.

     # noqa: E501

    :param multimeter_id: The identifier of the multimeter.
    :type multimeter_id: int

    :rtype: MultimeterInfo
    """
    # Multimeterendpoint doesnt exist in nest module yet
    multimeters = nest_get_multimeters()
    for multimeter in multimeters:
        if multimeter['multimeterId'] is multimeter_id:
            return multimeter
    error = Error(code ="invalidMultimeterID", message = "Multimeter ID doesnt exist.")
    response = ErrorResponse(error)
    return response, 500
Exemplo n.º 5
0
def get_version():  # noqa: E501
    """Returns the deployed insite and API versions.

     # noqa: E501


    :rtype: Version
    """
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, '../../version.txt')
    print(filename)
    with open(filename, "r") as json_file:
        versions = json.load(json_file)
        api_version = versions["api_version"]
        insite_version = versions["insite_version"]
        version = Version(api=api_version, insite=insite_version)
        return version, 200
    error = Error(code="NoVersion", message="Unable to read version from file")
    error_response = ErrorResponse(error)
    return error_response, 500
Exemplo n.º 6
0
def nest_get_nodes_by_node_collection(node_collection_id):  # noqa: E501
    """Retrieves the list of all node within the node collection.

     # noqa: E501

    :param node_collection_id: The identifier of the node collection
    :type node_collection_id: int

    :rtype: List[NestNodeProperties]
    """
    all_nodes = nest_get_nodes()
    nodes = []
    for node in all_nodes:
        if node['nodeCollectionId'] is node_collection_id:
            nodes.append(node)
    if len(nodes) is 0:
        error = Error(code ="invalidNodeCollectionID", message = "Node Collection ID doesnt exist.")
        response = ErrorResponse(error)
        return response, 500
    return nodes
Exemplo n.º 7
0
def nest_get_node_ids_by_node_collection(node_collection_id):  # noqa: E501
    """Retrieves the list of all node ids within the node collection.

     # noqa: E501

    :param node_collection_id: The identifier of the node collection
    :type node_collection_id: int

    :rtype: List[int]
    """
    nodes = nest_get_nodes()
    ids = []
    for node in nodes:
        if node['nodeCollectionId'] is node_collection_id:
            ids.append(node['nodeId'])
    if len(ids) is 0:
        error = Error(code ="invalidNodeCollectionID", message = "Node Collection ID doesnt exist.")
        response = ErrorResponse(error)
        return response, 500
    ids.sort()
    return ids
Exemplo n.º 8
0
def nest_get_multimeter_measurements(multimeter_id, attribute_name, from_time=None, to_time=None, node_ids=None, skip=None, top=None):  # noqa: E501
    """Retrieves the measurements for a multimeter, attribute and node IDs (optional).

     # noqa: E501

    :param multimeter_id: The multimeter to query
    :type multimeter_id: int
    :param attribute_name: The attribute to query (e.g., 'V_m' for the membrane potential)
    :type attribute_name: str
    :param from_time: The start time (including) to be queried.
    :type from_time: float
    :param to_time: The end time (excluding) to be queried.
    :type to_time: float
    :param node_ids: A list of node IDs queried for attribute data.
    :type node_ids: List[int]
    :param skip: The offset into the result.
    :type skip: int
    :param top: The maximum number of entries to be returned.
    :type top: int

    :rtype: MultimeterMeasurement
    """
    #TODO Cache this
    mult_info = nest_get_multimeters()

    mult_node_ids = []
    multimeter_exists = False
    for mult in mult_info:
        if mult["multimeterId"] == multimeter_id:
            multimeter_exists = True
            if attribute_name not in mult['attributes']:
                error = Error(code ="InvalidMultimeterRequest", message = "Given multimeter does not measure given attribute")
                error_response = ErrorResponse(error)
                return error_response, 400
            mult_node_ids = mult['nodeIds']
            break
    if not multimeter_exists:
        error = Error(code ="InvalidMultimeterRequest", message = "Given multimeter does not exist")
        error_response = ErrorResponse(error)
        return error_response, 400

    if node_ids == None:
        node_ids = mult_node_ids
    else:
        for node_id in node_ids:
            if node_id not in mult_node_ids:
                error = Error(code ="InvalidMultimeterRequest", message = "Node "+str(node_id)+" is not monitored by given Multimeter")
                error_response = ErrorResponse(error)
                return error_response, 400

    init = True
    sim_times = []
    measurement = MultimeterMeasurement([], [], [])
    for node in simulation_nodes.nest_simulation_nodes:

        if node_ids is not None:
            node_id_param = ",".join(map(str, node_ids))        
        else:
            node_id_param = None

        response = requests.get(
            node+"/multimeter_measurement", params={"multimeterId": multimeter_id, 
            "attribute": attribute_name, "fromTime": from_time,
            "toTime": to_time, "nodeIds": node_id_param}).json()
        if init:
            sim_times = response["simulationTimes"]
            measurement = MultimeterMeasurement(
                sim_times, node_ids, [None for x in range(0, (len(sim_times)*len(node_ids)))])
            init = False
        for x in range(len(response['nodeIds'])):
            node_id = response['nodeIds'][x]
            index = measurement.node_ids.index(node_id)
            index_offset = index * len(sim_times)
            for y in range(len(sim_times)):
                measurement.values[index_offset + y] = response['values'][x*len(sim_times)+y]

    # offset and limit
    if (skip is None):
        skip = 0
    if (top is None or (top + skip) > len(measurement.node_ids)):
        top = len(measurement.node_ids) - skip
    measurement.node_ids = measurement.node_ids[skip:skip+top]
    measurement.values = measurement.values[skip *
                                            len(sim_times):(skip+top)*len(sim_times)]

    return measurement
Exemplo n.º 9
0
def nest_get_multimeter_measurements(multimeter_id,
                                     attribute_name,
                                     from_time=None,
                                     to_time=None,
                                     node_ids=None,
                                     skip=0,
                                     top=0):  # noqa: E501
    """Retrieves the measurements for a multimeter, attribute and node IDs (optional).

     # noqa: E501

    :param multimeter_id: The multimeter to query
    :type multimeter_id: int
    :param attribute_name: The attribute to query (e.g., 'V_m' for the membrane potential)
    :type attribute_name: str
    :param from_time: The start time (including) to be queried.
    :type from_time: float
    :param to_time: The end time (excluding) to be queried.
    :type to_time: float
    :param node_ids: A list of node IDs queried for attribute data.
    :type node_ids: List[int]
    :param skip: The offset into the result.
    :type skip: int
    :param top: The maximum number of entries to be returned.
    :type top: int

    :rtype: MultimeterMeasurement
    """
    #TODO Cache this
    mult_found, mult_nodes = nest_get_nodes_by_multimeter_id(multimeter_id)

    node_id_params = node_ids
    if node_ids == None and mult_found:
        node_ids = mult_nodes
    else:
        for node_id in node_ids:
            if node_id not in mult_nodes:
                error = Error(code="InvalidMultimeterRequest",
                              message="Node " + str(node_id) +
                              " is not monitored by given Multimeter")
                error_response = ErrorResponse(error)
                return error_response, 400

    init = True
    sim_times = []
    if simulation_nodes.nest_simulation_nodes == None:
        return

    for node in simulation_nodes.nest_simulation_nodes:

        if node_id_params is not None:
            node_id_param = ",".join(map(str, node_id_params))
        else:
            node_id_param = None

        response = requests.get(node + "/multimeter_measurement",
                                params={
                                    "multimeterId": multimeter_id,
                                    "attribute": attribute_name,
                                    "fromTime": from_time,
                                    "toTime": to_time,
                                    "nodeIds": node_id_param
                                })
        response = orjson.loads(response.content)

        if init:
            sim_times = response["simulationTimes"]
            multimeter_values = [
                None for _ in range(0, (len(sim_times) * len(node_ids)))
            ]
            init = False
        for x in range(len(response['nodeIds'])):
            node_id = response['nodeIds'][x]
            index = node_ids.index(node_id)
            index_offset = index * len(sim_times)
            for y in range(len(sim_times)):
                multimeter_values[index_offset +
                                  y] = response['values'][x * len(sim_times) +
                                                          y]

    # offset and limit
    if skip > 0 or top > 0:
        print("slicing")
        top = len(node_ids) - skip
        node_ids = node_ids[skip:skip + top]
        multimeter_values = multimeter_values[skip *
                                              len(sim_times):(skip + top) *
                                              len(sim_times)]

    json_string = orjson.dumps({
        "simulationTimes": sim_times,
        "nodeIds": node_ids,
        "values": multimeter_values
    })
    return ConnexionResponse(status_code=200,
                             content_type='application/json',
                             mimetype='text/plain',
                             body=json_string)