Why does Frame.tryValues fail in this simple example? - f#

The help for Frame.tryValues has the following:
"Given a data frame containing columns of type tryval<'T>, returns a new data frame that contains the underlying values of type 'T."
I interpreted this as meaning that the function would strip the type tryval from values and return those stripped values. Maybe I did not understand the text because the function fails in the following case:
let dates =
[ DateTime(2013,1,1);
DateTime(2013,1,2);
DateTime(2013,1,3) ]
let values = [ 10.0; 20.0; 30.0 ]
let first = Series(dates, values)
let frame = Frame(["first"], [first])
let f (dt: DateTime) (row: ObjectSeries<string>) = row.Get("first") :?> double
let s =
frame
|> Frame.tryMapRows f
// frame1's second column has tryvalues
let frame1 = Frame(["first"; "second"], [first; s])
// frame2 has no tryvalues
let frame2 = Frame(["first"; "second"], [first; first])
let frame3 =
frame1
|> Frame.tryValues
// fails
let frame3 =
frame2
|> Frame.tryValues
// Ok, works fine
Why does the first call to Frame.tryValues above fail but the second does not?

This turns out to be a bug in Deedle. I looked into it and submitted a PR with a fix.

Related

FSCL error on a simple example

I am trying to use openCL with FSCL on F# but I am obtaining some errors that I don't understand
open FSCL.Compiler
open FSCL.Language
open FSCL.Runtime
open Microsoft.FSharp.Linq.RuntimeHelpers
open System.Runtime.InteropServices
[<StructLayout(LayoutKind.Sequential)>]
type gpu_point2 =
struct
val mutable x: float32
val mutable y: float32
new ( q ,w) = {x=q; y=w}
end
[<ReflectedDefinition>]
let PointSum(a:gpu_point2,b:gpu_point2) =
let sx =(a.x+b.x)
let sy =(a.y+b.y)
gpu_point2(sx,sy)
[<ReflectedDefinition;Kernel>]
let Modgpu(b:float32[], c:float32[],wi:WorkItemInfo) =
let gid = wi.GlobalID(0)
let arp = Array.zeroCreate<gpu_point2> b.Length
let newpoint = gpu_point2(b.[gid],c.[gid])
arp.[gid] <- newpoint
arp
[<ReflectedDefinition;Kernel>]
let ModSum(a:gpu_point2[],b:gpu_point2[],wi:WorkItemInfo) =
let gid = wi.GlobalID(0)
let cadd = Array.zeroCreate<gpu_point2> a.Length
let newsum = PointSum(a.[gid],b.[gid])
cadd.[gid] <- newsum
cadd
[<ReflectedDefinition;Kernel>]
let ModSum2(a:gpu_point2[],b:gpu_point2[],wi:WorkItemInfo) =
let gid = wi.GlobalID(0)
let cadd = Array.zeroCreate<gpu_point2> a.Length
let newsum = gpu_point2(a.[gid].x+b.[gid].x,a.[gid].y+b.[gid].y)
cadd.[gid] <- newsum
cadd
let ws = WorkSize(64L)
let arr_s1= <# Modgpu([|0.f..63.f|],[|63.f..(-1.f)..0.f|],ws)#>.Run()
let arr_s2 = <# Modgpu([|63.f..(-1.f)..0.f|],[|0.f..63.f|],ws)#>.Run()
With this code when I try to use ModSum as
let rsum = <# ModSum(arr_s1,arr_s2,ws)#>.Run()
doesn't work, but instead when I use ModSum2 works perfectly
let rsum = <# ModSum2(arr_s1,arr_s2,ws)#>.Run()
The error I obtain the first time I run it is
FSCL.Compiler.CompilerException: Unrecognized construct in kernel body NewObject (gpu_point2, sx, sy)
and if I re-run the fsi console says
System.NullReferenceException: Object reference not set to an instance of an object.
The only thing I know is that the error doesn't comes from the use of another function since I can define a dot product function that works.
[<ReflectedDefinition>]
let PointProd(a:gpu_point2,b:gpu_point2) =
let f = (a.x*b.x)
let s = (a.y*b.y)
f+s
Thus, I guess the problem comes from the return type of PointSum, but is there a way to create such a function to sum two points and return the point type? And Why is not working?
Edit/Update:
Also with a record happens the same if I define the type as :
[<StructLayout(LayoutKind.Sequential)>]
type gpu_point_2 = {x:float32; y:float32}
If I try to create a function that directly sums two gpu_point_2 on a function works, but if I call a second function it raises the same error as using a struct.
Try to add [<ReflectedDefinition>] on the constructor of gpu_point2:
[<StructLayout(LayoutKind.Sequential)>]
type gpu_point2 =
struct
val mutable x: float32
val mutable y: float32
[<ReflectedDefinition>] new (q, w) = {x=q; y=w}
end
Normally each code that is called from the device need this attribute, constructors included.

Assigning a value post while-loop results in error

I'm struggling to figure out why I receive the following error:
Block following this 'let' is unfinished. Expect an expression.
let hashset = System.Collections.Generic.HashSet<int>()
let mutable continueLooping = true
while (continueLooping) do
let value = System.Random().Next(0, 12)
let success = hashset.Add(value)
continueLooping <- hashset.Count <> 12
let z = hashet
The error is based on the following line:
let z = hashset
Why am I receiving this error?
NOTE:
I am new to F#. As a result, please forgive my ignorance.
as far as I can tell it's just because you mixed tabs and spaces in there - and in deed this works if I evaluate it in FSharpInteractive:
let hashset = System.Collections.Generic.HashSet<int>()
let mutable continueLooping = true
while (continueLooping) do
let value = System.Random().Next(0, 12)
let success = hashset.Add(value)
continueLooping <- hashset.Count <> 12
let z = hashset
evaluates to
val hashset : System.Collections.Generic.HashSet<int>
val mutable continueLooping : bool = false
val z : System.Collections.Generic.HashSet<int>
then z |> Seq.toArray evaluates to
val it : int [] = [|2; 8; 9; 3; 4; 10; 5; 11; 0; 6; 1; 7|]
which seems fine
btw: as you have a slight typo in there: ... z = hashet instead of hashsetI think you did not copy&paste the code that caused your error anyways.

