Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Fix new emitters #83

Merged
merged 25 commits into from
Apr 12, 2021
Merged

Fix new emitters #83

merged 25 commits into from
Apr 12, 2021

Conversation

alfredclwong
Copy link
Collaborator

@alfredclwong alfredclwong commented Apr 7, 2021

[WIP]

I didn't get a chance to review PR #79 as I was off on holiday. Having a look at it now, several things slipped through the code review. These are the most obvious fixes - when I have more time, I may have a deeper look. There are also some issues from PR #70.

  • We now have Emitter.jl, Emitter2.jl and Emitters.jl in the main branch. I don't agree with the decision to make dealing with this a low-priority matter, so I'll get this fixed here (delegated to Issue #104).
  • Some type signatures need fixing. For example, in
    right(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,1], t[2,1], t[3,1]))
    up(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,2], t[2,2], t[3,2]))
    forward(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,3], t[2,3], t[3,3]))
    OpticSim.origin(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = Vec3(t[1,4], t[2,4], t[3,4])
    the parametric type T is unused, and in
    Base.length(d::Constant{T}) where {T} = 1
    and a few other cases, it's entirely redundant.
  • Several relative import statements don't seem correct. For example,
    using ..OpticSim
    using DataFrames
    using Distributions
    using ..Emitters
    doesn't make sense to me, as OpticSim and Emitters should be on different levels of nesting.
  • Stylistically, Vec3/Vec4 may need some discussion. Since we're working with computer graphics, I'm not wholly opposed to explicit Vec3/Vec4 types, but some of the implementations, e.g.
    one3(::Type{T} = Float64) where {T<:Real} = Vec3{T}(one(T), one(T), one(T))
    are redundant due to LinearAlgebra (here, ones(Vec3{T}) would suffice). I think there are also some instances where the type signatures could make better use of multiple dispatch.
  • Spelling/grammar mistakes.
  • In https://microsoft.github.io/OpticSim.jl/dev/csg/, csg_ex.png is broken due to rotationd not being in scope. I'll change the doc build Github Action to fail on things like this now (I wasn't doing this because of the zoom lens example, but I'll comment that out).
  • Docstring errors (delegated to Issue #103).
  • What is this struct for? @galran
    struct MissingImplementationException <: Exception
    msg::String
    end
  • Emitters testset (delegated to Issue #104).

Further issues:

  • Redefinition of const parametric type T. @galran what's the intended purpose here?
    T = eltype(power)
  • export Geometry called from Emitters module. This is exporting an imported module.
    export Geometry, Spectrum, Directions, Origins, AngularPower, Sources
  • export generate called from Directions and Origins submodules.
    export generate
    export generate
  • Looking at these lines again, it seems that they should actually reside in Transform.jl. @galran is OpticSim.origin extended explicitly here because some previous code uses it?
    right(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,1], t[2,1], t[3,1]))
    up(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,2], t[2,2], t[3,2]))
    forward(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = normalize(Vec3(t[1,3], t[2,3], t[3,3]))
    OpticSim.origin(t::OpticSim.Geometry.Transform{T}) where {T<:Real} = Vec3(t[1,4], t[2,4], t[3,4])

1. rm Emitter2.jl
2. split Emitters.jl into separate submodule files
3. mv Emitters.Visualization -> Vis
4. rm hacky root-level include("Emitters.jl")
5. fix incorrect import statements
@codecov-io

This comment has been minimized.

@friggog friggog changed the title Fix new emitters [WIP] Fix new emitters Apr 7, 2021
Copy link
Contributor

@BrianGun BrianGun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deep nesting of modules still seems unnecessarily complicated to me but in the interests of making progress let's try this out and see how it works.

@alfredclwong alfredclwong mentioned this pull request Apr 7, 2021
15 tasks
@alfredclwong
Copy link
Collaborator Author

@galran I've now set the documentation build to fail on strict requirements, so we'll need to fix all the missing docstrings. This is the current list:

┌ Error: 15 docstrings not included in the manual:
│ 
│     OpticSim.Geometry.local2world :: Union{Tuple{StaticArrays.SMatrix{4, 4, T, 16}}, Tuple{T}} where T<:Real
│     OpticSim.Geometry.decomposeRTS :: Union{Tuple{StaticArrays.SMatrix{4, 4, T, 16}}, Tuple{T}} where T<:Real
│     OpticSim.GlassCat.CARGILLE.OG0607
│     OpticSim.Geometry.world2local :: Union{Tuple{StaticArrays.SMatrix{4, 4, T, 16}}, Tuple{T}} where T<:Real
│     OpticSim.NotebooksUtils.SetBackend :: Tuple{OpticSim.NotebooksUtils.Defs, String}
│     OpticSim.Emitters.Spectrum.spectrumpower :: Union{Tuple{T}, Tuple{OpticSim.Emitters.Spectrum.Measured{T}, T}} where T<:Real
│     OpticSim.GlassCat.CARGILLE.OG0608
│     OpticSim.NotebooksUtils.run :: Tuple{}
│     OpticSim.NotebooksUtils.run :: Tuple{Any}
│     OpticSim.NotebooksUtils.InitNotebook :: Tuple{}
│     OpticSim.Geometry.scale :: Tuple{T} where T<:Real
│     OpticSim.Geometry.scale :: Union{Tuple{T}, Tuple{T, T, T}} where T<:Real
│     OpticSim.Geometry.scale :: Union{Tuple{OpticSim.Geometry.Vec3{T}}, Tuple{T}} where T<:Real
│     OpticSim.GlassCat.CARGILLE.OG081160
│     OpticSim.NotebooksUtils.run_sample :: Union{Tuple{String}, Tuple{String, Bool}}
│ 
│ 
│ These are docstrings in the checked modules (configured with the modules keyword)
│ that are not included in @docs or @autodocs blocks.
└ @ Documenter.DocChecks ~/.julia/packages/Documenter/bFHi4/src/DocChecks.jl:69

Could you please add references to these into the appropriate .md files in docs? Thanks.

@BrianGun, the CARGILLE docstrings are a bit awkward. I think our two best options are either (1) to include a documentation page for CARGILLE, or (2) to remove CARGILLE entirely - currently it's only used once, in a testset.

@friggog
Copy link
Collaborator

friggog commented Apr 8, 2021

@alfredclwong if you are no longer providing docs for all glasscat catalogs (which seems like a sensible choice) then it’s probably best to just remove the docstrings from cargille, there shouldn’t be any need to remove the code

Also updating the complete reference page in the docs with new sub modules (using auto docs) will remove these warnings if you don’t want dedicated docs pages for all these

@alfredclwong
Copy link
Collaborator Author

@alfredclwong if you are no longer providing docs for all glasscat catalogs (which seems like a sensible choice) then it’s probably best to just remove the docstrings from cargille, there shouldn’t be any need to remove the code

Also updating the complete reference page in the docs with new sub modules (using auto docs) will remove these warnings if you don’t want dedicated docs pages for all these

Removing the docstrings seems like the way to go. Or at least making them into multi-line comments. @BrianGun, do you have any objection to this? There is a tiny bit of convenience in seeing the glass info when you hover over a glass in the source code.

autodocs also sounds good. @galran, did you want to put those docstrings in new_emitters.md at all? If not, I'll just add them to ref.md.

@BrianGun
Copy link
Contributor

BrianGun commented Apr 8, 2021

@alfredclwong if you are no longer providing docs for all glasscat catalogs (which seems like a sensible choice) then it’s probably best to just remove the docstrings from cargille, there shouldn’t be any need to remove the code
Also updating the complete reference page in the docs with new sub modules (using auto docs) will remove these warnings if you don’t want dedicated docs pages for all these

Removing the docstrings seems like the way to go. Or at least making them into multi-line comments. @BrianGun, do you have any objection to this? There is a tiny bit of convenience in seeing the glass info when you hover over a glass in the source code.

autodocs also sounds good. @galran, did you want to put those docstrings in new_emitters.md at all? If not, I'll just add them to ref.md.

I don't have any objection to removing the docstrings.

BrianGun
BrianGun previously approved these changes Apr 8, 2021
BrianGun
BrianGun previously approved these changes Apr 9, 2021
@BrianGun
Copy link
Contributor

BrianGun commented Apr 9, 2021

@alfredclwong documentation check is failing.

* remove random whitespace
* remove redundant comments above docstrings
* fix docstrings to follow existing conventions
* fix many function type signatures
* move exports to top of files to provide clear submodule headers
* Int -> Integer
* added empty docstrings for generate, visual_size and apply
  these functions clearly need documentation
@alfredclwong alfredclwong changed the title [WIP] Fix new emitters Fix new emitters Apr 11, 2021
@alfredclwong alfredclwong requested a review from BrianGun April 11, 2021 09:25
@alfredclwong alfredclwong enabled auto-merge (squash) April 11, 2021 09:37
@alfredclwong
Copy link
Collaborator Author

In 1916e2e, I'm not sure if Int or Integer is preferable. I chose Integer because it's more generic, but it might decrease performance because of this. @galran @BrianGun @friggog any thoughts?

alfredclwong added a commit that referenced this pull request Apr 11, 2021
@friggog
Copy link
Collaborator

friggog commented Apr 11, 2021

@alfredclwong it might be a problem, Integer is an abstract type (supertype of all ints) so may not have known size to the compiler, possibly resulting in allocations. Int is an alias for Int32 or Int64 depending on the system, so is always of known size to the compiler. It might be ok, but make sure you check carefully whether any runtime allocations have been introduced

@alfredclwong
Copy link
Collaborator Author

@alfredclwong it might be a problem, Integer is an abstract type (supertype of all ints) so may not have known size to the compiler, possibly resulting in allocations. Int is an alias for Int32 or Int64 depending on the system, so is always of known size to the compiler. It might be ok, but make sure you check carefully whether any runtime allocations have been introduced

That's what I'm worried about. I feel like having abstract types in function signatures is OK, but there might be performance issues if we allow struct fields to be too generic. What's the best way to test for allocations - adding representative workloads to Benchmarks.jl?

@alfredclwong
Copy link
Collaborator Author

alfredclwong commented Apr 12, 2021

@galran do you have any recommendations for a representative workload for the Emitters code?

If you could add these to Benchmarks.jl, it'd be very helpful.

@BrianGun
Copy link
Contributor

There are several things to consider. First what is the intent of the type? Here are the subtypes of Integer:

Bool
Signed <- ( BigInt, Int128, Int16, Int32, Int64, Int8) where <- means the elements on the right are subtypes
Unsigned <- (UInt128, UInt16, UInt32, UInt64, UInt8)

Will this code even work properly when passed all of these subtypes? Does it make sense to have this degree of generality? If the answer is no then it shouldn't be Integer.

Then there is the performance consideration. In general giving struct fields abstract types will lead to poor performance for the reasons @friggog mentioned. In this particular case Int64 is probably a better idea than Integer. No performance hit, no allocation, and plenty big enough to represent enough rays to keep a computer busy for a long time (billions of seconds, at least).

@alfredclwong
Copy link
Collaborator Author

alfredclwong commented Apr 12, 2021

Agreed, I'll revert the commit in a bit

@galran
Copy link
Collaborator

galran commented Apr 12, 2021

regarding the emitters, the only thing i can think of is running a couple of the examples (from the documentation or the notebook) - or at least just instantiating the non-visual part in order to not drag mesa into the mix.

@alfredclwong alfredclwong merged commit 999b8a6 into main Apr 12, 2021
@alfredclwong alfredclwong deleted the fix-new-emitters branch April 12, 2021 22:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants