This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Nexosis.Api.Sessions

Functions for interacting with the /sessions endpoint.

GET

Retrieve information about Sessions.

get : ClientConfig -> Int -> Int -> SortParameters -> Http.Request SessionList

GET a listing of Sessions, wth page limits and sorting information.

getForDataset : ClientConfig -> DataSetName -> Http.Request SessionList

GET a list of Session filtered by the DataSetName.

getOne : ClientConfig -> String -> Http.Request SessionData

GET a single Session by id.

POST

Start running a new Session. The type of Session will depend on the machine leaning problem you are trying to solve.

For some higher level information about Sessions, refer to the documentation.

postModel : ClientConfig -> ModelSessionRequest -> Http.Request SessionData

POST a ModelSessionRequest

postForecast : ClientConfig -> ForecastSessionRequest -> Http.Request SessionData

POST a ForecastSessionRequest

postImpact : ClientConfig -> ImpactSessionRequest -> Http.Request SessionData

POST a ImpactSessionRequest

Request Types

These types are used to specify parameters about a Session.

type alias ModelSessionRequest = { name : Maybe String , dataSourceName : DataSetName , columns : List ColumnMetadata , missingValues : List String , predictionDomain : PredictionDomain , balance : Maybe Bool , containsAnomalies : Maybe Bool }

Details used to build a Model. Provided ColumnMetadata will override the metadata of a DataSet. balance is only used for Classification, and containsAnomalies is only used for AnomalyDetection.

type alias ForecastSessionRequest = { name : Maybe String , dataSourceName : DataSetName , columns : List ColumnMetadata , missingValues : List String , dates : { startDate : ZonedDateTime , endDate : ZonedDateTime } , resultInterval : ResultInterval }

Details used to Forecast using time series algorithms. startDate and endDate is the date range that should be forecasted. resultInterval is the time interval at which the forecast should be made.

type alias ImpactSessionRequest = { name : Maybe String , dataSourceName : DataSetName , columns : List ColumnMetadata , missingValues : List String , dates : { startDate : ZonedDateTime , endDate : ZonedDateTime } , eventName : String , resultInterval : ResultInterval }

Details used to determine Impact of an event using time series algorithms. startDate and endDate is the date range that should be measured for Impact. resultInterval is the time interval at which the Impact should be made. eventName is a description name to help identify what impact is being measured in the date range.

Results

Once a Session is complete, results generated by the Session will be available. Different kinds of sessions will produce different result types.

results : ClientConfig -> String -> Int -> Int -> Http.Request SessionResults

GET results of a completed Session.

resultsCsv : ClientConfig -> String -> Http.Request String

GET the results as a CSV file, in a String.

getConfusionMatrix : ClientConfig -> String -> Int -> Int -> Http.Request ConfusionMatrix

GET a Confusion Matrix generated from the results of a Classification Session.

getDistanceMetrics : ClientConfig -> String -> Int -> Int -> Http.Request DistanceMetrics

GET the Mahalanobis Distances calculated by an Anomaly Detection Session.

getDistanceMetricsCsv : ClientConfig -> String -> Int -> Int -> Http.Request String

GET the Mahalanobis Distances calculated by an Anomaly Detection Session as a CSV String.

DELETE

delete : ClientConfig -> String -> Http.Request ()

DELETE a single Session.

module Nexosis.Api.Sessions exposing (ForecastSessionRequest, ImpactSessionRequest, ModelSessionRequest, delete, get, getConfusionMatrix, getDistanceMetrics, getDistanceMetricsCsv, getForDataset, getOne, postForecast, postImpact, postModel, results, resultsCsv)

