Skip to content

Commit

Permalink
Merge pull request #33 from EcoJulia/mkb/location_refactor
Browse files Browse the repository at this point in the history
Adapt to new EcoBase.AbstractPlaces
  • Loading branch information
mkborregaard authored Feb 10, 2019
2 parents 26cdbf6 + 14fc4ec commit b1a40fb
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 49 deletions.
26 changes: 13 additions & 13 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ version = "0.5.1"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "ec61a16eed883ad0cfa002d7489b3ce6d039bb9a"
git-tree-sha1 = "49269e311ffe11ac5b334681d212329002a9832a"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "1.4.0"
version = "1.5.1"

[[DataFrames]]
deps = ["CategoricalArrays", "CodecZlib", "Compat", "DataStreams", "Dates", "InteractiveUtils", "IteratorInterfaceExtensions", "LinearAlgebra", "Missings", "Printf", "Random", "Reexport", "SortingAlgorithms", "Statistics", "StatsBase", "TableTraits", "Tables", "Test", "TranscodingStreams", "Unicode", "WeakRefStrings"]
Expand All @@ -47,9 +47,9 @@ version = "0.4.1"

[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections", "Random", "Serialization", "Test"]
git-tree-sha1 = "8fc6e166e24fda04b2b648d4260cdad241788c54"
git-tree-sha1 = "ca971f03e146cf144a9e2f2ce59674f5bf0e8038"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.14.0"
version = "0.15.0"

[[Dates]]
deps = ["Printf"]
Expand All @@ -65,9 +65,9 @@ uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[EcoBase]]
deps = ["Compat", "RecipesBase"]
git-tree-sha1 = "7d05be63ecb77148b3807c89b8977d6397499618"
path = "/Users/michael/.julia/dev/EcoBase"
uuid = "a58aae7d-b440-5a11-b283-399458f99aac"
version = "0.0.4"
version = "0.0.4+"

[[Future]]
deps = ["Random"]
Expand Down Expand Up @@ -102,9 +102,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Missings]]
deps = ["Dates", "InteractiveUtils", "SparseArrays", "Test"]
git-tree-sha1 = "adc26d2ee85a49c413464110d922cf21efc9d233"
git-tree-sha1 = "d1d2585677f2bd93a97cfeb8faa7a0de0f982042"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "0.3.1"
version = "0.4.0"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down Expand Up @@ -201,10 +201,10 @@ uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "0.4.1"

[[Tables]]
deps = ["Requires", "Test"]
git-tree-sha1 = "940944e6b68a35046282897a2218891c7cf14a32"
deps = ["IteratorInterfaceExtensions", "LinearAlgebra", "Requires", "TableTraits", "Test"]
git-tree-sha1 = "37be2ed169d5771c1ac8d516d3bcb0093c49966e"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "0.1.12"
version = "0.1.15"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
Expand All @@ -225,6 +225,6 @@ uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[WeakRefStrings]]
deps = ["Missings", "Random", "Test"]
git-tree-sha1 = "1087e8be380f2c8b96434b02bb1150fc1c511135"
git-tree-sha1 = "bccf012a0e8815410ad8b6846944a33903992fc3"
uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
version = "0.5.3"
version = "0.5.6"
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RecipesBase
DataFrames 0.15
DataFramesMeta
StatsBase
EcoBase 0.0.4
EcoBase 0.0.5
RandomBooleanMatrices 0.1.1
RandomNumbers
#Phylo
8 changes: 4 additions & 4 deletions src/Constructor_helperfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function createsitenames(coords::DataFrames.DataFrame)
end

creategrid(coords::AbstractMatrix{<:Union{AbstractFloat, Missing}}, tolerance = sqrt(eps())) =
GridTopology(gridvar(coords[:,1], tolerance)..., gridvar(coords[:,2], tolerance)...)
GridTopology(gridvar(coords[:,1], tolerance), gridvar(coords[:,2], tolerance))

# could allow for n-dimensional binning, using code from StatsBase.Histogram
function gridvar(x, tolerance = sqrt(eps()))
Expand All @@ -158,11 +158,11 @@ function gridvar(x, tolerance = sqrt(eps()))
min = minimum(sux)
cellnumber = round(Int, maxrange(sux) / cellsize) + 1

min, cellsize, cellnumber
range(min, step = cellsize, length = cellnumber)
end