DateTime conversion to Excel Date in f#

I read on stackoverflow that easiest way to convert a DateTime variable back to Excel date was simply to do:
let exceldate = int(DateTime)
Admittedly this was in c# and not f#. This is supposed to work as the decimals represent time and int part represents date. I tried this and f# comes back with the error:
The type 'DateTime' does not support a conversion to the type 'int'
So how do I convert back to excel date?
More specificly, I m trying to create a vector of month 1st for a period between start date and end date. Both vector output and start date and end date are floats, i.e. excel dates. Here my clumsy first attempt:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new DateTime(stddt.Year, stddt.Month, 1)
let vecend = new DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- float(vecstart)
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = float temp2
dtsvec
This doesnt work because of the conversion issue and is rather complicated and imperative.
How do I do the conversion? How can I do this more functionally? Thanks
Once you have the DateTime object, just call ToOADate, like so:
let today = System.DateTime.Now
let excelDate = today.ToOADate()
So your example would end up like so:
let monthlies (std:float) (edd:float) =
let stddt = System.DateTime.FromOADate std
let edddt = System.DateTime.FromOADate edd
let vecstart = new System.DateTime(stddt.Year, stddt.Month, 1)
let vecend = new System.DateTime(edddt.Year, edddt.Month, 1)
let nrmonths = 12 * (edddt.Year-stddt.Year) + edddt.Month - stddt.Month + 1
let scaler = 1.0 - (float(stddt.Day) - 1.0) / float(System.DateTime.DaysInMonth(stddt.Year , stddt.Month))
let dtsvec:float[] = Array.zeroCreate nrmonths
dtsvec.[0] <- vecstart.ToOADate()
for i=1 to (nrmonths-1) do
let temp = System.DateTime.FromOADate dtsvec.[i-1]
let temp2 = temp.AddMonths 1
dtsvec.[i] = temp2.ToOADate()
dtsvec
In regards to getting rid of the loop, maybe something like this?
type Vector(x: float, y : float) =
member this.x = x
member this.y = y
member this.xDate = System.DateTime.FromOADate(this.x)
member this.yDate = System.DateTime.FromOADate(this.y)
member this.differenceDuration = this.yDate - this.xDate
member this.difference = System.DateTime.Parse(this.differenceDuration.ToString()).ToOADate
type Program() =
let vector = new Vector(34.0,23.0)
let difference = vector.difference

Dereferencing a ref inside a function yields different results. Why?

In this sample, get_final_answer is being eagerly evaluated, and always returns 0.0. I thought expressions containing refs were treated differently (and not eagerly evaluated in this case) due to their inherently mutable characteristics. I expected it to return 7.0.
let FinalAnswer = ref 0.0
let get_final_answer = !FinalAnswer
let rec eval_expr_fail =
FinalAnswer := 7.0
get_final_answer // fails, returns 0.0
let rec eval_expr_works =
FinalAnswer := 7.0
!FinalAnswer // works, return 7.0
How do I dereference FinalAnswer outside the block where I updated it?
get_final_answer in let get_final_answer = !FinalAnswer is a float value, not a function. It is the value of 0.0, and has nothing to do with FinalAnswer once the value is assigned.
Making it as a function gets what you want:
let FinalAnswer = ref 0.0
let get_final_answer() = !FinalAnswer
let rec eval_expr_fail =
FinalAnswer := 7.0
get_final_answer () // returns 7.0

How to convert string array to float array and substitute Double.NaN for non-numeric values?

I'm writing a parser for CSV data, and am trying to determine how to handle records
that are blank ("") or contain character data ("C"). The parser code I have below works great, but forces me to deal with the float conversions later. I'd like to be able to just make my string[][] a float[][], and handle the conversions when I parse the file, but I notice that it blows up with any non-numeric data. Ideally there would be no non-numeric or blank values, but they are unavoidable, and as such, have to be dealt with.
Can someone possibly recommend a concise approach to attempt to convert to Double, and then if it doesn't work, replace with Double.NaN instead? (Without sacrificing much performance if possible). Thank you.
let stringLine = [| "2.0"; "", "C"|]
let stringLine2Float = Array.map float stringLine
//desiredFloatArray = [| 2.0; Double.NaN; Double.NaN |]
type csvData = { mutable RowNames: string[]; mutable ColNames: string[]; mutable Data: string[][] }
let csvParse (fileString: string) =
let colNames = ((fileLines fileString |> Seq.take 1 |> Seq.nth 0).Split(',')).[1..]
let lines = fileLines fileString |> Seq.skip 1 |> Array.ofSeq
let rowNames = Array.init lines.Length string;
let allData : string [][] = Array.zeroCreate rowNames.Length
for i in 0..rowNames.Length - 1 do
let fields = lines.[i].Split(',')
allData.[i] <- fields.[1..]
rowNames.[i] <- fields.[0]
{ RowNames = rowNames; ColNames = colNames; Data = allData }
Use this instead of the built-in float conversion:
let cvt s =
let (ok,f) = System.Double.TryParse(s)
if ok then f else nan

Resources