{-| Functions for interacting with the `/sessions` endpoint.


## GET

Retrieve information about `Sessions`.

@docs get, getForDataset, getOne


## POST

Start running a new `Session`. The type of `Session` will depend on the machine leaning problem you are trying to solve.

For some higher level information about Sessions, refer to [the documentation](https://docs.nexosis.com/guides/sessions).

@docs postModel, postForecast, postImpact


## Request Types

These types are used to specify parameters about a `Session`.

@docs ModelSessionRequest, ForecastSessionRequest, ImpactSessionRequest


## Results

Once a `Session` is complete, results generated by the Session will be available. Different kinds of sessions will produce different result types.

@docs results, resultsCsv, getConfusionMatrix, getDistanceMetrics, getDistanceMetricsCsv


## DELETE

@docs delete

-}

import Http
import HttpBuilder
import Json.Encode as Encode
import Nexosis exposing (ClientConfig, getBaseUrl)
import Nexosis.Decoders.ConfusionMatrix exposing (decodeConfusionMatrix)
import Nexosis.Decoders.DistanceMetric exposing (decodeDistanceMetrics)
import Nexosis.Decoders.Session exposing (decodeSession, decodeSessionList, decodeSessionResults)
import Nexosis.Encoders.Columns exposing (encodeColumnMetadataList)
import Nexosis.Types.Columns exposing (ColumnMetadata)
import Nexosis.Types.ConfusionMatrix exposing (ConfusionMatrix)
import Nexosis.Types.DataSet exposing (DataSetName, dataSetNameToString)
import Nexosis.Types.DistanceMetric exposing (DistanceMetrics)
import Nexosis.Types.PredictionDomain exposing (PredictionDomain)
import Nexosis.Types.Session exposing (ResultInterval, SessionData, SessionList, SessionResults)
import Nexosis.Types.SortParameters exposing (SortParameters)
import NexosisHelpers exposing (addHeaders, pageParams, sortParams)
import Time.ZonedDateTime exposing (ZonedDateTime, toISO8601)


{-| GET a listing of `Sessions`, wth page limits and sorting information.
-}
get : ClientConfig -> Int -> Int -> SortParameters -> Http.Request SessionList
get config page pageSize sorting =
    let
        params =
            pageParams page pageSize
                ++ sortParams sorting
    in
    (getBaseUrl config ++ "/sessions")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeSessionList
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET results of a completed `Session`.
-}
results : ClientConfig -> String -> Int -> Int -> Http.Request SessionResults
results config sessionId page pageSize =
    let
        params =
            pageParams page pageSize
    in
    (getBaseUrl config ++ "/sessions/" ++ sessionId ++ "/results")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeSessionResults
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET the results as a CSV file, in a `String`.
-}
resultsCsv : ClientConfig -> String -> Http.Request String
resultsCsv config sessionId =
    (getBaseUrl config ++ "/sessions/" ++ sessionId ++ "/results")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectString
        |> HttpBuilder.withHeader "Accept" "text/csv"
        |> HttpBuilder.withQueryParams [ ( "pageSize", "1000" ) ]
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET a Confusion Matrix generated from the results of a `Classification` `Session`.
-}
getConfusionMatrix : ClientConfig -> String -> Int -> Int -> Http.Request ConfusionMatrix
getConfusionMatrix config sessionId page pageSize =
    let
        params =
            pageParams page pageSize
    in
    (getBaseUrl config ++ "/sessions/" ++ sessionId ++ "/results/confusionmatrix")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeConfusionMatrix
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET a list of `Session` filtered by the `DataSetName`.
-}
getForDataset : ClientConfig -> DataSetName -> Http.Request SessionList
getForDataset config dataSetName =
    let
        params =
            [ ( "dataSetName", dataSetNameToString dataSetName ) ]
    in
    (getBaseUrl config ++ "/sessions")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeSessionList
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| DELETE a single `Session`.
-}
delete : ClientConfig -> String -> Http.Request ()
delete config sessionId =
    (getBaseUrl config ++ "/sessions/" ++ sessionId)
        |> HttpBuilder.delete
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET a single `Session` by id.
-}
getOne : ClientConfig -> String -> Http.Request SessionData
getOne config sessionId =
    (getBaseUrl config ++ "/sessions/" ++ sessionId)
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeSession
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| Details used to build a `Model`. Provided `ColumnMetadata` will override the metadata of a `DataSet`.
`balance` is only used for `Classification`, and `containsAnomalies` is only used for `AnomalyDetection`.
-}
type alias ModelSessionRequest =
    { name : Maybe String
    , dataSourceName : DataSetName
    , columns : List ColumnMetadata
    , missingValues : List String
    , predictionDomain : PredictionDomain
    , balance : Maybe Bool
    , containsAnomalies : Maybe Bool
    }


{-| POST a `ModelSessionRequest`
-}
postModel : ClientConfig -> ModelSessionRequest -> Http.Request SessionData
postModel config sessionRequest =
    let
        requestBody =
            encodeModelSessionRequest sessionRequest
    in
    (getBaseUrl config ++ "/sessions/model")
        |> HttpBuilder.post
        |> HttpBuilder.withExpectJson decodeSession
        |> addHeaders config
        |> HttpBuilder.withJsonBody requestBody
        |> HttpBuilder.toRequest


encodeMissingValues : List String -> Encode.Value
encodeMissingValues missingValues =
    if List.isEmpty missingValues then
        Encode.null
    else
        Encode.list <| List.map Encode.string missingValues


encodeModelSessionRequest : ModelSessionRequest -> Encode.Value
encodeModelSessionRequest sessionRequest =
    Encode.object
        [ ( "dataSourceName", Encode.string <| dataSetNameToString <| sessionRequest.dataSourceName )
        , ( "name", encodeName sessionRequest.name )
        , ( "columns", encodeColumnMetadataList <| sessionRequest.columns )
        , ( "missingValues", encodeMissingValues sessionRequest.missingValues )
        , ( "predictionDomain", Encode.string <| toString <| sessionRequest.predictionDomain )
        , ( "extraParameters", encodeExtraParameters <| sessionRequest )
        ]


{-| Details used to Forecast using time series algorithms. `startDate` and `endDate` is the date range that should be forecasted.
`resultInterval` is the time interval at which the forecast should be made.
-}
type alias ForecastSessionRequest =
    { name : Maybe String
    , dataSourceName : DataSetName
    , columns : List ColumnMetadata
    , missingValues : List String
    , dates :
        { startDate : ZonedDateTime
        , endDate : ZonedDateTime
        }
    , resultInterval : ResultInterval
    }


{-| POST a `ForecastSessionRequest`
-}
postForecast : ClientConfig -> ForecastSessionRequest -> Http.Request SessionData
postForecast config sessionRequest =
    let
        requestBody =
            encodeForecastSessionRequest sessionRequest
    in
    (getBaseUrl config ++ "/sessions/forecast")
        |> HttpBuilder.post
        |> HttpBuilder.withExpectJson decodeSession
        |> addHeaders config
        |> HttpBuilder.withJsonBody requestBody
        |> HttpBuilder.toRequest


encodeForecastSessionRequest : ForecastSessionRequest -> Encode.Value
encodeForecastSessionRequest sessionRequest =
    Encode.object
        [ ( "dataSourceName", Encode.string <| dataSetNameToString <| sessionRequest.dataSourceName )
        , ( "name", encodeName sessionRequest.name )
        , ( "columns", encodeColumnMetadataList <| sessionRequest.columns )
        , ( "missingValues", encodeMissingValues sessionRequest.missingValues )
        , ( "startDate", Encode.string <| toISO8601 <| sessionRequest.dates.startDate )
        , ( "endDate", Encode.string <| toISO8601 <| sessionRequest.dates.endDate )
        , ( "resultInterval", Encode.string <| toString <| sessionRequest.resultInterval )
        ]


{-| Details used to determine Impact of an event using time series algorithms. `startDate` and `endDate` is the date range that should be measured for Impact.
`resultInterval` is the time interval at which the Impact should be made. `eventName` is a description name to help identify what impact is being measured in the date range.
-}
type alias ImpactSessionRequest =
    { name : Maybe String
    , dataSourceName : DataSetName
    , columns : List ColumnMetadata
    , missingValues : List String
    , dates :
        { startDate : ZonedDateTime
        , endDate : ZonedDateTime
        }
    , eventName : String
    , resultInterval : ResultInterval
    }


{-| POST a `ImpactSessionRequest`
-}
postImpact : ClientConfig -> ImpactSessionRequest -> Http.Request SessionData
postImpact config sessionRequest =
    let
        requestBody =
            encodeImpactSessionRequest sessionRequest
    in
    (getBaseUrl config ++ "/sessions/impact")
        |> HttpBuilder.post
        |> HttpBuilder.withExpectJson decodeSession
        |> addHeaders config
        |> HttpBuilder.withJsonBody requestBody
        |> HttpBuilder.toRequest


encodeImpactSessionRequest : ImpactSessionRequest -> Encode.Value
encodeImpactSessionRequest sessionRequest =
    Encode.object
        [ ( "dataSourceName", Encode.string <| dataSetNameToString <| sessionRequest.dataSourceName )
        , ( "name", encodeName sessionRequest.name )
        , ( "columns", encodeColumnMetadataList <| sessionRequest.columns )
        , ( "missingValues", encodeMissingValues sessionRequest.missingValues )
        , ( "startDate", Encode.string <| toISO8601 <| sessionRequest.dates.startDate )
        , ( "endDate", Encode.string <| toISO8601 <| sessionRequest.dates.endDate )
        , ( "eventName", Encode.string <| sessionRequest.eventName )
        , ( "resultInterval", Encode.string <| toString <| sessionRequest.resultInterval )
        ]


encodeName : Maybe String -> Encode.Value
encodeName name =
    name
        |> Maybe.map Encode.string
        |> Maybe.withDefault Encode.null


encodeExtraParameters : ModelSessionRequest -> Encode.Value
encodeExtraParameters sessionRequest =
    let
        balance =
            sessionRequest.balance
                |> Maybe.map Encode.bool
                |> Maybe.withDefault Encode.null

        anomalies =
            sessionRequest.containsAnomalies
                |> Maybe.map Encode.bool
                |> Maybe.withDefault Encode.null
    in
    Encode.object
        [ ( "balance", balance )
        , ( "containsAnomalies", anomalies )
        ]


{-| GET the Mahalanobis Distances calculated by an Anomaly Detection `Session` as a CSV String.
-}
getDistanceMetricsCsv : ClientConfig -> String -> Int -> Int -> Http.Request String
getDistanceMetricsCsv config sessionId page pageSize =
    let
        params =
            pageParams page pageSize
    in
    (getBaseUrl config ++ "/sessions/" ++ sessionId ++ "/results/mahalanobisdistances")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectString
        |> HttpBuilder.withHeader "Accept" "text/csv"
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest


{-| GET the Mahalanobis Distances calculated by an Anomaly Detection `Session`.
-}
getDistanceMetrics : ClientConfig -> String -> Int -> Int -> Http.Request DistanceMetrics
getDistanceMetrics config sessionId page pageSize =
    let
        params =
            pageParams page pageSize
    in
    (getBaseUrl config ++ "/sessions/" ++ sessionId ++ "/results/mahalanobisdistances")
        |> HttpBuilder.get
        |> HttpBuilder.withExpectJson decodeDistanceMetrics
        |> HttpBuilder.withQueryParams params
        |> addHeaders config
        |> HttpBuilder.toRequest