function getindices(coords::AbstractMatrix{<:Union{AbstractFloat, Missing}}, grid::GridTopology, tolerance = 2*sqrt(eps()))
index1 = 1 .+ floor.(Int,(coords[:,1] .- grid.xmin) ./ grid.xcellsize .+ tolerance)
index2 = 1 .+ floor.(Int,(coords[:,2] .- grid.ymin) ./ grid.ycellsize .+ tolerance)
index1 = 1 .+ floor.(Int,(coords[:,1] .- xmin(grid)) ./ xcellsize(grid) .+ tolerance)
index2 = 1 .+ floor.(Int,(coords[:,2] .- ymin(grid)) ./ ycellsize(grid) .+ tolerance)
hcat(index1, index2)
end
2 changes: 1 addition & 1 deletion src/Constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Assemblage(site::P, occ::SpeciesData{D};
end

function createLocations(coords::AbstractMatrix, cdtype::coordstype = auto, #by design, this is not type stable, but maybe that is OK for type constructors
sitestat = DataFrames.DataFrame(sites = sitenames(occ)))
sitestat = DataFrames.DataFrame(sites = 1:size(coords, 1)))

cdtype == pointdata && return Locations{PointData}(PointData(coords), sitestat)
cdtype == griddata && return Locations{GridData}(GridData(coords), sitestat)
Expand Down
24 changes: 10 additions & 14 deletions src/DataTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@
# Insertion point for defining my own functions. They are mainly abstract
# over the full type and it's view, so another possibility would be to
# implement with unions
abstract type SESpatialData <: EcoBase.AbstractPlaces end
abstract type SELocations <: EcoBase.AbstractLocations end
abstract type SESpatialData{T<:EcoBase.AbstractLocationData} <: EcoBase.AbstractPlaces{T} end
abstract type SELocations{T<:EcoBase.AbstractLocationData} <: EcoBase.AbstractPlaces{T} end
abstract type SEThings{D <: Real} <: EcoBase.AbstractThings end
abstract type SEGrid <: EcoBase.AbstractGrid end
abstract type SEPointData end
abstract type SEPoints <: EcoBase.AbstractPoints end

abstract type AbstractComMatrix{ D<:Real } end
abstract type AbstractComMatrix{D<:Real} end


# I could implement sitestats as a Dict with several DataFrames to make space for big data sets, but I prefer to not do this now. Example below.

# I could do a lot more with immutable types if I had a clearer view/copy implementation
mutable struct GridTopology <: EcoBase.AbstractGrid
xmin::Number
xcellsize::Number
xcells::Int
ymin::Number
ycellsize::Number
ycells::Int
xs::StepRangeLen{Float64}
ys::StepRangeLen{Float64}
end

mutable struct Bbox
Expand All @@ -33,7 +29,7 @@ mutable struct Bbox
end

# Do I need sitenames here? I think so, they should match those in sitestats, and be separate
mutable struct PointData <: SEPointData
mutable struct PointData <: SEPoints
coords::Matrix{Float64}
end

Expand All @@ -42,7 +38,7 @@ mutable struct GridData <: SEGrid
grid::GridTopology
end

mutable struct Locations{T<:Union{GridData, PointData}} <: SELocations
mutable struct Locations{T<:Union{GridData, PointData}} <: SELocations{T}
coords::T
sitestats::DataFrames.DataFrame
function Locations{T}(coords, sitestats = DataFrames.DataFrame(id = 1:size(coords,1))) where T
Expand Down Expand Up @@ -73,8 +69,8 @@ end
SpeciesData(commatrix::ComMatrix{D}, traits) where D<:Real = SpeciesData{D}(commatrix, traits)

# Not really sure what this type is for
mutable struct SiteData{S} <: SESpatialData where S <: Locations
site::S
mutable struct SiteData{T<:Union{GridData, PointData}} <: SESpatialData{T}
site::T
end

abstract type SEAssemblage{D<:Real, T<:SEThings, P<:SELocations} <: EcoBase.AbstractAssemblage{D, T, P} end
Expand Down
4 changes: 2 additions & 2 deletions src/DispersionFields.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

mutable struct DispersionField <: SESpatialData
mutable struct DispersionField{T} <: SESpatialData{T}
site::SELocations
DFmat::Matrix{Int}

function DispersionField(asm::Assemblage)
df = asm.occ.occurrences' * asm.occ.occurrences
new(asm.site, df)
new{typeof(asm.site.coords)}(asm.site, df)
end
end
2 changes: 1 addition & 1 deletion src/GetandSetdata.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coordinates(asm::SEAssemblage) = coordinates(asm.site)
coordinates(sd::SiteData) = coordinates(sd.site)
coordinates(pd::SEPointData) = pd.coords
coordinates(pd::SEPoints) = pd.coords
coordinates(l::SELocations) = coordinates(getcoords(l))
function coordinates(gd::SEGrid)
index1 = xrange(gd.grid)[gd.indices[:,1]]
Expand Down
14 changes: 7 additions & 7 deletions src/Gridfunctions.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
xmin(g::GridTopology) = g.xmin
ymin(g::GridTopology) = g.ymin
xcellsize(g::GridTopology) = g.xcellsize
ycellsize(g::GridTopology) = g.ycellsize
xcells(g::GridTopology) = g.xcells
ycells(g::GridTopology) = g.ycells
xmin(g::GridTopology) = minimum(g.xs)
ymin(g::GridTopology) = minimum(g.ys)
xcellsize(g::GridTopology) = step(g.xs)
ycellsize(g::GridTopology) = step(g.ys)
xcells(g::GridTopology) = length(g.xs)
ycells(g::GridTopology) = length(g.ys)
indices(g::SEGrid) = g.indices
indices(g::SEGrid, idx) = g.indices[:,idx]
indices(g::SEGrid, idx) = g.indices[:,idx]
boundingbox(g::GridTopology) = Bbox(xmin(g), xmax(g), ymin(g), ymax(g))

@forward_func GridData.grid xmin, ymin, xcellsize, ycellsize, cellsize, xcells, ycells, cells, xrange, yrange, xmax, ymax, boundingbox
Expand Down
12 changes: 6 additions & 6 deletions src/Subsetting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ mutable struct SubGridData <: SEGrid
grid::GridTopology
end

mutable struct SubPointData
mutable struct SubPointData <: SEPoints
coords::SubArray{Float64,2}
end

mutable struct SubLocations{T<:Union{SubGridData, SubPointData}} <: SELocations
mutable struct SubLocations{T<:Union{SubGridData, SubPointData}} <: SELocations{T}
coords::T
sitestats::DataFrames.SubDataFrame
end
Expand All @@ -34,7 +34,7 @@ mutable struct SubAssemblage{D <: Real, P <: SubLocations} <: SEAssemblage{D, Su
end

# TODO delete
mutable struct SubSiteData{S} <: SESpatialData where S <: SubLocations
mutable struct SubSiteData{S<:SubLocations} <: SESpatialData{S}
site::S
end

Expand All @@ -55,7 +55,7 @@ function view(com::AbstractComMatrix; species = 1:nspecies(com), sites = 1:nsite
SubComMatrix(view(com.occurrences, spec, sit), view(com.speciesnames, spec), view(com.sitenames, sit)) #TODO change the order of these in the object to fit the array index order
end

view(pd::SEPointData, sites) = SubPointData(view(pd.coords, sites, :))
view(pd::SEPoints, sites) = SubPointData(view(pd.coords, sites, :))
view(gd::SEGrid, sites) = SubGridData(view(gd.indices, sites, :), gd.grid)
view(lo::SELocations, sites) = SubLocations{SubGridData}(view(lo.coords, sites), view(lo.sitestats, sites, :))
view(sp::SESpatialData, sites = 1:nsites(sp)) = SubSiteData(view(sp.site, sites))
Expand Down Expand Up @@ -83,7 +83,7 @@ Assemblage(assm::SubAssemblage) = copy(assm)

copy(asm::SEAssemblage) = Assemblage(copy(asm.site), copy(asm.occ))
copy(sp::SESpatialData) = SiteData(copy(sp.site))
copy(pd::SEPointData) = PointData(copy(pd.coords), copy(pd.sitestats))
copy(pd::SEPoints) = PointData(copy(pd.coords), copy(pd.sitestats))
copy(pd::AbstractComMatrix) = ComMatrix(copy(pd.occurrences), copy(pd.speciesnames), copy(pd.sitenames))
copy(occ::SEThings) = SpeciesData(copy(occ.commatrix), my_dataframe_copy(occ.traits))
copy(lo::SELocations) = Locations(copy(lo.coords), my_dataframe_copy(lo.sitestats))
Expand Down Expand Up @@ -113,7 +113,7 @@ function subsetgrid(indices, grid)
ymin = yrange(grid)[minimum(indices[:,2])]
xcells = maxrange(indices[:,1]) + 1
ycells = maxrange(indices[:,2]) + 1
GridTopology(xmin, grid.xcellsize, xcells, ymin, grid.ycellsize, ycells)
GridTopology(range(xmin, step = xcellsize(grid), length = xcells), range(ymin, step = ycellsize(grid), length = ycells))
end

function show(io::IO, com::SubComMatrix)
Expand Down

0 comments on commit b1a40fb

Please sign in to comment.