How about letting W be the finite element space that has the dofs you are referring to as all dofs in the mesh. Let f be the "function for filtering". Then do
v = TestFunction(W)
L = f*v*dx
b = assemble(L)
Now b contains the value for each dof in the mesh.